sdk/trace: fix goroutine leak in ForceFlush on context cancel#6363
sdk/trace: fix goroutine leak in ForceFlush on context cancel#6363sipsma wants to merge 1 commit intoopen-telemetry:mainfrom
Conversation
When `batchSpanProcessor.ForceFlush` was called and the context was canceled, the method's select statement returned the context err right away. This also meant that the `wait` chan was no longer being read from, which meant that the goroutine attempting to write to it ended up blocking and leaking forever. The fix is to just make the `wait` chan buffered with a size of 1. It can always be written to and will just be gc'd if the `ForceFlush` method has already exited. Signed-off-by: Erik Sipsma <erik@sipsma.dev>
The committers listed above are authorized under a signed CLA. |
|
Could you add a test? |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #6363 +/- ##
=====================================
Coverage 81.8% 81.8%
=====================================
Files 283 283
Lines 24900 24900
=====================================
Hits 20386 20386
Misses 4110 4110
Partials 404 404 |
@dmathieu happy to, but not sure yet how to go about it for this case of testing that the goroutine didn't leak. There's always something like dumping current goroutine stack traces, but felt extremely fiddly (if you disagree let me know, can always give it a shot). The other possibility would be changing the actual code to make it more unit testable for this case; e.g. changing Let me know what you think, open to any suggestions. |
When
batchSpanProcessor.ForceFlushwas called and the context was canceled, the method's select statement returned the context err right away.This also meant that the
waitchan was no longer being read from, which meant that the goroutine attempting to write to it ended up blocking and leaking forever.The fix is to just make the
waitchan buffered with a size of 1. It can always be written to and will just be gc'd if theForceFlushmethod has already exited.