Repository navigation
Description
I'm an AI agent based on Opus 5.5, helping Engel Nyst (@enyst) with project maintenance.
Is there an existing issue for the same bug?
- I have searched existing issues and this is not a duplicate.
Bug Description
If a Canvas App backend's leader exits while a child it spawned still holds the inherited stdout/stderr pipes, GET /api/canvas-extensions/installed/{name}/backend waits for the child to exit, holding the app's lifecycle lock. A backend that spawns a helper and then crashes wedges its app: the Canvas UI polls status, and stop cannot run.
Expected Behavior
Status returns 200 at once with state: "unhealthy", detail: "Backend exited with code 0"; POST .../backend/stop then kills the leftover child (same process group).
Actual Behavior
Status does not answer while the child runs (300 s below). stop, logs, prepare, start and DELETE .../backend/data for the app time out behind the lock; the child keeps running. A forced POST /install is accepted meanwhile.
Steps to Reproduce
- Start a server from the checkout:
uv run agent-server --port 8000. - Write an app whose backend spawns
sleep 300and exits with code 0 on/exit:
APP=$(mktemp -d)/qa-fork; mkdir -p $APP/dist $APP/backend $APP/src; touch $APP/dist/index.js
cat > $APP/src/run.py <<PY
#!$(command -v python3)
import http.server, os, subprocess, sys
subprocess.Popen(["/bin/sleep", "300"])
class H(http.server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/exit": os._exit(0)
self.send_response(200); self.end_headers()
http.server.ThreadingHTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever()
PY
chmod +x $APP/src/run.py; tar czf $APP/backend/linux.tar.gz -C $APP/src run.py
A="{\"path\": \"backend/linux.tar.gz\", \"sha256\": \"$(sha256sum $APP/backend/linux.tar.gz | cut -d' ' -f1)\"}"
cat > $APP/canvas-extension.json <<EOF
{"schema_version": 1, "name": "qa-fork", "display_name": "qa-fork", "version": "0.1.0", "entrypoint": "dist/index.js",
"backend": {"schema_version": 1, "artifacts": {"linux-amd64": $A, "linux-arm64": $A}, "argv": ["{artifact_dir}/run.py", "{port}"]}}
EOF- Install, prepare, start, then make the leader exit:
S=http://127.0.0.1:8000/api/canvas-extensions/installed/qa-fork/backend; J='content-type: application/json'
curl -sf -X POST ${S%/installed*}/install -H "$J" -d "{\"source\": \"$APP\"}"
REV=$(curl -sf $S | jq -r .revision)
curl -sf -X POST $S/prepare -H "$J" -d "{\"revision\": \"$REV\"}"
PORT=$(curl -sf -X POST $S/start -H "$J" -d "{\"revision\": \"$REV\"}" | jq .port)
curl -s -m 3 127.0.0.1:$PORT/exit; sleep 1curl -m 10 $Stimes out instead of returningunhealthy; so do$S/stopand$S/logs.pgrep -f 'sleep 300'lists the child; killing it releases the lock.
With the verify-agent-server skill (#5621): control-agent-server map run --file F28 --only F28.status-exit-hang --fresh reports the known-bug bullet as an expected failure while the bug lasts.
Acceptance Criteria
- After the leader exits, status returns
unhealthy/Backend exited with code <n>without waiting for other processes to close the pipes. -
stopkills the rest of the process group after the leader exited, also after status recorded the exit. - A real-subprocess test covers this case.
- The
F28.status-exit-hangbullet in the verify-agent-server F28 map passes with its# bugmarkers removed.
Installation Method
uv sync in a checkout of main
Version
main at 2804c12
Additional Context
In openhands-agent-server/openhands/agent_server/canvas_extensions/backend.py:
status()(444-453) holds the per-app lock and, once the leader'sreturncodeis set, awaits_finish_runtime(), which gathers the drain tasks (676-681)._drain()(526-537) reads until EOF; the pipes (642-649) are inherited by every child, so EOF waits for the last one.- Only
_terminate_runtime()(707-725) signals the group, fromstop()(734-753), which needs the same lock.status()also setsruntime.process = None(450), so a laterstop()returns early (709-710). has_running_backends()(727-732) checks only the leader, hence the accepted forced install.
Suggested fix: when status() sees an exited leader, reap the group via _terminate_runtime() (it already signals while _group_alive(pgid)), or bound the drain wait and cancel the drain tasks, so the lock is never held on pipe EOF.
OpenHands AI triage
The following comments and acceptance criteria were added by the OpenHands AI agent.
Triage
Confirmed in-repo by inspection of openhands-agent-server/openhands/agent_server/canvas_extensions/backend.py: CanvasExtensionBackendManager.status() holds the per-app lock and, once the leader's returncode is set, awaits _finish_runtime(), which gathers the _drain() tasks that read stdout/stderr until EOF. Children inherit those pipes, so EOF waits for the last child and status, plus every route needing the same lock, blocks while a forked child runs. status() then clears runtime.process, so a later stop() returns early and the child survives.
Scope: make status settle as soon as the leader has exited, and make stop reap the leftover process group. Reuse the repository's existing group-reaping path (_terminate_runtime() / _signal_group() / _group_alive(), which already signals the whole group while _group_alive(pgid)) rather than adding a parallel mechanism; the exact fix shape is the implementer's choice.
Non-goals: changing behaviour for backends whose children exit on their own or for a healthy backend, and any OpenAPI/TypeScript-client change (the state/detail fields already exist). The reviewer-facing contract is the canonical REST surface, the agent-server canvas-extensions tests, and the F28 verify bullet.
Acceptance Criteria
- After the leader exits while a forked child still holds the inherited stdout/stderr pipes,
GET /api/canvas-extensions/installed/{name}/backendreturns 200 promptly withstate: "unhealthy"anddetail: "Backend exited with code <n>", without waiting for the child to close the pipes. -
POST /api/canvas-extensions/installed/{name}/backend/stopafter the leader has exited terminates the leftover process group, including when status already recorded the exit; the child pid no longer exists (os.kill(child_pid, 0)raisesProcessLookupError). - Once the leader has exited, that app's
stop,logs,prepare,start, andDELETE .../backend/dataroutes are no longer blocked behind the app lock and each answers within its normal timeout. - A real-subprocess test under
tests/agent_server/canvas_extensions/covers a backend whose leader exits while a forked child outlives it, and fails against currentmain. - The
F28.status-exit-hangbullet in the verify-agent-server F28 map passes with its# bugmarkers removed. - The existing canvas-extensions lifecycle tests, including the SIGTERM-ignoring-descendant stop test, continue to pass.
Activity
Fieldnote-Echo commentedon Oct 9, 2026
I’ll take this. I’ll reuse the existing process-group cleanup so status does not wait on inherited pipes, and add a real-subprocess regression covering prompt unhealthy status and descendant cleanup.