Skip to content

fix(run): exit with non-zero code on session error#15787

Closed
zerone0x wants to merge 1 commit intoanomalyco:devfrom
zerone0x:fix/15558-exit-code-on-session-error
Closed

fix(run): exit with non-zero code on session error#15787
zerone0x wants to merge 1 commit intoanomalyco:devfrom
zerone0x:fix/15558-exit-code-on-session-error

Conversation

@zerone0x
Copy link
Copy Markdown
Contributor

@zerone0x zerone0x commented Mar 3, 2026

Problem

When opencode run encounters a session error (e.g., an invalid model specified via --model), the process exits with code 0 instead of 1.

$ opencode run --model "invalid-model-123" "test" 2>/dev/null; echo "EXIT: $?"
EXIT: 0  # Should be 1

Root Cause

loop() was fired with .catch() (not awaited), so after sdk.session.prompt() returned, execute() completed immediately without waiting for the event loop to finish. Even when the loop ran and set the error variable from a session.error event, process.exit(1) was never called because error was scoped inside the unawaited promise.

Fix

  • Store the loop() promise as loopDone instead of calling .catch() immediately
  • Await loopDone after the prompt/command is submitted (the loop terminates naturally when the session reaches idle state)
  • Call process.exit(1) if the error variable was set during the session

This preserves the existing behavior of printing errors to stderr and calling process.exit(1) on unexpected loop exceptions.

Fixes #15558

When opencode run encounters a session.error event (e.g., invalid model
specified via --model), the process exits with code 0 instead of 1.

Root cause: loop() was fired and forgotten (.catch only), so after
sdk.session.prompt() returned, execute() exited immediately without
waiting for the event loop. Even when loop() ran and set the `error`
variable, process.exit(1) was never called.

Fix:
- Store the loop() promise instead of .catch()-ing it immediately
- Await loopDone after the prompt/command call
- Call process.exit(1) if error was set during the session

Fixes anomalyco#15558
@github-actions github-actions bot added contributor needs:compliance This means the issue will auto-close after 2 hours. labels Mar 3, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 3, 2026

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

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.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 3, 2026

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: exit code when an invalid model is passed

1 participant