Skip to content

fix: LazyValue no longer raises ThreadError for concurrent fibers under a fiber scheduler - #119

Closed
HolyWalley wants to merge 1 commit into
googleapis:mainfrom
44-pixels:fix/lazy-value-fiber-scheduler
Closed

HolyWalley wants to merge 1 commit into
googleapis:mainfrom
44-pixels:fix/lazy-value-fiber-scheduler

Conversation

@HolyWalley

Copy link
Copy Markdown

LazyValue identifies the caller that is computing by Thread.current. Under a fiber scheduler (Falcon, Async), every fiber on a thread shares one Thread.current. So when a second fiber calls #get while another fiber's computation is waiting on I/O, wait_compute treats it as re-entry and raises:

ThreadError: deadlock: tried to call LazyValue#get from its own computation

Nothing is actually deadlocked. The second fiber could simply wait: since Ruby 3.0, Mutex and ConditionVariable block per fiber under a scheduler.

We hit this in production on GKE with Falcon. Parallel requests that sign GCS URLs share one metadata-server LazyValue (ComputeMetadata, via expiring_value). One request computes and the others fail with the error above.

Fix: a private current_owner helper replaces the four uses of Thread.current (enter_compute, wait_compute, handle_success, handle_failure). It returns Fiber.current while a fiber scheduler is set, and Thread.current otherwise.

  • Without a scheduler, nothing changes. That includes re-entry through a hand-driven fiber (e.g. the computation calls Enumerator#next whose body calls #get): it still raises ThreadError. A plain Fiber.current would hang there, because with no scheduler the waiting fiber blocks its thread.
  • Under a scheduler, other fibers wait for the computation and share its result, as threads already do. A fiber re-entering its own computation still raises ThreadError.

Tests: two new tests use a minimal Fiber::Scheduler defined in the test file, so no new dependency is needed:

  • three fibers share one computation
  • a fiber re-entering its own computation still raises

Without the fix, the first test fails with the ThreadError above. toys test and toys rubocop pass on Ruby 3.2, 3.3, 3.4 and 4.0. The exception is ComputeSMBIOS "gets data on Linux", which also fails on unchanged main in a Docker container with no DMI data.

🤖 Generated with Claude Code

@HolyWalley
HolyWalley requested a review from a team as a code owner October 28, 2026 13:20
@google-cla

google-cla Bot commented Oct 28, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

…er a fiber scheduler

LazyValue identified the computing caller by Thread.current. Under a fiber
scheduler (Falcon, Async) every fiber on a thread shares it, so a second fiber
calling #get during another fiber's computation was taken for re-entry and
raised "deadlock: tried to call LazyValue#get from its own computation".
Under a scheduler the caller is now the fiber; without one it stays the
thread, so thread-only code behaves as before.
@HolyWalley
HolyWalley force-pushed the fix/lazy-value-fiber-scheduler branch from 2c5bcd0 to 24dc268 Compare October 28, 2026 13:29
@HolyWalley

Copy link
Copy Markdown
Author

Superseded by #120: the same commit, opened from my personal fork instead of the company one.

@HolyWalley HolyWalley closed this Oct 28, 2026
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.

1 participant