-
Notifications
You must be signed in to change notification settings - Fork 326
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
Fix write to closed channel panic() in internal/connection during connection close #539
Merged
wolfstudy
merged 2 commits into
apache:master
from
dferstay:fix-write-to-closed-channel-panic-during-conn-close
Jun 30, 2021
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,7 @@ type connectionState int32 | |
const ( | ||
connectionInit = iota | ||
connectionReady | ||
connectionClosing | ||
connectionClosed | ||
) | ||
|
||
|
@@ -98,6 +99,8 @@ func (s connectionState) String() string { | |
return "Initializing" | ||
case connectionReady: | ||
return "Ready" | ||
case connectionClosing: | ||
return "Closing" | ||
case connectionClosed: | ||
return "Closed" | ||
default: | ||
|
@@ -142,6 +145,7 @@ type connection struct { | |
|
||
requestIDGenerator uint64 | ||
|
||
incomingRequestsWG sync.WaitGroup | ||
incomingRequestsCh chan *request | ||
incomingCmdCh chan *incomingCmd | ||
closeCh chan interface{} | ||
|
@@ -331,10 +335,15 @@ func (c *connection) waitUntilReady() error { | |
} | ||
|
||
func (c *connection) failLeftRequestsWhenClose() { | ||
// wait for outstanding incoming requests to complete before draining | ||
// and closing the channel | ||
c.incomingRequestsWG.Wait() | ||
|
||
reqLen := len(c.incomingRequestsCh) | ||
for i := 0; i < reqLen; i++ { | ||
c.internalSendRequest(<-c.incomingRequestsCh) | ||
} | ||
|
||
close(c.incomingRequestsCh) | ||
} | ||
|
||
|
@@ -546,8 +555,13 @@ func (c *connection) Write(data Buffer) { | |
|
||
func (c *connection) SendRequest(requestID uint64, req *pb.BaseCommand, | ||
callback func(command *pb.BaseCommand, err error)) { | ||
if c.getState() == connectionClosed { | ||
c.incomingRequestsWG.Add(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this waitgroup needed for both send requests? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned #539 (comment) we need to track all writes to the incomingRequestsCh |
||
defer c.incomingRequestsWG.Done() | ||
|
||
state := c.getState() | ||
if state == connectionClosed || state == connectionClosing { | ||
callback(req, ErrConnectionClosed) | ||
|
||
} else { | ||
select { | ||
case <-c.closeCh: | ||
|
@@ -563,7 +577,11 @@ func (c *connection) SendRequest(requestID uint64, req *pb.BaseCommand, | |
} | ||
|
||
func (c *connection) SendRequestNoWait(req *pb.BaseCommand) error { | ||
if c.getState() == connectionClosed { | ||
c.incomingRequestsWG.Add(1) | ||
defer c.incomingRequestsWG.Done() | ||
|
||
state := c.getState() | ||
if state == connectionClosed || state == connectionClosing { | ||
return ErrConnectionClosed | ||
} | ||
|
||
|
@@ -586,7 +604,8 @@ func (c *connection) internalSendRequest(req *request) { | |
c.pendingReqs[*req.id] = req | ||
} | ||
c.pendingLock.Unlock() | ||
if c.getState() == connectionClosed { | ||
state := c.getState() | ||
if state == connectionClosed || state == connectionClosing { | ||
c.log.Warnf("internalSendRequest failed for connectionClosed") | ||
if req.callback != nil { | ||
req.callback(req.cmd, ErrConnectionClosed) | ||
|
@@ -755,6 +774,8 @@ func (c *connection) UnregisterListener(id uint64) { | |
// broadcasting the notification on the close channel | ||
func (c *connection) TriggerClose() { | ||
c.closeOnce.Do(func() { | ||
c.setState(connectionClosing) | ||
|
||
cnx := c.cnx | ||
if cnx != nil { | ||
cnx.Close() | ||
|
@@ -775,9 +796,10 @@ func (c *connection) Close() { | |
} | ||
|
||
c.log.Info("Connection closed") | ||
c.TriggerClose() | ||
// do not use changeState() since they share the same lock | ||
c.setState(connectionClosed) | ||
c.TriggerClose() | ||
|
||
c.pingTicker.Stop() | ||
c.pingCheckTicker.Stop() | ||
|
||
|
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.
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.
I'm a little confused why are we waiting here?
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.
@cckellogg ,
At this point we are sure that the closeCh is closed and that the state of the connection is either connectionClosing or connectionClosed. We wait to be sure that there will be no further writes to the incomingRequestsCh before attempting to drain and close it. It is possible for the sending go-routine to be executing either of the following lines at the time this function is executed:
As mentioned in the motivation for this MR:
Whenever I see a channel being closed by a reader go-routine I am suspicious of writers attempting to write to the channel after it is closed.