Skip to content

fix(start-sdk): release subcontainer hold when a oneshot exits cleanly - #4848

Open
only21mil wants to merge 2 commits into
Start9Labs:masterfrom
only21mil:fix/oneshot-releases-subcontainer-hold
Open

only21mil wants to merge 2 commits into
Start9Labs:masterfrom
only21mil:fix/oneshot-releases-subcontainer-hold

Conversation

@only21mil

@only21mil only21mil commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Daemon.start() takes a SubContainer hold (Daemon.ts:131) that runLoop() never releases on clean oneshot exit (Daemon.ts:174). Shared-volumes PR #4485 (34e0813) already resolves the Daemons.dynamic symptom on master by restarting shared peers together; Daemon.ts still leaks the hold for direct callers. Fixes #4632.

Change

  • Daemon.ts: extract releaseHold() for stop() and clean oneshot exit (success && this.oneshot) with a post-await signal.aborted guard; failed retries and non-oneshot daemons keep holds; clean exit does not call destroy().
  • DaemonLifecycle.test.ts: 5 regression tests + 1 non-oneshot guard; patch changelog fragment.

Verification

AI agents ran every check below.

Setup. Linux x86_64, Node v24.22.3, compiled @start9labs/start-sdk dist/.
Steps. Spawn child-process Oneshot on shared SubContainerEager → exit 0 → start Daemon → daemon.term() → oneshot.term().

Before (e914710):

0. Loaded compiled SDK classes: CommandController=function Daemon=function Oneshot=function SubContainerEager=function
1. Starting oneshot (real child process via CommandController command branch) on shared SubContainerEager
   childProcess pid=1579140 exitCode=0 signal=null
   oneshotExited=true holdCount=1 destroyFsCalls=0 destroyed=false
2. Starting sharing daemon on the same SubContainerEager
   holdCount=2 destroyFsCalls=0 destroyed=false
3. Calling daemon.term() (releases daemon hold + calls sub.destroy())
   holdCount=1 destroyFsCalls=0 destroyed=false
4. Calling oneshot.term()
   holdCount=0 destroyFsCalls=1 destroyed=true

After (c428aab):

0. Loaded compiled SDK classes: CommandController=function Daemon=function Oneshot=function SubContainerEager=function
1. Starting oneshot (real child process via CommandController command branch) on shared SubContainerEager
   childProcess pid=1721078 exitCode=0 signal=null
   oneshotExited=true holdCount=0 destroyFsCalls=0 destroyed=false
2. Starting sharing daemon on the same SubContainerEager
   holdCount=1 destroyFsCalls=0 destroyed=false
3. Calling daemon.term() (releases daemon hold + calls sub.destroy())
   holdCount=0 destroyFsCalls=1 destroyed=true
4. Calling oneshot.term()
   holdCount=0 destroyFsCalls=1 destroyed=true

Negative control (e914710): 5 failed, 30 passed. Candidate c428aab: SDK checks, format-check, check-version, merge-tree passed.

Changed files, negative control, and candidate check details

Changed files:

  • projects/start-sdk/lib/mainFn/Daemon.ts
  • projects/start-sdk/lib/test/DaemonLifecycle.test.ts
  • projects/start-sdk/changelog/patch-fixed-oneshot-subcontainer-hold.md

Negative control (e914710 Daemon.ts + new DaemonLifecycle.test.ts):

FAIL lib/test/DaemonLifecycle.test.ts
  Oneshot subcontainer hold lifecycle
    ✕ releases its hold on clean exit without destroying the subcontainer, allowing immediate teardown
    ✕ triggers pending subcontainer destruction when a oneshot exits cleanly
    ✕ re-takes and re-releases the hold across stop/start and destroys once on term()
    ✕ waits for pending async hold release and suppresses onExit on concurrent stop()
    with fake timers
      ✕ retains its hold across failing retries until a clean exit
      ✓ retains a non-oneshot daemon hold on clean exit across restarts until stopped

Test Suites: 1 failed, 1 total
Tests:       5 failed, 30 passed, 35 total

Candidate c428aab:

  • make -C projects/start-sdk check (tsc --noEmit): exit 0.
  • make -C projects/start-sdk bundle: exit 0.
  • npx jest --detectOpenHandles lib/test/DaemonLifecycle.test.ts: 35/35 passed, 0 open handles (exit 0).
  • make start-sdk-test: @start9labs/start-core (TS) 39 suites / 542 tests; start-sdk 63/63 script tests + 32 Jest suites / 422 tests; test-rpc-docs.cjs 2/2 (exit 0).
  • make -C projects/start-sdk check-fmt and make format-check (rustfmt, prettier, taplo): exit 0.
  • node scripts/changelog.mjs check-version projects/start-sdk: exit 0 (5.0.0).
  • git merge-tree --write-tree origin/master HEAD vs e914710: clean tree cb15384c471effe5078b395bd2b1d21d9412dbd3 (exit 0).

