Skip to content
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

Remove lock from sessionWS.SendBytes #1140

Merged
merged 2 commits into from
Jan 24, 2024

Conversation

joshgarnett
Copy link
Contributor

There are six different calls to s.Lock() in sessionWS. Three of these locks are around calls to SetWriteDeadline and WriteMessage on the WebSocket. WriteMessage will block for up to the timeout set in SetWriteDeadline, which has a default of 5000 milliseconds. This lock is also acquired by SendBytes to determine if the session has been stopped. This is a problem as the event processing methods in StatusRegistry and Track call SendBytes directly on the Session. If one of the Sessions being written to has delays in writing data, this could cause backups in the event processing.

The simplest approach (this commit) is to just remove the lock within SendBytes. To avoid a race condition with writing to a closed channel, we need to not close the outgoingCh channel in the Close method. This is safe to do as channels do not need to be explicitly closed and will automatically be cleaned up by the garbage collector. It should also be possible to remove the lock entirely from sessionWS, but that is a much larger change.

There are six different calls to s.Lock() in sessionWS.  Three of these locks are around calls to SetWriteDeadline and WriteMessage on the WebSocket.  WriteMessage will block for up to the timeout set in SetWriteDeadline, which has a default of 5000 milliseconds.  This lock is also acquired by SendBytes to determine if the session has been stopped.  This is a problem as the event processing methods in StatusRegistry and Track call SendBytes directly on the Session.  If one of the Sessions being written to has delays in writing data, this could cause backups in the event processing.

The simplest approach (this commit) is to just remove the lock within SendBytes.  To avoid a race condition with writing to a closed channel, we need to not close the outgoingCh channel in the Close method.  This is safe to do as channels do not need to be explicitly closed and will automatically be cleaned up by the garbage collector.  It should also be possible to remove the lock entirely from sessionWS, but that is a much larger change.
@CLAassistant
Copy link

CLAassistant commented Nov 14, 2023

CLA assistant check
All committers have signed the CLA.

joshgarnett added a commit to joshgarnett/nakama that referenced this pull request Nov 14, 2023
There are six different calls to s.Lock() in sessionWS.  Three of these locks are around calls to SetWriteDeadline and WriteMessage on the WebSocket.  WriteMessage will block for up to the timeout set in SetWriteDeadline, which has a default of 5000 milliseconds.  This lock is also acquired by SendBytes to determine if the session has been stopped.  This is a problem as the event processing methods in StatusRegistry and Track call SendBytes directly on the Session.  If one of the Sessions being written to has delays in writing data, this could cause backups in the event processing.

This change is more complex than heroiclabs#1140.  It fully removes the mutex from sessionWS.  All writes have been moved to only occur within processOutgoing so no panics are generated by concurrent writes.  The stopped variable is converted to an atomic.Bool, which is only used in the Close method to prevent more than one Close call being made.  As the outgoingCh is not closed, Send() is used to send the final messages and then the ctxCancelFn is called, which causes a Close message to be sent in processOutgoing after all messages are sent.  The ping backoff logic was also simplified.
@zyro zyro merged commit 0ae1013 into heroiclabs:master Jan 24, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants