You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: wait for HTTP shutdown before closing dependencies - #1253
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.
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.
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: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
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
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
When Tinyauth receives SIGTERM,
serve()ininternal/bootstrap/router_bootstrap.gois 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:ctx.Done(), then calledserver.Shutdown();Shutdown()closes the listener first, which makesServe()returnhttp.ErrServerClosedright away;serve()saw that return and exited, even thoughShutdown()was still waiting for active requests to finish.Since the listener's Ding goroutine (
RingNormal) was now done, Ding moved on toRingCriticaland 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 andserve()stays in control.Serve()sends its result to a buffered channel.serve()selects between that result andctx.Done().Shutdown()synchronously, soserve()only returns after the in-flight requests are done, or after the 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 theServegoroutine 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
TestServeWaitsForActiveRequestDuringShutdownininternal/bootstrap/router_bootstrap_test.go. It opens a request, blocks inside the handler, cancels the context, and checks thatserve()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 testmake vetmake test-racego build -tags nomsgpack ./...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-byorco-developedtrailers.Summary by CodeRabbit