Links

Not shown

  • Live StartOS start-cli package attach in LXC (verified on compiled SDK in Node).
  • start-core-test, start-os-scripts-test (N/A; start-sdk only); PR CI (test.yaml, post-open).

model: gemini-4-neon

Generated with Gemini 4 Neon in T3 Code Nightly

review effort: 'xhigh'

Release a oneshot's SubContainer hold in Daemon.runLoop() once the process
exits cleanly, instead of holding the container alive until stop() or term()
runs.

releaseHold() clears releaseSubcontainerHold before awaiting the release
callback and is shared by both runLoop() and stop(). On a concurrent stop(),
stop() clears onExitFns and awaits loop.done (waiting for any in-flight hold
release to complete); checking signal.aborted right after await
this.releaseHold() keeps exitedSuccess false and prevents any onExit callback
registered after an un-awaited stop() from firing.

Fixes Start9Labs#4632
…hot-releases-subcontainer-hold

# Conflicts:
#	projects/start-sdk/lib/test/DaemonLifecycle.test.ts
@MattDHill

Copy link
Copy Markdown
Member

Reviewed, looks good to merge.

@helix-white helix-white left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved on code review at bc3cd5521872dcca19f926015ac23019c44d50d9. No correctness findings; git merge-tree confirms a clean merge with current master.

A successful oneshot releases its subcontainer hold without requesting destruction. This fixes direct callers that share a subcontainer with another daemon; the reconciler already restarts shared peers together. Failed retries and ordinary daemons retain their holds. The existing stop-path release operation is shared rather than duplicated, and stopping during an asynchronous release still drains the loop and suppresses exit callbacks.

As Matt noted, success callbacks now wait for any destruction triggered by the final hold release. In that pending-destruction case, dependent readiness waits for filesystem teardown. This is not a blocker to the fix.

Implementation diff: +15 / −9 lines in 1 file (excludes lockfiles, tests, docs, release notes, and changelog).

Verification evidence: The author records passing full SDK/core TypeScript suites, typecheck, bundle, formatting, and five failing negative controls on Linux x86_64 with Node 24.22.3 at c428aabf6; the daemon implementation is unchanged in the merge head. Matt additionally reports 36/36 lifecycle tests passing. I did not run builds or tests. Live StartOS/LXC execution was not covered.

CI still needs workflow approval: Current-head Automated Tests and Build Products runs report action_required; the successful metadata check does not establish test success. The open #4853 also edits the lifecycle tests, so the second merge may require conflict resolution.

Helix-Harness: pi
Helix-Model: openai-codex/gpt-6.1-sol

@helix-white

Copy link
Copy Markdown
Member

@dr-bonez A completed oneshot must remain restartable when its dependency recovers. I traced that path again; releasing the hold does not itself destroy the subcontainer:

  • Dependency loss calls Daemon.stop(), not term() (HealthDaemon.ts:91–96). Neither this pause nor the dependency's automatic command restart requests subcontainer destruction.
  • A zero hold count triggers teardown only if destroy() was explicitly requested (SubContainer.ts:711–736). The daemon retains the subcontainer object, and construction detaches it from context cleanup.
  • Recovery calls Daemon.start(), which takes a fresh hold before starting the command (Daemon.ts:127–136). It therefore reuses the live subcontainer. The restartable pause/resume contract is already documented on Daemon.stop().

The PR's re-takes and re-releases the hold across stop/start regression covers two successful runs on the same subcontainer, separately from the existing HealthDaemon test that verifies dependency loss uses stop rather than term. I have not executed tests as reviewer.

An explicit teardown of a sharing consumer is different: it can request destruction, and this PR allows that pending request to complete after the oneshot exits. Reusing that destroyed handle is not supported; the dynamic reconciler replaces all sharing peers together with fresh handles. Ordinary dependency crashes/exits do not take that teardown path, so I do not find a rerun regression there.

@helix-white

Copy link
Copy Markdown
Member

@dr-bonez You are right that GC cleanup is another destruction path: SubContainerEager.onDrop() calls _destroyImmediate() when there are no holds, even without a prior destroy() request. My earlier statement about destruction requiring an explicit request was too broad.

However, the last hold is not the last JavaScript reference. The live daemon chain retains HealthDaemon, which retains its daemon, whose readonly subcontainer remains a strong reference after clean exit. The dynamic reconciler likewise retains that ownership chain in its running-entry map. Releasing the hold clears the release callback, not the subcontainer reference. Therefore the subcontainer cannot become GC-unreachable while that oneshot remains owned for a dependency-triggered rerun.

GC cleanup applies after the owning objects become unreachable, not merely when the hold count reaches zero. The dependency pause/resume path keeps those objects reachable. This conclusion is from the reference graph and finalizer code, not from a GC test run.

This branch has not been deployed

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

Projects

None yet

3 participants