Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

Halimao
Copy link
Contributor

@Halimao Halimao commented May 22, 2024

Similar to #29813, this PR modifies all other sync.WaitGroup cases.

  1. call wg.Done without wg.Add will cause panic.
  2. wg.Done() is meaningless without wg.Add and goroutine
  3. Putting wg.Add and wg.Done together can makes us easily ensure that both counts are equal.

core/state/statedb_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@holiman holiman left a 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()
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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

@fjl
Copy link
Contributor

fjl commented May 23, 2024

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.

@fjl fjl closed this May 23, 2024
@Halimao Halimao deleted the refactor/waitgroup branch May 23, 2024 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants