Skip to content

Fiber#new and current Thread #1806

Open

Description

@robin850

Hello,

There is a tiny problem with JRuby when dealing with a Fiber and a Thread object. For instance, with this code:

thread = Thread.current

Fiber.new {
  thread == Thread.current
}.resume

The fiber will yield false because JRuby spawns a new Thread calling Fiber#new. The fiber will yield true on MRI though.

Looking at the logs, the commit introducing this thread creation (i.e. 0a8680cf) is the initial commit of the RubyFiber.java file so we have no information about this.

This has been tested with JRuby 1.7.13 and current master.

Have a nice day.

Activity

  1. zanker commented on Feb 12, 2015

    @zanker

    I can confirm this still exists on JRuby 1.7.19/9.0.0.0.pre1. It can lead to some fun issues when you try to reuse threads using Thread.current, like statsd-ruby or net-http-persistent and something that uses Fibers like Celluloid/Sidekiq.

  2. chuckremes commented on Feb 3, 2018

    @chuckremes

    This issue is also still causing headaches. See issue 2 in ruby-io for details.

  3. headius commented on Feb 13, 2018

    @headius
    Member

    This has come up many times, and it's not an easy thing to fix. The main issue is that our Fiber subsystem uses normal Ruby synchronization mechanisms (specifically, Queue) which depends on the Fiber and the Thread it's synchronizing against to have their own notions of current thread.

    However, since I wrote this implementation, we have improved our internal logic for synchronizing threads with non-Ruby mechanisms without interfering in Ruby signalling. This can be revisited, but most likely not until 9.2.

  4. added this to the JRuby 9.2.0.0 milestone on Feb 13, 2018
  5. zanker commented on Feb 13, 2018

    @zanker
  6. headius commented on Sep 19, 2018

    @headius
    Member

    FWIW there is now a working coroutine implementation for OpenJDK: Project Loom. Given a bit of time (or help from the community) we could ship pre-release support for it in JRuby any day now. Maybe for 9.2.2?

  7. 24 remaining items

  8. headius commented on Oct 7, 2021

    @headius
    Member

    This didn't make the cut for 9.3, which we wanted to get out the door to work on Ruby 3.0 support in 9.4. Punting to 9.4 to reevaluate.

  9. headius commented on Nov 16, 2022

    @headius
    Member

    The latest PR has been rebased on current 9.4 but it's too drastic a change to make now right before release. It will fit better with the more extensive changes planned for 9.5 (planned for mid-2023), so I have retargeted it.

    It's also worth mentioning that since this PR came out, a number of changes have happened to fibers that makes this change less important, like adding explicitly fiber-local values and other fiber-specific versions of per-thread functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions