signalfx / signalfx-go

Go client library and instrumentation bindings for SignalFx
https://www.signalfx.com
Apache License 2.0
14 stars 48 forks source link

fix: Close leaking goroutines when conn == nil #190

Closed kskitek closed 1 year ago

kskitek commented 1 year ago

Problem

When the connection is not established (for example due to the wrong realm) Client.Close() leaves goroutines behind.

While testing, I have spawned 33 clients and closed 32 - this should leave me with just 2 goroutines but I have 34 instead. image

Details

Solution

Fix closing of the client

Refactor NewClient

I didn't take this path - random thoughts of other changes to fix the situation.

Testing

The scenario can be tested manually by:

I have provided a test Test_Close_DoesNotLeakGoroutines that simulates this situation and compares how many goroutines are before and after the test.

benkeith-splunk commented 1 year ago

Thanks for fixing this @kskitek. I'm looking at this and considering the different approaches and seeing if there is a better way. If I don't find anything we'll merge this. Thanks.

benkeith-splunk commented 1 year ago

Hey @kskitek sorry for the delay. This client was for a while needing a refactoring so I went ahead and made https://github.com/signalfx/signalfx-go/pull/198 that will be a new v2 module. I incorporated your goroutine check and have completely reworked how shutdown happens to hopefully avoid any leaks and make the client generally more robust.

kskitek commented 1 year ago

@benkeith-splunk Thank you for the fix! I like the changes in the reworked client. It will probably take us some time but we will migrate to v2.