Skip to content

fix: wait for HTTP shutdown before closing dependencies - #1253

Open
Yestardays wants to merge 1 commit into
tinyauthapp:mainfrom
Yestardays:fix/http-shutdown-wait
Open

Yestardays wants to merge 1 commit into
tinyauthapp:mainfrom
Yestardays:fix/http-shutdown-wait

Conversation

@Yestardays

@Yestardays Yestardays commented Nov 27, 2026 •

Copy link
Copy Markdown

What was wrong

When Tinyauth receives SIGTERM, serve() in internal/bootstrap/router_bootstrap.go is supposed to stop the HTTP listener before the rest of the shutdown sequence runs. It wasn't waiting long enough.

The old code ran server.Serve() on the caller goroutine and did the shutdown in a separate goroutine:

  • the goroutine waited for ctx.Done(), then called server.Shutdown();
  • Shutdown() closes the listener first, which makes Serve() return http.ErrServerClosed right away;
  • serve() saw that return and exited, even though Shutdown() was still waiting for active requests to finish.

Since the listener's Ding goroutine (RingNormal) was now done, Ding moved on to RingCritical and closed the database. A request that was still being handled at that point could lose its DB connection, which is what you'd see as failed requests during a restart or deploy.

The fix

I reversed the control flow: Serve() now runs in a goroutine and serve() stays in control.

  • Serve() sends its result to a buffered channel.
  • serve() selects between that result and ctx.Done().
  • On cancellation it calls Shutdown() synchronously, so serve() only returns after the in-flight requests are done, or after the timeout.
  • The 5 second timeout (model.GracefulShutdownTimeout) and the existing error log are unchanged. If the timeout is hit, shutdown still continues with the remaining rings, same as before.
  • listener.Close() is deferred. The channel has room for one value, so the Serve goroutine won't get stuck if shutdown wins the race.

I used Grafana's HTTP server shutdown as a reference for the structure.

Tests

I added TestServeWaitsForActiveRequestDuringShutdown in internal/bootstrap/router_bootstrap_test.go. It opens a request, blocks inside the handler, cancels the context, and checks that serve() does not return until the handler is released. It fails on the old code and passed 20 runs in a row with the fix.

Also ran:

  • make test
  • make vet
  • make test-race
  • go build -tags nomsgpack ./...
  • Playwright e2e: 40 passed
  • frontend lint: 0 errors, 5 existing warnings

AI disclosure

I used GPT-6.2 Sol Ultra and GPT-6.2 Astra Ultra while working on this. They helped me analyze the shutdown ordering, make the Go change, write the regression test, and run the checks above. I reviewed everything, I understand the change, and I'm responsible for it. No assisted-by or co-developed trailers.

Summary by CodeRabbit

  • Bug Fixes
    • Active requests can now finish during a graceful service shutdown, rather than being interrupted as soon as shutdown begins. Shutdown remains bounded by the configured grace period, and the service reports listener-start errors while handling shutdown errors in its logs.

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

BootstrapApp.serve now handles context cancellation by initiating timed graceful shutdown. A new integration test checks that serve waits for an active request to complete before returning.

Changes: Graceful server shutdown

Layer / File(s) Summary
Serve cancellation and graceful shutdown
internal/bootstrap/router_bootstrap.go, internal/bootstrap/router_bootstrap_test.go
serve runs server.Serve asynchronously and selects between its result and context cancellation. On cancellation, it calls server.Shutdown with a timeout and defers listener closure. The test verifies that shutdown waits for an active request, which receives HTTP 204, before serve returns.

Estimated code review effort: 2 (Simple) | ~15 minutes

Change: Bug fix

Suggested reviewers: steveiliop56

Merge Risk: Minimal · up to 66fd1

This change makes the server wait for in-flight HTTP requests to finish, up to the existing 5-second timeout, before dependencies such as the database are closed during shutdown. I found no remaining merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix: wait for HTTP shutdown before closing dependencies" matches the main change. The change makes serve() call Shutdown() synchronously and return only after active requests finish or the …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "fix: wait for HTTP shutdown before closing dependencies" matches the main change. The change makes serve() call Shutdown() synchronously and return only after active requests finish or the timeout expires, so dependencies such as the database close after HTTP draining. The title is specific and concise. It omits the test addition, which is expected.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 54e86ea and 66fd1bc.

Files selected for processing: 2

  • internal/bootstrap/router_bootstrap.go
  • internal/bootstrap/router_bootstrap_test.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

This branch has not been deployed

No deployments
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