subprocess.Popen hang issues on "opencode run message --json format"? #8391

Open
opened 2026-02-16 18:09:50 -05:00 by yindo · 5 comments
Owner

Originally created by @davidbernat on GitHub (Feb 2, 2026).

Originally assigned to: @rekram1-node on GitHub.

Question

We are simply trying to launch opencode in the background via Python subprocess.Popen, and the execution hands indefinitely at the process.readline() moment. For clarity, this is the standard, very common, way to launch a subprocess (or opencode turn) into the command line from within Python, and yet there are a number of well-known peculiarities in Python between the process.readline() hanging indefinitely when new lines and empty byte strings are not encountered, potentially when the underlying command line process does something peculiar with its handling of newlines etc. I searched the opencode GitHub issues and found only one mention of subprocess.Popen (which really surprises me given how procedurally common this is a requirement to run from Python) so I figured it might be helpful for everyone others to debug this issue here.

This command works as intended when run directly from the command line in about 10 seconds, returning its first json formatted line (type=step_start) within about 7 seconds. All pretty common and in line with TUI expectations.

The use of --format json does not impact the situation, nor does simpler prompts, nor do prompts without files.

The replacement of cmd with trivial or simple cmds (e.g. `cmd=["echo", ""hello""] all work as expected with no issues.

I have searched my code repositories for how I have approached this the few times I remember this being an issue, and most of my implementations elsewhere are more or less identical, so I do not know what the issue is.

Do you?

cmd = ["opencode", "run", "\"Please complete the task using @test_TASK.md @test_PROJECT.md\"" "-f" "test_TASK.md" "-f" "test_PROJECT.md" "--format" "json"]

env = os.environ.copy()
proc = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while self.proc.poll() is None:
  # stdout, stderr = self.proc.communicate()  # tried this too
    for stream, is_error in [(self.proc.stdout, False), (self.proc.stderr, True)]:
        if stream and not stream.closed:
            try:
                line = stream.readline()  # <== HANGS HERE INDEFINITELY ON FIRST READ
                if line:
                    line = line.decode("utf-8", errors="replace").rstrip()
            except: pass
🔧 Executing command:
   opencode run "Please complete the task using @test_TASK.md @test_PROJECT.md" -f test_TASK.md -f test_PROJECT.md --format json
📝 Process 19836 started at 23:28:12
❤️ Heartbeat monitoring active (every 5 seconds)
️❤️ Heartbeat now=23:28:12    +0m 0s pid=19836 state=running_waiting
️❤️ Heartbeat now=23:28:17    +0m 5s pid=19836 state=running_waiting
️❤️ Heartbeat now=23:28:22    +0m10s pid=19836 state=running_waiting
️❤️ Heartbeat now=23:28:27    +0m15s pid=19836 state=running_waiting
️❤️ Heartbeat now=23:28:32    +0m20s pid=19836 state=running_waiting
️❤️ Heartbeat now=23:28:37    +0m25s pid=19836 state=running_waiting
️❤️ Heartbeat now=23:28:42    +0m30s pid=19836 state=running_waiting
️❤️ Heartbeat now=23:28:47    +0m35s pid=19836 state=running_waiting
️❤️ Heartbeat now=23:28:52    +0m40s pid=19836 state=running_waiting
Originally created by @davidbernat on GitHub (Feb 2, 2026). Originally assigned to: @rekram1-node on GitHub. ### Question We are simply trying to launch opencode in the background via Python subprocess.Popen, and the execution hands indefinitely at the process.readline() moment. For clarity, this is the standard, very common, way to launch a subprocess (or opencode turn) into the command line from within Python, and yet there are a number of well-known peculiarities in Python between the process.readline() hanging indefinitely when new lines and empty byte strings are not encountered, potentially when the underlying command line process does something peculiar with its handling of newlines etc. I searched the opencode GitHub issues and found only one mention of `subprocess.Popen` (which really surprises me given how procedurally common this is a requirement to run from Python) so I figured it might be helpful for everyone others to debug this issue here. This command works as intended when run directly from the command line in about 10 seconds, returning its first json formatted line (type=step_start) within about 7 seconds. All pretty common and in line with TUI expectations. The use of `--format json` does not impact the situation, nor does simpler prompts, nor do prompts without files. The replacement of cmd with trivial or simple cmds (e.g. `cmd=["echo", "\"hello\""] all work as expected with no issues. I have searched my code repositories for how I have approached this the few times I remember this being an issue, and most of my implementations elsewhere are more or less identical, so I do not know what the issue is. Do you? ```python cmd = ["opencode", "run", "\"Please complete the task using @test_TASK.md @test_PROJECT.md\"" "-f" "test_TASK.md" "-f" "test_PROJECT.md" "--format" "json"] env = os.environ.copy() proc = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE) while self.proc.poll() is None: # stdout, stderr = self.proc.communicate() # tried this too for stream, is_error in [(self.proc.stdout, False), (self.proc.stderr, True)]: if stream and not stream.closed: try: line = stream.readline() # <== HANGS HERE INDEFINITELY ON FIRST READ if line: line = line.decode("utf-8", errors="replace").rstrip() except: pass ``` ``` 🔧 Executing command: opencode run "Please complete the task using @test_TASK.md @test_PROJECT.md" -f test_TASK.md -f test_PROJECT.md --format json 📝 Process 19836 started at 23:28:12 ❤️ Heartbeat monitoring active (every 5 seconds) ️❤️ Heartbeat now=23:28:12 +0m 0s pid=19836 state=running_waiting ️❤️ Heartbeat now=23:28:17 +0m 5s pid=19836 state=running_waiting ️❤️ Heartbeat now=23:28:22 +0m10s pid=19836 state=running_waiting ️❤️ Heartbeat now=23:28:27 +0m15s pid=19836 state=running_waiting ️❤️ Heartbeat now=23:28:32 +0m20s pid=19836 state=running_waiting ️❤️ Heartbeat now=23:28:37 +0m25s pid=19836 state=running_waiting ️❤️ Heartbeat now=23:28:42 +0m30s pid=19836 state=running_waiting ️❤️ Heartbeat now=23:28:47 +0m35s pid=19836 state=running_waiting ️❤️ Heartbeat now=23:28:52 +0m40s pid=19836 state=running_waiting ```
Author
Owner

@github-actions[bot] commented on GitHub (Feb 2, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #8203: opencode run hangs forever on API errors (breaks CLI/automation integrations) - directly addresses subprocess automation hanging
  • #8434: Local MCPs fail to start - stdin closes immediately after spawn - related subprocess communication and stdio handling
  • #1717: TUI exits but process hangs (no shell return) when opencode.json is present - related process hanging and subprocess cleanup issues
  • #11836: Embedded bun runtime hangs indefinitely on bun add during bootstrap on Linux - similar subprocess hanging indefinitely issues

Feel free to ignore if your specific case involves different circumstances or error conditions.

@github-actions[bot] commented on GitHub (Feb 2, 2026): This issue might be a duplicate of existing issues. Please check: - #8203: opencode run hangs forever on API errors (breaks CLI/automation integrations) - directly addresses subprocess automation hanging - #8434: Local MCPs fail to start - stdin closes immediately after spawn - related subprocess communication and stdio handling - #1717: TUI exits but process hangs (no shell return) when opencode.json is present - related process hanging and subprocess cleanup issues - #11836: Embedded bun runtime hangs indefinitely on bun add during bootstrap on Linux - similar subprocess hanging indefinitely issues Feel free to ignore if your specific case involves different circumstances or error conditions.
Author
Owner

@justindra commented on GitHub (Feb 3, 2026):

This could potentially be related to a fix in #11895 - if the model was attempting to use the question tool during opencode run, it would hang indefinitely waiting for a response that would never come (since there's no interactive TUI to answer questions).

The fix ensures that tools denied by session permissions (like the question tool in non-interactive mode) are properly filtered out before being sent to the model, preventing the model from ever calling them.

@justindra commented on GitHub (Feb 3, 2026): This could potentially be related to a fix in #11895 - if the model was attempting to use the question tool during `opencode run`, it would hang indefinitely waiting for a response that would never come (since there's no interactive TUI to answer questions). The fix ensures that tools denied by session permissions (like the question tool in non-interactive mode) are properly filtered out before being sent to the model, preventing the model from ever calling them.
Author
Owner

@davidbernat commented on GitHub (Feb 3, 2026):

Thanks @justindra, definitely related, but I do not think that will resolve the issue.

  1. running manually CLI the prompt executed correctly a dozens times, and never by prompting a question.
  2. the issue only occurs when running the same CLI execution from subprocess.Popen

Notes:

  1. I was using opencode v1.1.48 last night and upgraded to v1.1.49, which is the version with the question bug.
  2. Using v1.1.49 does not result in question hanging and works properly, but not from subprocess.Popen

Let me know what to debug. Nothing jumps out at me immediately.
This is a blocker. I have other housekeeping I can work on.

@davidbernat commented on GitHub (Feb 3, 2026): Thanks @justindra, definitely related, but I do not think that will resolve the issue. 1. running manually CLI the prompt executed correctly a dozens times, and never by prompting a question. 2. the issue only occurs when running the same CLI execution from subprocess.Popen Notes: 1. I was using opencode v1.1.48 last night and upgraded to v1.1.49, which is the version with the question bug. 2. Using v1.1.49 does not result in question hanging and works properly, but not from subprocess.Popen Let me know what to debug. Nothing jumps out at me immediately. This is a blocker. I have other housekeeping I can work on.
Author
Owner

@dogmatic69 commented on GitHub (Feb 5, 2026):

having the same issue trying to run from python in 1.1.52. I added "permission": "allow" to the config and then seems to work, so it's 100% getting stuck on a question.

@dogmatic69 commented on GitHub (Feb 5, 2026): having the same issue trying to run from python in 1.1.52. I added `"permission": "allow"` to the config and then seems to work, so it's 100% getting stuck on a question.
Author
Owner

@davidbernat commented on GitHub (Feb 12, 2026):

@rekram1-node @dogmatic69 again, perhaps I am the For Dummies author of our future.

  1. Why on earth would I permission a closed UI system to blanket permission=allow?
  2. Can we find a diagnostic to confirm opencode serve and opencode run hang "for questions"?
  3. I appreciate @dogmatic69 chiming in but can we get a member of the company to chime in? (Love IKEA, btw.)
  4. The commands run in Terminal perfectly as expected. Only through subprocess.Popen and REST API do we see this.
  5. Nothing in Terminal successes suggests the system is getting hung up on AskUserQuestion.
  6. Using the CLI tool from Terminal works as expected, repeatedly, with no other indications.
  7. I have very, very thoroughly built out our subprocess.Popen infrastructure this week waiting for an answer.

We are being very helpful. We are really disappointed in OpenCode.
I cannot think of a single post that was answered responsibly.

Here is a simple one: opencode/big-pickle seems to have started underperforming, did a backend change occur?
We are very serious practitioners here with a tremendous amount of sympathy to your investors over the years.

@davidbernat commented on GitHub (Feb 12, 2026): @rekram1-node @dogmatic69 again, perhaps I am the For Dummies author of our future. 1. Why on earth would I permission a closed UI system to blanket `permission=allow`? 2. Can we find a diagnostic to confirm `opencode serve` and `opencode run` hang "for questions"? 3. I appreciate @dogmatic69 chiming in but can we get a member of the company to chime in? (Love IKEA, btw.) 4. The commands run in Terminal perfectly as expected. Only through subprocess.Popen and REST API do we see this. 5. Nothing in Terminal successes suggests the system is getting hung up on AskUserQuestion. 6. Using the CLI tool from Terminal works as expected, repeatedly, with no other indications. 7. I have very, very thoroughly built out our subprocess.Popen infrastructure this week waiting for an answer. We are being very helpful. We are really disappointed in OpenCode. I cannot think of a single post that was answered responsibly. Here is a simple one: opencode/big-pickle seems to have started underperforming, did a backend change occur? We are very serious practitioners here with a tremendous amount of sympathy to your investors over the years.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#8391