Andi Vajda <va...@apache.org> wrote: > > On Mar 10, 2009, at 10:02, Bill Janssen <jans...@parc.com> wrote: > > > Andi Vajda <va...@apache.org> wrote: > > > >> On Mon, 9 Mar 2009, Bill Janssen wrote: > >> > >>> I just spent some time tracking down a core dump in a big Python > >>> program, in PyLucene, till I figured out that the thread making > >>> the call > >>> had never been registered as a Java thread (the code at some interim > >>> calling frame changed till it suddenly was calling into Java). > >>> > >>> Is there some way to make that throw an exception rather than a > >>> segfault > >>> -- "Attempt to call into Java from a non-Java thread"? That would > >>> be > >>> really useful. > >> > >> Yes, there is a way but I'm not sure it's too efficient. If the > >> JCCEnv::get_vm_env() method returns NULL it can mean that your thread > >> wasn't attached to the VM. More precisely, if you didn't attach your > >> thread, this method must be returning NULL. > >> > >> inline JNIEnv *get_vm_env() > >> { > >> return (JNIEnv *) pthread_getspecific(VM_ENV); > >> } > >> > >> The problem is that this method is called a _lot_ and adding code > >> checking the value and raising an exception would be costly for this > >> currently short inlined function. > >> > >> It would be ideal if a less frequently called, but nonetheless > >> indispensable, piece of code could catch that problem instead of > >> adding a check at that low a level. I haven't found one yet. Another > >> way to improve this (and the similar problem of missing the call to > >> initVM()) would be to have a better debug mode where the check would > >> be then compiled in. > > > > So, it sounds like I could add to my code a call to lucene.getVMEnv(). > > If it returns None, it's a bad thread, and I shouldn't make any other > > calls to lucene in it. That way I could control the efficiency > > concerns > > in application code. Right? > > I'm not sure (not near my computer at the moment) but if you are > prepared to call a piece of code, why not call attachCurrentThread() > then ? it's supposed to be a nop if it was called before. > > Andi..
Well, it's not supposed to happen, so it indicates a bug in the program flow, and I'd just as soon raise an exception and catch that bug, not swallow it. Bill