Repository navigation
Conversation
alexciuca
force-pushed
the
fix/api-listen-shutdown-race
branch
from
November 6, 2026 17:22
8209086 to
1d0c474
Compare
Listen and Shutdown both use server.http without a lock, so Ctrl-C during startup is a data race. If Shutdown runs first it does nothing, and Listen keeps serving forever. Add a mutex around server.http and a stopped flag, so Listen won't start serving once Shutdown has been called. Fixes Shopify#667
alexciuca
force-pushed
the
fix/api-listen-shutdown-race
branch
from
November 6, 2026 17:23
1d0c474 to
f6ffd61
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Listenwritesserver.httpfrom its goroutine whileShutdownreads it from main, so Ctrl-C during startup is a data race. IfShutdownruns first, it sees nil and returns, andListenthen serves forever.server.httpis now guarded by a mutex, andShutdownsets astoppedflag so aListenthat hasn't started serving returns nil. The lock is released beforehttp.Server.Shutdown, which can block for up towait_timeout. IfShutdownlands between the unlock andListenAndServe,net/httpreturnsErrServerClosed, whichListenmaps to nil.Behavior change:
Shutdownis final.ListenafterShutdownreturns nil instead of serving, matchinghttp.Server, which can't be reused. Nothing in the repo restarts a server.Testing
TestShutdownBeforeListenfails onmaineven without-race.TestConcurrentListenAndShutdownfails onmainunder-race.make testandgolangci-lint runpass.Relation to #668
#668 targets the same issue but conflicts with
mainand hasn't been reviewed. It also still races:server.shutdownis assigned afterinitOnce.Doreturns, so thesync.Oncedoesn't order that write with the read inShutdown.Replaces #789. Fixes #667