Skip to content

[core] retain resource cleanup across acquisition interrupts - #1954

Closed
DamianReeves wants to merge 1 commit into
mainfrom
fix/scope-acquire-handover
Closed

DamianReeves wants to merge 1 commit into
mainfrom
fix/scope-acquire-handover

Conversation

@DamianReeves

Copy link
Copy Markdown
Collaborator

Scope.acquireRelease could produce a resource and then lose its cleanup when the fiber was interrupted before the registration continuation. Fiber.init uses this path, so a child that had already started could survive its parent scope's shutdown.

Register the release in the same step that receives the acquired value, using a private kernel completion observer. Suspended acquisition stays interruptible. If the scope already closed, run the rejected release exactly once on a detached fiber and retain the Closed error. Public effect rows and Scope.ensure rejection behavior remain unchanged.

Investigated after #1949's Linux ARM64 JVM job reported no reclaim events after interrupting a SQL lease's parent. A deterministic core regression interrupts inside the final acquisition step and observes scope teardown: acquisition ran once, release ran zero times. It fails directly in 16 ms before the fix. This proves the core defect independently; the CI log does not establish its exact interleaving.

Shared regressions cover interruption during acquisition, a scope closing before acquisition completes, rejected asynchronous cleanup failure, acquisition failure/panic, exactly-once release, and the original scope creation frame in Closed. The existing closed-scope acquisition regression is enabled.

Validation:

  • Full JVM core suite: 1,756 passed across 42 suites.
  • Full JVM kernel suite: 681 passed; SQL cancellation suite: 21 passed.
  • ScopeTest: 53 passed on JVM, JS, Native, and Wasm.
  • FiberTest: JVM 113, JS 109, Native 111, Wasm 109 passed.
  • PendingTest and RuntimeEffectTest: 61 passed on each of JS, Native, and Wasm.
  • Serial scalafmtCheckAll and git diff --check passed.

Register release when acquisition returns, before interruption can
abandon the continuation. Release late acquisitions on a detached
fiber when their scope has already closed.
fwbrasil added a commit that referenced this pull request Sep 18, 2026
#1954 closes, on the old kernel, the window between an acquire producing its value and the release being
registered, through a completion observer; this branch closes the same window structurally, since
Scope.acquireRelease registers through ensureMap. The PR is superseded by the new kernel, and its five
regression leaves were compared against what this branch already pins.

Taken, because the branch had no leaf for them:
- a typed Abort failure in the acquire registers no release (the branch pinned only the throw);
- the Closed a refused registration raises names the scope's creation frame, folded into the existing
  closed-scope leaf, which also stops accepting a success there: with the gate opening after Scope.run has
  returned, the refusal is the only correct outcome;
- a refused release that suspends and then fails still runs exactly once, and the acquiring fiber's error
  stays the refusal rather than the release's failure.

Not taken: the interrupt-in-the-final-step and suspended-acquisition leaves, already covered by the #1224
and #1820 sections and by ScopeInterruptTest; and the PR's assertion that a Scope.ensure refused by a closed
scope does not run, which contradicts this branch's contract of running a refused registration detached
rather than dropping it.

Verified: ScopeTest green on JVM (76 leaves) and JS (75) in a copy of this tree; the run in this tree is in
progress at commit time.
@fwbrasil

Copy link
Copy Markdown
Collaborator

I'm preparing to open a PR with the new arrow-based kernel and it has major changes in how this code works. I'll close this PR since the new kernel fixes this issue as well.

@fwbrasil fwbrasil closed this Sep 18, 2026
fwbrasil pushed a commit that referenced this pull request Sep 18, 2026
### Problem

The JavaScript WeakReference shim was also compiled for Scala Native,
shadowing its runtime implementation. That broke reference casts and
inherited thread-local copying, including the runtime stack-unwinding
buffers.

### Solution

Restrict the shim to JS and Wasm. Add regressions for clearing
references, replacing cleared registry entries, and child-local
inheritance. Enable Native machine-metric readback tests and remove the
scheduler tests' exception suppression.

### Notes

Validated on this standalone branch: 282 registry tests across JVM,
Native, JS, and Wasm; three machine-metric tests each on JVM and Native;
both README doctests; and serial `scalafmtCheckAll`. The three Native
weak-reference regressions reproduced the original defects before the
fix.

Extracted from #1868 so the filesystem stack can be reviewed
independently.

Rebased onto `6d50eecae` on September 17. Revalidated all 282 registry
tests across JVM, Native, JS, and Wasm, plus six machine-metric tests
across JVM and Native. The previous Linux JVM CI failures are addressed
separately in #1953 and #1954.
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.

2 participants