You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pylucene-dev@lucene.apache.org by Bill Janssen <ja...@parc.com> on 2009/03/09 10:21:21 UTC

core dump with unregistered threads

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.

Bill

Re: core dump with unregistered threads

Posted by Andi Vajda <va...@apache.org>.
On Tue, 10 Mar 2009, Bill Janssen wrote:

> Andi Vajda <va...@apache.org> wrote:

>> 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.

Good point. getVMEnv() returns the global env object created when you called 
initVM(). Before that call, it returns None. Once you've called initVM(), 
it returns that env from any thread.

This makes sense since you need that env object to call 
attachCurrentThread(), a method on it.

To see if you've already attached this thread, I added a new method on the 
env object returned by initVM() or getVMEnv(), called 
isCurrentThreadAttached(). This method should be very fast, it just checks 
that JCCENv::get_vm_env() returns non NULL.

For example:

    >>> from lucene import *
    >>> getVMEnv()
    >>> initVM(CLASSPATH)
    <jcc.JCCEnv object at 0x293a0>
    >>> from threading import Thread
    >>> def foo(): print getVMEnv().isCurrentThreadAttached()
    ...
    >>> Thread(target=foo).start()
    False
    >>> def bar(): getVMEnv().attachCurrentThread(); print getVMEnv().isCurrentThreadAttached()
    ...
    >>> Thread(target=bar).start()
    True

This got checked into rev 752295.

Andi..

Re: core dump with unregistered threads

Posted by Bill Janssen <ja...@parc.com>.
Andi Vajda <va...@apache.org> wrote:

> 
> On Mar 10, 2009, at 10:02, Bill Janssen <ja...@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

Re: core dump with unregistered threads

Posted by Andi Vajda <va...@apache.org>.
On Mar 10, 2009, at 10:02, Bill Janssen <ja...@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..

>
>
> Bill

Re: core dump with unregistered threads

Posted by Bill Janssen <ja...@parc.com>.
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?

Bill

Re: core dump with unregistered threads

Posted by Andi Vajda <va...@apache.org>.
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.

Andi..