[PR #7828] fix: prevent CLI hang on invalid config JSON syntax #12522

Closed
opened 2026-02-16 18:17:25 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/7828

State: closed
Merged: No


What does this PR do?

Fixes #7789

Observations:

  • opencode run exits with error since it runs on the main thread
  • opencode runs the TUI on the main thread and spawns worker threads for the core business logic

Alternatives Considered

  • Fix RPC error propagation globally - Add try/catch to Rpc.listen() and introduce a new rpc.error message type. Rejected because it required changes to the RPC protocol and didn't seem like that was in scope
  • Validate config syntax on main thread before spawning worker - Parse config files early in TuiThreadCommand.handler() before the worker starts. Rejected because config loading is tightly coupled to Instance.state() which requires the worker context, and duplicating the file search logic would be heavy.
  • Move config loading to main thread entirely - Load config before spawning the worker. Rejected because the worker architecture exists for a reason (manages multiple instances, server, LSP, file watchers) and config is instance-specific.

Proposed Solution

Update the subscription logic for the TUI to handle the case where the worker returns result.subscribed = false instead of always returning true. If result.subscribed == false, we handle ConfigJsonError and ConfigInvalidError and propagate that error higher up the call stack.

How did you verify your code works?

  • Added a new "unit" test (is it really unit if it spins up the whole application?)
  • Manual testing:
$ opencode --version                                                                                                                                                                                                          1.1.13
$ timeout 5 ./opencode-7789; [ $? -eq 124 ] && echo "Bug: OpenCode hung and was killed after 5s"
Bug: OpenCode hung and was killed after 5s
$ timeout 5 ./opencode-7789; [ $? -eq 124 ] && echo "Bug: OpenCode hung and was killed after 5s" # Locally built binary
Error: Config file at /private/tmp/oc-7789/opencode.json is not valid JSON(C):
--- JSONC Input ---
{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "example" : {
      "type": "local"
      "enabled": true,
      "command" : ["echo", "'Hello World'"]
    },
  }
}

--- Errors ---
CommaExpected at line 6, column 7
   Line 6:       "enabled": true,
                ^
--- End ---
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/7828 **State:** closed **Merged:** No --- ### What does this PR do? Fixes #7789 ### Observations: - `opencode run` exits with error since it runs on the main thread - `opencode` runs the TUI on the main thread and spawns worker threads for the core business logic ### Alternatives Considered - Fix RPC error propagation globally - Add try/catch to `Rpc.listen()` and introduce a new `rpc.error` message type. Rejected because it required changes to the RPC protocol and didn't seem like that was in scope - Validate config syntax on main thread before spawning worker - Parse config files early in `TuiThreadCommand.handler()` before the worker starts. Rejected because config loading is tightly coupled to `Instance.state()` which requires the worker context, and duplicating the file search logic would be heavy. - Move config loading to main thread entirely - Load config before spawning the worker. Rejected because the worker architecture exists for a reason (manages multiple instances, server, LSP, file watchers) and config is instance-specific. ### Proposed Solution Update the subscription logic for the TUI to handle the case where the worker returns ` result.subscribed = false` instead of always returning `true`. If `result.subscribed == false`, we handle `ConfigJsonError` and `ConfigInvalidError` and propagate that error higher up the call stack. ### How did you verify your code works? - Added a new "unit" test (is it really unit if it spins up the whole application?) - Manual testing: ``` $ opencode --version 1.1.13 $ timeout 5 ./opencode-7789; [ $? -eq 124 ] && echo "Bug: OpenCode hung and was killed after 5s" Bug: OpenCode hung and was killed after 5s $ timeout 5 ./opencode-7789; [ $? -eq 124 ] && echo "Bug: OpenCode hung and was killed after 5s" # Locally built binary Error: Config file at /private/tmp/oc-7789/opencode.json is not valid JSON(C): --- JSONC Input --- { "$schema": "https://opencode.ai/config.json", "mcp": { "example" : { "type": "local" "enabled": true, "command" : ["echo", "'Hello World'"] }, } } --- Errors --- CommaExpected at line 6, column 7 Line 6: "enabled": true, ^ --- End --- ```
yindo added the pull-request label 2026-02-16 18:17:25 -05:00
yindo closed this issue 2026-02-16 18:17:25 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12522