Repository navigation
Mark a fiber responded before scheduling it when an external future completes - #709
Conversation
…ompletes EmbeddingSupportTest.closingARunAsItEndsOnItsOwnEndsItOnceAndLeavesNothingBehind hung once in master's CI after #703 (run 39440797959): close() waited forever for the runner's killTask, and the runtime was idle. A fiber that waits with Frame.waitForExternalCompletion, as Container.kill does for the termination of the container's services, is woken by two separate callbacks on the future: waitForExternalCompletion's own ensureScheduled, and the wait frame's onResponse, which marks the fiber ready. When the future completes as a dependent of another one, as Container.kill's terminateServices().thenApply(...) does, CompletableFuture can run them in registration order, ensureScheduled first. The service then ran on a worker, found no ready fiber, and released its lock before onResponse marked the fiber; onResponse does not touch the scheduling lock, so nothing scheduled the service again. The waiting fiber stayed ready in an idle service. Here the Task service's kill() never returned, so TaskRegistry stayed in the critical section of the runner's killTask, and close(), startTask and taskStatus all waited on it. The external completion now marks the fiber responded and then schedules the service, in one callback, as Frame.createWaitIOFrame already does. Module initialization (Utils) waits the same way and gets the same fix. Diagnosis, on a temporary branch that repeats the scenario up to 1000 times on the CI runner and dumps the runtime when close() hangs: it hung at iterations 82, 110, 74 and 11 of four runs, always with the Task fiber in Container.kill's WaitForResult, its future assigned, responded and ready, in an idle service with no scheduling lock held. About 2,100 local iterations on macOS never hung. Validation: with this change the same probe ran 1000 iterations on the CI runner without a hang (run 39459197798), where the four runs without it hung within 110; :javatools:test 456 tests, 0 failures, 42 existing skips; spotlessCheck passes.
|
Interesting catch! No idea how I missed that :/ |
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
There was a problem hiding this comment.
🔵 Needs a closer look
It is a subtle concurrency/scheduling fix in the core interpreter with no deterministic regression test, warranting final human review.
No open issues 🎉
What changed in this PR
This PR fixes a lost wake-up in the XVM interpreter's fiber scheduling. When a fiber waits on an externally completed future via Frame.waitForExternalCompletion (used by Container.kill() and module initialization), two separate whenComplete callbacks were registered on the future: one to schedule the service (ensureScheduled) and a later one to mark the fiber ready (onResponse). Because CompletableFuture may run the scheduling callback first — notably when the future completes as a dependent of another future (terminateServices().thenApply(...)) — the service could run on a worker, find no ready fiber, and go idle with nothing left to reschedule it, hanging operations like close(). The fix combines both actions into a single callback that marks the fiber responded before scheduling the service, mirroring the existing createWaitIOFrame pattern.
Changes:
- Merge the
onResponse()andensureScheduled(true)notifications into one future callback so the fiber is always marked ready before the service is scheduled. - Add an explanatory comment describing the callback-ordering hazard.
| File | Description |
|---|---|
| javatools/src/main/java/org/xvm/runtime/Frame.java | Combines the external-completion future callback to call f_fiber.onResponse() before f_context.ensureScheduled(true), preventing a lost wake-up when callbacks run out of registration order. |
Review effort: Balanced
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
…cleanup and Marketplace prep errs-upstream-issues.md gains a table of the prepared upstream fixes for UP16, UP17, UP20, UP24 and UP29, each with its branch and commit and its native check without and with the change, and the register rows point to it. errs-integration-plan.md records #709's cherry-picks, the commits that remove Hungarian names and declare fields first in the files this branch and #693 add, the Marketplace publishing branch and #710, and the close of LSP4IJ #1744.
Summary
Fixes a lost wake-up in the interpreter's scheduling of a fiber that waits on an externally
completed future. It hung
EmbeddingSupportTest.closingARunAsItEndsOnItsOwnEndsItOnceAndLeavesNothingBehindin master's CI after #703 (run 39440797959, cleared by a rerun).
close()waited forever for therunner's
killTask, and the whole runtime was idle. An IDE user would see it as a Stop that neverfinishes.
Root cause
Frame.waitForExternalCompletion, used byContainer.kill()and by module initialization(
Utils), registers two separate callbacks on the future:cf.whenComplete(... f_context.ensureScheduled(true)), thenwaitandcreateWaitFrame,cf.whenComplete(... f_fiber.onResponse()), which marks thefiber ready.
When the future completes directly,
CompletableFutureruns them last-registered first, so thefiber is marked before the service is scheduled. When the future completes as a dependent of
another future, as
Container.kill'sterminateServices().thenApply(...)does,postCompletemoves the later callbacks to the outer future's stack and runs
ensureScheduledfirst. The servicethen runs on a worker, finds no ready fiber, and releases its scheduling lock. If that happens
before
onResponsemarks the fiber, nothing schedules the service again, becauseonResponsedoesnot touch the scheduling lock. The fiber stays ready in an idle service.
In the failing test, that fiber was the
Taskservice'skill(), soTaskRegistrystayed in thecritical section of
killTask(runner.x), andclose(),startTaskandtaskStatusall waitedon it.
Change
The external completion marks the fiber responded and then schedules the service, in one callback,
as
Frame.createWaitIOFramealready does. Module initialization waits through the same method andgets the same fix.
Diagnosis
A temporary branch repeated the test's scenario up to 1000 times on the CI runner and dumped the
runtime when
close()hung: each service's shutdown state, scheduling lock and fibers. Four runshung, at iterations 82, 110, 74 and 11. Each time, the
Taskfiber was inContainer.kill'sWaitForResult, with its future assigned, responded and ready, in an idle service with schedulinglock 0. About 2,100 local iterations on macOS never hung.
Validation
39459197798).
:javatools:test: 456 tests, 0 failures, 42 existing skips.spotlessCheckpasses.Limits
There is no deterministic regression test. Forcing the interleaving needs the service to drain
between the two callbacks, which only a
Runtimethat runs scheduled work inline can do, andInterpreterConnectorcreates itsRuntimeitself. Adding that seam is more API than this fixwarrants. The existing
EmbeddingSupportTestkeeps exercising the path. I had to do all this (and queue it up) manually.