Skip to content

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

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

HolyWalley wants to merge 1 commit into
googleapis:mainfrom
HolyWalley: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

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