-
Notifications
You must be signed in to change notification settings - Fork 141
Fix isClosing data race #418
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
Open
dnerdy
wants to merge
4
commits into
main
Choose a base branch
from
fix-websocket-client-close-data-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -44,15 +44,19 @@ const ( | |
| ) | ||
|
|
||
| type webSocketClient struct { | ||
| Dialer Dialer | ||
| header http.Header | ||
| endpoint string | ||
| conn WSConn | ||
| connParams map[string]interface{} | ||
| Dialer Dialer | ||
| conn WSConn | ||
| header http.Header | ||
| connParams map[string]interface{} | ||
| // Closed when exiting the receive loop in listenWebSocket | ||
| errChan chan error | ||
| endpoint string | ||
| subscriptions subscriptionMap | ||
| isClosing bool | ||
| sync.Mutex | ||
|
|
||
| // Hold when accessing `exitListenWebSocket` | ||
| exitListenWebSocketMu sync.Mutex | ||
| // Set to indicate the listenWebSocket should exit | ||
| exitListenWebSocket bool | ||
| } | ||
|
|
||
| type webSocketInitMessage struct { | ||
|
|
@@ -104,27 +108,34 @@ func (w *webSocketClient) waitForConnAck() error { | |
| return nil | ||
| } | ||
|
|
||
| func (w *webSocketClient) handleErr(err error) { | ||
| w.Lock() | ||
| defer w.Unlock() | ||
| if !w.isClosing { | ||
| w.errChan <- err | ||
| } | ||
| } | ||
|
|
||
| func (w *webSocketClient) listenWebSocket() { | ||
| for { | ||
| if w.isClosing { | ||
| // The listenWebSocket goroutine "owns" interfaceChan. Both sending | ||
| // (in forwardWebSocketData below) and closure (here) happen in this | ||
| // goroutine, so there is no possibility of races between send and close. | ||
| // | ||
| // interfaceChan's are closed at the top of listenWebSocket to | ||
| // guarantee the channels are closed even if listenWebSocket will exit. | ||
| w.subscriptions.forEachSubscription(func(sub *subscription) { | ||
| if sub.hasBeenUnsubscribed() && sub.interfaceChan != nil { | ||
| reflect.ValueOf(sub.interfaceChan).Close() | ||
| sub.interfaceChan = nil | ||
| } | ||
| }) | ||
| w.exitListenWebSocketMu.Lock() | ||
| if w.exitListenWebSocket { | ||
| close(w.errChan) | ||
| return | ||
|
Collaborator
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. Do we need to unlock here? |
||
| } | ||
| w.exitListenWebSocketMu.Unlock() | ||
| _, message, err := w.conn.ReadMessage() | ||
| if err != nil { | ||
| w.handleErr(err) | ||
| w.errChan <- err | ||
| return | ||
| } | ||
| err = w.forwardWebSocketData(message) | ||
| if err != nil { | ||
| w.handleErr(err) | ||
| w.errChan <- err | ||
| return | ||
| } | ||
| } | ||
|
|
@@ -139,22 +150,20 @@ func (w *webSocketClient) forwardWebSocketData(message []byte) error { | |
| if wsMsg.ID == "" { // e.g. keep-alive messages | ||
| return nil | ||
| } | ||
| w.subscriptions.Lock() | ||
| defer w.subscriptions.Unlock() | ||
| sub, success := w.subscriptions.map_[wsMsg.ID] | ||
| if !success { | ||
| return fmt.Errorf("received message for unknown subscription ID '%s'", wsMsg.ID) | ||
| if wsMsg.Type == webSocketTypeComplete { | ||
| return w.subscriptions.Unsubscribe(wsMsg.ID) | ||
| } | ||
| if sub.hasBeenUnsubscribed { | ||
| return nil | ||
|
|
||
| sub, ok := w.subscriptions.GetSubscription(wsMsg.ID) | ||
| if !ok { | ||
| return fmt.Errorf("received message for unknown subscription ID '%s'", wsMsg.ID) | ||
| } | ||
| if wsMsg.Type == webSocketTypeComplete { | ||
| sub.hasBeenUnsubscribed = true | ||
| w.subscriptions.map_[wsMsg.ID] = sub | ||
| reflect.ValueOf(sub.interfaceChan).Close() | ||
| // Note: there's no data race between hasBeenUnsubscribed and the closed | ||
| // state of interfaceChan because interfaceChan is only closed by the | ||
| // caller of this function. | ||
| if sub.hasBeenUnsubscribed() { | ||
| return nil | ||
| } | ||
|
|
||
| return sub.forwardDataFunc(sub.interfaceChan, wsMsg.Payload) | ||
| } | ||
|
|
||
|
|
@@ -206,10 +215,11 @@ func (w *webSocketClient) Close() error { | |
| if err != nil { | ||
| return fmt.Errorf("failed to send closure message: %w", err) | ||
| } | ||
| w.Lock() | ||
| defer w.Unlock() | ||
| w.isClosing = true | ||
| close(w.errChan) | ||
|
|
||
| w.exitListenWebSocketMu.Lock() | ||
| w.exitListenWebSocket = true | ||
| w.exitListenWebSocketMu.Unlock() | ||
|
|
||
| return w.conn.Close() | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
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.
Struct fields were reordered due to the following lint error: