Skip to content

fix(review): handle SIGTERM for graceful shutdown - #1111

Open
HOWILLMAKEIT wants to merge 1 commit into
alibaba:mainfrom
HOWILLMAKEIT:fix/review-sigterm-graceful-shutdown
Open

fix(review): handle SIGTERM for graceful shutdown#1111
HOWILLMAKEIT wants to merge 1 commit into
alibaba:mainfrom
HOWILLMAKEIT:fix/review-sigterm-graceful-shutdown

Conversation

@HOWILLMAKEIT

Copy link
Copy Markdown
Contributor

Description

ocr review registers signal.NotifyContext with only os.Interrupt, so SIGTERM kills the process with zero cleanup. Real-world SIGTERM producers:

  • docker stop — sends SIGTERM to PID 1 (10s grace, then SIGKILL)
  • GitLab CI — job cancellation and timeout send SIGTERM
  • The project's own VS Code extension — sends SIGTERM on review cancel (merged PR [codex] fix(vscode): force-kill unresponsive reviews #489), with a 3s SIGKILL escalation

Impact when SIGTERM hits mid-review:

  • --output report is never created (the lazy writer opens the file only at emit time — not truncated, simply absent)
  • session_end and run_manifest are never persisted
  • --resume is rejected even when completed per-file checkpoints are on disk — the user pays for a full re-review

With this change, SIGTERM follows the same graceful path as Ctrl+C: context cancellation runs the existing defer chain (closeOut, MCP client close, manifest freeze, session_end persist). Measured graceful-shutdown latency on the SIGINT path is ~25ms — two orders of magnitude inside docker's 10s grace and the extension's 3s escalation window.

Ecosystem precedent: long-running container/CI CLIs (docker compose, hugo, terraform, controller-runtime) all register SIGINT+SIGTERM; SIGINT-only tools (gh CLI, kubectl port-forward) are short-lived commands where the distinction rarely matters.

Scope: only review_cmd.go. The scan path has a separate open PR (#996).

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

How Has This Been Tested?

  • make check passes (gofmt, go vet, license, english-check)
  • go test ./cmd/opencodereview/ -run TestReview passes
  • Cross-platform compile verified (linux/amd64, windows/amd64, darwin/arm64) — syscall.SIGTERM is a cross-platform constant
  • End-to-end: built a local binary with mock LLM, sent SIGTERM mid-review → exit 1, partial --output written, session_end persisted, --resume accepted (identical to SIGINT behavior); unpatched control → exit 143, output absent, resume rejected

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • New and existing unit tests pass locally with my changes
  • I have signed the CLA

Related Issues

Follow-up to #902 (which introduced the SIGINT-only NotifyContext and scoped out non-graceful termination) and #1054 (which fixed the launcher's exit code on signal death but not the graceful-shutdown half).

review_cmd.go registered signal.NotifyContext with only os.Interrupt.
SIGTERM — sent by docker stop, GitLab CI job cancellation/timeout, and
the project's own VS Code extension on review cancel (PR alibaba#489) — killed
the process with no cleanup: the --output report was never created
(lazyFileWriter opens on first write at emit time), session_end and
run_manifest were never persisted, and --resume was rejected even when
completed per-file checkpoints existed on disk, forcing a full paid
re-review.

Add syscall.SIGTERM to the NotifyContext call so SIGTERM follows the
same graceful path as Ctrl+C: context cancellation runs the existing
defer chain (closeOut, MCP client close, manifest freeze, session_end
persist). Measured graceful-shutdown latency on the SIGINT path is
~25ms — two orders of magnitude inside both docker's 10s grace period
and the VS Code extension's 3s SIGKILL escalation window.

Long-running container/CI CLI peers (docker compose, hugo, terraform,
controller-runtime) all register SIGINT+SIGTERM; SIGINT-only tools (gh,
kubectl port-forward) are short-lived commands where the distinction
rarely matters.
@github-actions

Copy link
Copy Markdown
Contributor

OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants