Skip to content

fix(execd): return the command SSE handler after execution completes - #1672

Open
vinkiYu wants to merge 1 commit into
opensandbox-group:mainfrom
vinkiYu:fix/execd-command-sse-tail-latency
Open

fix(execd): return the command SSE handler after execution completes#1672
vinkiYu wants to merge 1 commit into
opensandbox-group:mainfrom
vinkiYu:fix/execd-command-sse-tail-latency

Conversation

@vinkiYu

@vinkiYu vinkiYu commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

Closes #1661.

Summary

Foreground commands (the /command SSE endpoint) pay a fixed ~1s tail latency: RunCommand waits for the execution-complete event and then unconditionally sleeps for --graceful-shutdown-timeout (default 1s), so short commands such as true, echo, or mkdir measure ~1.01s end to end (measurements in #1661).

waitForExecutionComplete already bounds the drain window: it returns as soon as the final SSE event is written and flushed, on client disconnect, or at the --graceful-shutdown-timeout deadline. This drops the extra sleep so the command endpoint behaves like RunCode and RunInSession, which adopted exactly this early-return pattern in 7f95adb ("return immediately to avoid fixed tail latency"). --graceful-shutdown-timeout keeps its meaning as the completion-wait deadline, so deployments that want a longer safety window can still tune it.

On the egress-sidecar concern from the old comment: RunCode / RunInSession have returned immediately after the completion callback since 7f95adb over the same runner / SSE / egress topology, so this aligns the command endpoint with behavior that has already shipped.

Testing

  • Not run (explain why)
  • Unit tests
  • Integration tests
  • e2e / manual verification

New TestRunCommandReturnsBeforeGracefulShutdownTimeoutAfterImmediateComplete mirrors the existing RunCode regression test: the handler must return well before the timeout when execution completes immediately. It fails on the old code and passes with this change. The rest of the pkg/web/controller suite shows no new failures (TestBackgroundRun_HTTPFlow and TestGetRunLogs_RejectsMalformedCursor fail identically on unmodified main in a Windows environment; Linux CI is unaffected).

Breaking Changes

  • None
  • Yes (describe impact and migration path)

The intended behavior change is the latency fix itself: the SSE response now closes right after the final event instead of holding the connection open for the grace window. The flag still bounds the completion wait, so no configuration migration is needed.

Checklist

  • Linked Issue or clearly described motivation
  • Added/updated docs (if needed)
  • Added/updated tests (if needed)
  • Security impact considered
  • Backward compatibility considered

RunCommand waited for the execution-complete event and then slept for
the full graceful-shutdown timeout (default 1s), adding a fixed tail
latency to every foreground command (opensandbox-group#1661: short commands such as
true or echo measure ~1.01s end to end).

waitForExecutionComplete already bounds the drain window: it returns
as soon as the final SSE event is written and flushed, on client
disconnect, or at the --graceful-shutdown-timeout deadline. Drop the
extra sleep so the command endpoint behaves like RunCode and
RunInSession, which have returned immediately after completion since
7f95adb. The flag keeps its meaning as the completion-wait deadline.

Add a regression test mirroring the existing RunCode one: the handler
must return well before the timeout when execution completes
immediately.
Copilot AI lite review requested due to automatic review settings August 29, 2026 05:12
@github-actions github-actions Bot added component/execd size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Aug 29, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes the fixed tail-latency from the components/execd foreground /command SSE endpoint by eliminating the unconditional post-completion sleep, aligning RunCommand with the existing early-return behavior already used by RunCode and RunInSession.

Changes:

  • Removed the unconditional time.Sleep(flag.ApiGracefulShutdownTimeout) after command execution completion, relying on waitForExecutionComplete as the bounded drain/completion-wait mechanism.
  • Added a regression unit test to ensure RunCommand returns well before the graceful shutdown timeout when execution completes immediately.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
components/execd/pkg/web/controller/command.go Drops the extra post-completion sleep so the handler can return immediately after the completion event is flushed (bounded by waitForExecutionComplete).
components/execd/pkg/web/controller/command_test.go Adds a regression test verifying RunCommand returns before the configured graceful shutdown timeout on immediate completion.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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

Labels

component/execd size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce or clarify the default 1s SSE tail-drain latency for foreground commands

2 participants