Exceptions during a script job's function are silent

I’ve been debugging an issue with a function that is triggered with a script job, and it’s been difficult because the errors are mysteriously suppressed – but only when triggered by the script job. It’s possible that there’s something fundamental about script jobs that I just do not yet understand.

Here’s what I’m seeing: when an exception occurs in the triggered function, the error and stack trace that should be logged is instead totally silenced… until the job is killed. When you do kill the job, the error will pop as if it had just occurred, even if many other operations have happened since.

Here’s some example code that illustrates the problem.



import pymel.core as pm

def foo():
    print "selected:",pm.selected()
    print "Everything is good!"

def bar():
    print "selected:",pm.selected()
    raise Exception("Something bad happened!!")
    print "Everything is bad!" # This won't happen, of course
    

goodJob = pm.scriptJob( e=["SelectionChanged", foo])
badJob  = pm.scriptJob( e=["SelectionChanged", bar])

Select some objects, see in the log we see the selected objects from both , but we do not see the exception pop. Kill the jobs…


pm.scriptJob( kill=goodJob )
pm.scriptJob( kill=badJob )

and now you’ll see the exception.

So what’s going on here? What can I do about it? Obviously this is bad, because if a user hits an error we’ll never know about it, since it appears everything is working fine (which is actually what happened here).

I ran into this a couple of days ago too, exceptions were being suppressed when raised in a function executed via a scriptJob.

I’ve not hit on a solution yet either, but anything I find out I’ll post here.

See what happens if you call maya.utils.processIdleEvents() during the interval between the error and the printout

Adding the call to maya.utils.processIdleEvents() in the bar function after the exception is raised has no apparent effect.

Disappointing, I expected that to work.

Thanks, Theodox. It’s cool that you’re still so active on the forums :slight_smile:

I tried processIdleEvents in my example. I did get a crash the first time, which was weird, but subsequent tests seemed fine.

My actual scriptjob function is a bit more complex. Piggybacking off that idea, this is what I did:

mayautils.executeDeferred( my_scriptjob_function )

As a point of curiosity, I do wonder why the exception would only pop when we’re in the idle event loop, but it seems to be working so far. :slight_smile: