-
Notifications
You must be signed in to change notification settings - Fork 19.6k
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
beacon,console,core,eth,p2p,rpc: keep wg.Add
and wg.Done
closer
#29818
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is indeed more wordy than the original, but I agree about it. I also think having the Add
close to the Done
is nice.
@@ -47,7 +47,10 @@ func startEngineClient(config *lightClientConfig, rpc *rpc.Client, headCh <-chan | |||
cancelRoot: cancel, | |||
} | |||
ec.wg.Add(1) | |||
go ec.updateLoop(headCh) | |||
go func() { | |||
defer ec.wg.Done() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really see the difference between two approaches. They should be equivalent, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rjl493456442 Yes sir, in most cases they are equivalent.
But keeping wg.Done
close to wg.Add
is clearer for us to ensure we call wg.Done
correctly.
And if we just wanna run ec.updateLoop
in the main process for a test, wrapping wg.Done
in ec.updateLoop
will force us to call wg.Add
even though I don't wanna run in a goroutine.
IMO letting the caller decide the way to call ec.updateLoop
is more flexible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so too, I think if we follow the previous behavior, we must run this function in a goroutine
It's not a bad idea to structure the WaitGroup usage like it is proposed here, but there is no need to update all code retroactively to change the style. It works as is. |
Similar to #29813, this PR modifies all other
sync.WaitGroup
cases.wg.Done
withoutwg.Add
will cause panic.wg.Done()
is meaningless withoutwg.Add
and goroutinewg.Add
andwg.Done
together can makes us easily ensure that both counts are equal.