Wind down spawned tasks when startup fails - #1086
Open
Jolah1 wants to merge 1 commit into
Open
Conversation
`Node::start` spawns background tasks - wallet sync, RGS gossip, pathfinding scores - before it can still fail, e.g. when resolving or binding the configured listening addresses. Until now the error path only stopped the chain source, leaving those tasks running behind a node that never came up, and leaving the node in a state a subsequent `start` could not cleanly recover from. Extract the wind-down sequence from `Node::stop` into a `Node::shutdown` helper and run it on any `start_inner` error. As the helper now also runs after a partial startup, it can no longer assume that every task exists: the two shutdown `watch::Sender::send` calls are allowed to find no receivers, and the `debug_assert!`s in `Runtime::wait_on_background_tasks` and `Runtime::wait_on_background_processor_task` that required a fully-started node are dropped in favour of doc comments spelling out that case. Fixes lightningdevkit#1009. This change was written with the assistance of Claude Code. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SUGgjhnpkCuFjsE3BjYAEx
|
I've assigned @tnull as a reviewer! |
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.
Fixes #1009.
Node::startspawns background tasks — wallet sync, RGS gossip, pathfinding scores — before it can still fail, most visibly when resolving or binding the configured listening addresses. The error arm only calledchain_source.stop(), so those tasks kept running behind a node that never came up.This extracts the wind-down sequence out of
Node::stopinto a privateNode::shutdownand runs it on anystart_innererror.Reviewer note — relaxed assertions. Because the wind-down now also runs after a partial startup, it can no longer assume every task exists:
watch::Sender::sendcalls are allowed to find no receivers (previouslylog_error!+debug_assert!(false)).Runtime::wait_on_background_tasks'debug_assert!(tasks.len() > 0)andRuntime::wait_on_background_processor_task'debug_assert!(false, "Expected a background processing task")are dropped, with doc comments in their place explaining that the empty case is now reachable by design.I considered keeping the assertions by threading an
expect_running: boolthroughshutdown(), but that trades a real invariant for a parameter the caller can get wrong, and the assertions only ever restated "we got here viastop". Happy to go the other way if you'd rather keep them.Testing
tests/integration_tests_rust.rs::failed_start_winds_down_background_tasks— squats one of the node's listening addresses sobindfails after the wallet-sync and pathfinding-scores tasks are up, asserts via aCollectingLogWriterthat the full shutdown sequence ran, then frees the address and confirms the node starts and stops cleanly.src/runtime.rs::winding_down_without_spawned_tasks_is_a_noop— the runtime wind-down calls tolerate finding nothing to wait on.This PR was written with the assistance of Claude Code.