Shutdown chain router before waiting for chain creator to exit when shutting down #4633
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.
Why this should be merged
Currently, when the X-chain executes transactions while bootstrapping, it cannot halt until it finishes executing the current batch of transactions, because as seen in the stack trace below, the X-chain's bootstrapping is done from within the chain creation goroutine. In order to stop the bootstrapper of the X-chain, the chain router needs to be stopped which in turn stops the various chains. However, before stopping the chain router, the chain manager's shutdown procedure waits for the chain creation procedure to finish, which cannot finish before the bootstrapping is done.
How this works
Fortunately, it's safe to close the chain creation queue first, which prevents new chains from forming, and then proceed to stop the chain router and only afterwards to wait for the chain creation to finish.
The reason it's safe is because the chain router simply calls Stop() on all chains, and each chain's stop procedure simply calls thread safe methods.
How this was tested
When compiling a bootstrapping Fuji node with race detector and shutting down the chain, it doesn't complain:
Need to be documented in RELEASES.md?