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.
One day, alertmanager could not send alerts in my production. So i made a dump file. The dump file shows that
Dispather
blocks on cleaning up empty aggrGroups while aggrGroup blocks onag.done
inaggrGroup.stop()
. After analysing the code,aggrGroup.stop()
should terminate theaggrGroup.run()
loop, and thenaggrGroup.stop()
exits normally. But in fact, it does not work as we hope.I think that there is a memory visibility error.
aggrGroup.run()
runs as a goroutine which initializesag.done
, andaggrGroup.stop()
runs in another goroutine which readsag.done
channel itself and waiting value from this channel. This is a data race! If those two goroutine runs on different cpu cores,aggrGroup.stop()
may be read from a nil channel even thoughaggrGroup.run()
has initialized the channel. So i fix this error by initializingag.done
innewAggrGroup()
.There is may be another error. If
aggrGroup.stop()
runs beforeaggrGroup.run()
,aggrGroup.stop()
may be also waiting from a nil channel becauseag.done
not be initialized inaggrGroup.run()
. Of course, it seems thataggrGroup.stop()
won't run beforeaggrGroup.run()
. But i don't think that initializing shared variables in one goroutine is a good practice.