Repository navigation
fix(node): close the api service on shutdown - #5671
Open
martinconic wants to merge 2 commits into
Open
martinconic wants to merge 2 commits into
martinconic wants to merge 2 commits into
Conversation
Shutdown closed only the api http.Server, which leaves hijacked websocket connections open, so api.Service.Close was never called. Websocket handlers never saw s.quit and clients were dropped without a close frame when the process exited. The api service was closed on shutdown before the bee and debug apis were merged. The chunk upload stream checked s.quit only between messages, so an idle client would still hold up the service close. Close it from a watcher, and send at most one close frame. Closes #5670.
martinconic
requested review from
acud,
akrem-chabchoub,
aloknerurkar,
gacevicljubisa,
janos and
sbackend123
October 21, 2026 17:40
akrem-chabchoub
requested changes
Oct 21, 2026
The api service's quit channel and background context are created in Configure, so closing it before then panics with close of nil channel. A node whose startup failed between creating the api service and configuring it did that during its shutdown, for example with a payment threshold below the minimum.
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.
Checklist
Description
Closes #5670.
On node shutdown,
Bee.Shutdownclosed only the API'shttp.Server.http.Serverdoes not track hijacked connections, so open websockets were left alone, andapi.Service.Close()was never called. As a result,s.quitnever fired,s.bgCtxwas never canceled, and websocket clients of/pss,/gsocand/chunks/streamwere dropped without a close frame (1006) when the process exited. Before #4651 merged the bee and debug APIs, the node closed the API service on shutdown; that line was dropped.Changes:
pkg/node:Shutdowncloses the API service right after thehttp.Server. The service is only registered for closing onceConfigurehas run, because its quit channel and background context are created there: a node whose startup fails before that point must not close it. The server stops accepting connections first; then the service tells the websocket handlers that are still open to send their close frame and return.api.Service.Close()waits up to 1s for them, and an error is added to the shutdown errors like any other component's.pkg/apichunk upload stream: its read loop checkeds.quitonly between messages, so an idle client kept the handler blocked in a read and made the service close time out. A watcher now sends1001and closes the socket on shutdown.sync.Onceguards the close frame, since the read loop and the watcher can both try to send it.Testing
TestShutdownClosesAPIServicechecks thatShutdowncloses the server and then the service, and returns the service's close error.TestChunkUploadStreamShutdownWithIdleClientchecks that an idle upload stream gets1001and thatService.Closedoes not report open websockets.TestNewBeeStartupFailureBeforeAPIConfigureruns the realNewBeewith a payment threshold below the minimum, which fails after the API service is created but before it is configured, and checks that it returns the error instead of panicking withclose of nil channelduring shutdown./chunks/streamand/pss/subscribeboth got1006, and the node log had noapi shutting downline.bee start --payment-threshold=1shuts down cleanly instead of panicking; on a normal SIGTERM the idle/chunks/streamgets1001 "node shutting down", and/pss/subscribegets a close frame (1005, since the pss handler sends one without a status code). The API service closes in under 1ms and the node exits normally.make build,make lint,go vetand the race detector onpkg/apiandpkg/nodeall pass.This merges cleanly with #5618, which documents the current
1006behavior for the new chunk stream and can return to promising1001once this lands.AI Disclosure