Skip to content

[feat] support clearing of thread local state#8369

Merged
kares merged 2 commits intojruby:masterfrom
kares:thread-clear-locals
Oct 30, 2024
Merged

[feat] support clearing of thread local state#8369
kares merged 2 commits intojruby:masterfrom
kares:thread-clear-locals

Conversation

@kares
Copy link
Member

@kares kares commented Oct 10, 2024

this is to support cases where e.g. a Java thread pool is running Ruby code.

if the ruby code uses any fiber/thread local state e.g. Thread.current[:foo] = :bar than there isn't an easy way to cleanup accumulated state in RubyThread's locals (and due the way foreign Java threads are adopted the state will stick around for when the Java thread is re-cycled).

@kares kares force-pushed the thread-clear-locals branch from c8cf998 to 8c7ebca Compare October 14, 2024 08:07
return RubyString.newString(runtime, rubyName);
}

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would be interested in seeing this fall into org.jruby.api somewhere. I have been trying to get all methods people would use in native extensions into this package space. I have also been dogfooding our internals to use these same APIs when it makes sense.

org.jruby.api.Runtime#currentContext(Ruby) or something like that? Not sure on either the class name or the method name but just putting that out there.

The other broad point is to kill off any external consumers from hitting Ruby methods at all. I would love to never see us use Ruby as a reference anywhere some day but I think needing to get current ThreadContext will be with us for a long time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to add here that 9.4 has none of the new .api packages but the plan is to backport those files so we have a migration path for 9.4 and possibly 9.3 for native extensions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to never see us use Ruby as a reference anywhere some day

it's a multi-tenant runtime so I am not sure how that would end up being achieved...

what I can tell, as I've been on multiple bigger projects consuming and mixing Ruby and Java is that the runtime is usually something that is kept externally or queried frequently Ruby.getGlobalRuntime (even if it's a single runtime use-case).

seeing this fall into org.jruby.api somewhere

would be nice, but it's a big chunk IMHO, the only way to cleanup the distinction would be to have interfaces on objects that Ruby users from Java encounter e.g. RubyArray implements IRubyArray, RubyHash, RubyThread etc. having just the one IRubyThread added here, with a static method, doesn't get us far...

I can remove the extra current(final Ruby runtime) in this PR and can continue consuming current(IRubyObject recv), it's mostly for convenience

Copy link
Member

@enebo enebo Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kares yeah for embedding with multiple runtimes you have to use runtime. I meant for internal use and java native extension gems. Even then I know we will still need something to get TC from runtime.

Also I am only talking about this single method current so we don't have another way of getting TC. If you just move this to a new class with a static method: org.jruby.api.VM.threadContext(Ruby runtime) then I think it is fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reverted the change for now (the work-around with RubyThread.curren(IRubyObject) will do, it's likely a minor use-case anyways).
I think I get what you mean but I believe it deserves a proper API design, just moving a static method somewhere while still returning RubyThread sounds a bit half-baked and I can not commit to the full thingy atm.

@kares kares added this to the JRuby 9.4.9.0 milestone Oct 19, 2024
@kares
Copy link
Member Author

kares commented Oct 19, 2024

marked as 9.4.9.0 would be nice to have it but feel free to post-pone...

@headius
Copy link
Member

headius commented Oct 30, 2024

I'm +1 to merge.

@kares kares merged commit cbcc1e6 into jruby:master Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants