Skip to content

Small cleanup in App and Node #3962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

geoff-vball
Copy link

@geoff-vball geoff-vball commented May 14, 2025

Why this should be merged

While preparing a new RPC-signer PR, I noticed a few things that could be improved/simplified. There are 4 different atomic commits - feel free to take or leave any of them :)

How this works

  • There's a (currently inconsequential) race condition between checking and setting the shuttingDown atomic flag which could lead to shuttingDownExitCode being set more than once.
  • In App - Start(), Stop(),ExitCode() always return nil errors. I removed the error returns from the function signatures, which makes the possible code paths much easier to reason about.
  • DoneShuttingDown in Node is redundant, because the Once.Do() inside Shutdown() will block until shutdown is completed.
  • The error returned by n.Net.Dispatch() is used as the return value, but only ~25 lines later. Renamed to retErr to make it more clear that it is the error that should be returned.

How this was tested

Existing tests

Need to be documented in RELEASES.md?

No

@geoff-vball geoff-vball changed the title Small cleanup Small cleanup in App and Node May 14, 2025
@geoff-vball geoff-vball force-pushed the gstuart/small-cleanup branch from d1bffe0 to b8d9b39 Compare May 14, 2025 17:56
@yacovm
Copy link
Contributor

yacovm commented May 14, 2025

DoneShuttingDown in Node is redundant, because the Once.Do() inside Shutdown() will block until shutdown is completed.

It seems to me that the DoneShuttingDown is there to delay the Dispatch method until the first Shutdown invocation finished. If we are in the second invocation of Shutdown, the once.Do is a no-op, and therefore the second invocation will proceed to remove the ProcessContextFilePath before the first invocation of Shutdown finished.

@yacovm
Copy link
Contributor

yacovm commented May 14, 2025

There's a (currently inconsequential) race condition between checking and setting the shuttingDown atomic flag which could lead to shuttingDownExitCode being set more than once.

While that's true, we should note that all exit codes that are of endogenous reasons are 1, and all of exogenous reasons are 0, so in practice - it is highly unlikely that this can have a race.

@geoff-vball
Copy link
Author

geoff-vball commented May 14, 2025

@yacovm A second invocation of once.Do() is not a no-op, it blocks until the first invocation finishes. From the documentation no call to Do returns until the one call to f returns.

@yacovm
Copy link
Contributor

yacovm commented May 14, 2025

Oh you're right! My bad :-)

@joshua-kim joshua-kim requested review from a team and samliok and removed request for a team May 19, 2025 15:08
@joshua-kim joshua-kim moved this to In Progress 🏗️ in avalanchego May 19, 2025

// Stop notifies the application to exit and returns immediately.
// Stop should only be called after [Start].
// It is safe to call Stop multiple times.
Stop() error
Stop()

// ExitCode should only be called after [Start] returns with no error. It
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should be updated

@@ -129,9 +128,7 @@ func Run(app App) int {
close(stackTraceSignal)

// if there was an error closing or running the application, report that error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment should be updated

@@ -148,7 +145,7 @@ type app struct {
// Start the business logic of the node (as opposed to config reading, etc).
// Does not block until the node is done. Errors returned from this method
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment is stale

}

// Stop attempts to shutdown the currently running node. This function will
// return immediately.
func (a *app) Stop() error {
// block until shutdown is complete.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment change isn't really inaccurate (at least based on the intention of the original code).

Calling Shutdown(0) on the node doesn't actually block until shutdown finishes. Shutdown finishes when Dispatch() returns. Notice that Shutdown only starts the shutdown process.


// If the P2P server isn't running, shut down the node.
// If node is already shutting down, this does nothing.
// If node is already shutting down, this does not tigger shutdown again,
// and blocks until the shutdown is complete.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// and blocks until the shutdown is complete.
// and blocks until the shutdown returns.

@@ -682,7 +676,8 @@ func (n *Node) Dispatch() error {
)
}
// If the API server isn't running, shut down the node.
// If node is already shutting down, this does nothing.
// If node is already shutting down, this does not tigger shutdown again,
// and blocks until the shutdown is complete.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// and blocks until the shutdown is complete.
// and blocks until the shutdown returns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress 🏗️
Development

Successfully merging this pull request may close these issues.

4 participants