I just tried calling to_proc on a block, but the NPE is still popping up
sporadically.
Here is the code:
def daemon_thread(name, &block)
x = block.to_proc
t = java.lang.Thread.new(as_runnable { x.call })
t.name = name
t.daemon = true
t.start
t
end
My debugging traced the NPE to this method in ThreadService.java, which
returns null:
public ThreadContext getCurrentContext() {
WeakReference wr = (WeakReference) localContext.get();
if (wr == null) {
wr = adoptCurrentThread();
} else if(wr.get() == null) {
wr = adoptCurrentThread();
}
return (ThreadContext)wr.get(); // Can still return null. Bug?
}
Does this look like a bug to you? This is no longer a problem for me, since
I don't need to set daemon status anymore. However, if you want, I can file
a JIRA for future reference.
Peter
-----Original Message-----
From: Peter K Chan [mailto:[EMAIL PROTECTED]
Sent: Tuesday, May 22, 2007 9:23 PM
To: [email protected]
Subject: RE: [jruby-dev] Can blocks be safely run in different Java threads?
Thanks, Charles.
When I was stepping through in debug mode, I notice that there is a
RubyThread.adopt method, which seems to adopt a foreign thread.
As I was debugging, I realized that all Ruby threads are daemon (silly me
for not realizing this earlier), so I just took my own block->Runnable
adoption out, which eliminated the problem. However, this used to work in
previous releases of JRuby, so it may be a regression in some way.
Peter
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Charles Oliver Nutter
Sent: Tuesday, May 22, 2007 9:02 PM
To: [email protected]
Subject: Re: [jruby-dev] Can blocks be safely run in different Java threads?
Peter K Chan wrote:
> I am wondering if there is any potential problem with running a Ruby block
> in a different native java.lang.Thread?
>
> My code converts a block into a java.lang.Runnable and starts it directly
> using the Java thread constructor. I am not using RubyThread, because I
need
> to start a daemon thread.
>
> I am seeing some sporadic NPE in my code. About one-third of the time,
when
> starting a block in a different (native) Java thread, I see error below
(1.0
> RC3 revision 3714):
>
> Exception in thread "NET2" java.lang.NullPointerException
> at org.jruby.runtime.Block.call(Block.java:174)
> at org.jruby.RubyProc.call(RubyProc.java:173)
> at org.jruby.RubyProc.call(RubyProc.java:148)
> at org.jruby.javasupport.Java$1.invoke(Java.java:853)
> at $Proxy0.run(Unknown Source)
> at java.lang.Thread.run(Thread.java:595)
>
> Any tip on this would be greatly appreciated.
Hmm...well to start, blocks are not generally supposed to be called
across threads, but procs can be. Procs/lambdas handle the local scoping
and framing a bit differently so that multiple callers don't interfere
with each other. I would recommend that if you're going to memoize the
block and call it across threads, that you first turn it into a proc or
lambda.
- Charlie
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email