-
Notifications
You must be signed in to change notification settings - Fork 459
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
[DONE] Fix concurrently closing and writing to the same chan #35
Conversation
Pull Request Test Coverage Report for Build 347
💛 - Coveralls |
That way won't you leak channels? |
client/client.go
Outdated
@@ -300,8 +300,6 @@ func (c *Client) sendHeartbeats(interval int) { | |||
func (c *Client) Disconnect() { | |||
if c.Connected { | |||
c.Connected = false | |||
close(c.closeChan) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 207 in 724c8df
case <-c.closeChan: |
closeChan
is actually being used to stop these goroutines:
go c.handlePackets()
go c.pendingRequestsReaper()
so by not closing it you will have a leak
can't you solve this with a lock in both of the goroutines + the Disconnect() method? |
724c8df
to
b3bdd5b
Compare
as @cscatolini pointed out, the closeChan must be closed to avoid leaking goroutines. @felipejfc I could. But isn't it simpler this way? |
I guess we could leak channels by not closing them but in this case it doesn't seems like a reason for not doing it because we're only using this client for tests... (depending on how we implement pitaya-bot this may be a point of memory leak tho) |
@felipejfc I think channels are cleaned by the GC, no? |
After doing some research I guess it is @rcmgleite, you can merge this |
Problem
The go client spawns two goroutines: handlePackets and pendingRequestsReaper
They both write to client.IncomingMsgChan.
The problem is: the client.Disconnect method was closing the IncomingMsgChan
and there is no way for the handlePackets and pendingRequestReaper goroutines to know
that this happened.
When one of these 2 goroutines tries to write to the closed channel the application panics.
Solution
I'm not completely sure that just not closing the IncomingMsgChan is the correct solution
but in some articles like this there is a 'rule' to only close channels from the writer goroutine.