trace: avoid race between event recycling and freeTrace() #75
+101
−1
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.
When there are less than 5 events in a trace, the
tr.events
slice is backed by thetr.eventsBuf
array. This leads to a race between the background recycler invocation inunref()
. IffreeTrace()
runs before the recycler goroutine,tr.events
just consists of empty event objects when the recycler finally gets to them, and thus the user's recycling function cannot do any useful recycling (asevent.What
is now nil). So in the case where the array is shared, we make a copy and pass that to the recycler, so that recycling can be done in the background butfreeTrace
can be run immediately.Since the copying is only done when there are less than 5 events to copy, it should not have a significant performance impact.
I added a bunch of test cases around this, perhaps too many. And, you will be horrified at how we wait for all the recyclers to run in the test. I did this to avoid modifying the code to add synchronization between the entire recycling operation and the test, and to avoid the test hanging for a long time when it's in the failing state. Suggestions for improvement would be most welcome!