Skip to content

fix(server): cancel the calls left after the grace, and wait for them - #17

Merged
rosylilly merged 1 commit into
mainfrom
fix/shutdown-cancels-calls
Nov 25, 2026
Merged

rosylilly merged 1 commit into
mainfrom
fix/shutdown-cancels-calls

Conversation

@rosylilly

Copy link
Copy Markdown
Member

Summary

The ctx that main gets from signal.NotifyContext reached server.Run, but no call ever received it. On SIGTERM:

  1. Shutdown stopped taking new calls and waited for the ones in flight. That part was right.
  2. A call's ctx came from its connection, not from the SIGTERM ctx, so -shutdown-timeout running out did not cancel it.
  3. Run returned an error with those calls still running, and main called os.Exit(1). That skipped the deferred cleanups:
    • The temporary directory of each such call, holding the upload with its metadata, was left on disk. The README promises it is always removed.
    • Outside a container, ImageMagick and FFmpeg kept running after the plugin had exited.

Changes

  • http.Server.BaseContext gives each call a ctx derived from Run's ctx through context.WithoutCancel. It keeps the ctx's values but is not cancelled by SIGTERM itself, so in-flight calls keep their grace.
  • When the grace is over, Run:
    • cancels the ctx of the calls left, which kills their commands through exec.CommandContext;
    • closes their connections with srv.Close();
    • waits for every handler to return before returning itself, so that each handler removes its temporary directory first.
  • The error Run returns says the calls were cut short.
  • In both READMEs, the -shutdown-timeout row now says that the calls still running are cancelled and their files removed.

Tests

  • TestShutdownLetsCallsFinish: a call shorter than the grace is not cancelled and answers normally, and Run returns nil.
  • TestShutdownCancelsCallsAfterGrace: a call that never ends is cancelled after the grace, and Run returns only after its handler has finished cleaning up.
    • Against the old code it fails as expected: context deadline exceeded, and Run returns before the handler.

scripts/lint.sh and go test -race ./internal/... pass.

🤖 Generated with Claude Code

The context of a call came from the connection, not from Run's: a SIGTERM shut
the server down, but never cancelled a call. After -shutdown-timeout, Run
returned with the calls still running, and main exited at once: the commands of
those calls ran on, outside a container, and their temporary directories, which
hold the uploads with their metadata, stayed.

Each call's context now comes from Run's, without its end: a SIGTERM still leaves
the calls their grace. When the grace is over, Run cancels the calls left, which
stops their commands (exec.CommandContext), closes their connections, and waits
for their handlers, which remove their directories, before it returns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rosylilly
rosylilly merged commit 4bad043 into main Nov 25, 2026
9 checks passed
@rosylilly
rosylilly deleted the fix/shutdown-cancels-calls branch November 25, 2026 03:41
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.

1 participant