fix(run): wait for background/child sessions before exiting#4
Merged
Conversation
config.yml had blank_issues_enabled: false (inherited from upstream opencode). Gitea reads this GitHub-compatible config and hides the 'Open a blank issue' option, so users could only pick from the 3 templates. Flip to true so plain issues are creatable.
In opencode run mode, the event loop only watched the main session's idle status and exited as soon as it went idle. Background agents spawned via delegate_task(run_in_background=true) run in child sessions via prompt_async; when the main session finished the process exited, disposing the instance and cancelling all runners, which killed the still-running background agents. Track every session's busy/idle status in the event loop and gate execute()'s return on a 'done' promise that resolves only once the main session is idle AND no background/child sessions remain busy. Also resolve the promise if the SSE stream closes unexpectedly to avoid hanging.
Plugin background handles (omo-stable's unawaited prompt promises and task-manager timers) keep the Node.js event loop alive after every session is idle, causing the run command to hang indefinitely. Verified at runtime: done resolves correctly when all sessions idle (confirmed via debug instrumentation), execute() returns, but the process hangs on dangling plugin handles. Add an unreffed 2s fallback exit timer after await done. unref() means it never prevents natural exit (no plugin handles -> process exits immediately, timer is moot); with plugin handles it force-exits after 2s. Without background agents behavior is unchanged (process exits naturally before the timer fires).
Contributor
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In
opencode runmode, the event loop only watched the main session's idle status and exited as soon as it went idle. Background agents spawned viadelegate_task(run_in_background=true)run in child sessions; when the main session finished, the process exited, disposing the instance and cancelling all runners — killing the still-running background agents. They could never complete.Root cause
delegate_task(run_in_background=true)creates a child session and starts it asynchronously, returning immediately.run.tsexecute()returned right aftersdk.session.prompt()resolved (main session idle).effectCmdfinallycallsstore.dispose(ctx)→SessionRunStatefinalizer cancels all runners → background child sessions interrupted.Fix
busyset.execute()'s return on adonepromise that resolves only once the main session is idle AND no background/child sessions remain busy.process.exit(0)for plugin handles that keep the event loop alive after all sessions complete.Verification
Runtime-tested with isolated
XDG_DATA_HOME(zero production impact):finish=stop(completes, not killed),doneresolves correctly, clean EXIT=0 ✓