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

Prevent concurrent writes to a websocket connection #1656

Merged
merged 1 commit into from
Nov 22, 2023

Conversation

zkokelj
Copy link
Contributor

@zkokelj zkokelj commented Nov 22, 2023

Why this change is needed

Gateway crashed because of this. See logs:

https://app.datadoghq.eu/logs?query=service%3Aobscuro_gateway_sepolia_testnet%20container_id%3A6f39991112df50e334e5c2627fa104decaae3e0977db20b27ef10540a5d843f9%20&cols=host%2Cservice&event=AgAAAYv1CizXdvjiHgAAAAAAAAAYAAAAAEFZdjFDamRRQUFDZHVZQnZoUWZ5M1FBSAAAACQAAAAAMDE4YmY1MmYtNGM3NC00MDRiLWFiNGUtOGZhNDcxN2EwNzMw&index=%2A&messageDisplay=inline&refresh_mode=sliding&stream_sort=desc&view=spans&viz=stream&from_ts=1700556971076&to_ts=1700643371076&live=true

What changes were made as part of this PR

Please provide a high level list of the changes made

PR checks pre-merging

Please indicate below by ticking the checkbox that you have read and performed the required
PR checks

  • PR checks reviewed and performed

Copy link

coderabbitai bot commented Nov 22, 2023

Walkthrough

The update involves adding thread-safety to a Go struct, userConnWS, by introducing a mutex, mu. This mutex is used to control access to the struct's methods, ensuring that only one goroutine can write to the websocket connection at a time. The WriteResponse method now includes calls to lock and unlock this mutex, preventing race conditions and potential data corruption when multiple goroutines interact with the websocket connection concurrently.

Changes

File Change Summary
.../userconn/user_conn.go Added a sync.Mutex named mu to the userConnWS struct and implemented mutex locking and unlocking in the WriteResponse method for thread-safety.

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

@zkokelj zkokelj marked this pull request as ready for review November 22, 2023 08:58
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4321483 and 23476d4.
Files selected for processing (1)
  • tools/walletextension/userconn/user_conn.go (3 hunks)
Additional comments: 3
tools/walletextension/userconn/user_conn.go (3)
  • 6-12: The import of the sync package is correct and necessary for the use of sync.Mutex. Ensure that there are no unused imports.

  • 40-43: The addition of the mu sync.Mutex field to the userConnWS struct is appropriate for handling concurrent writes to the websocket connection. This change should prevent the race conditions that were causing the Gateway to crash.

  • 111-112: The use of mu.Lock() and defer mu.Unlock() is correct for ensuring that the WriteResponse method is safe for concurrent use by multiple goroutines. However, ensure that there are no other methods that modify the conn field or other shared resources without proper synchronization.

@@ -106,6 +108,9 @@ func (w *userConnWS) ReadRequest() ([]byte, error) {
}

func (w *userConnWS) WriteResponse(msg []byte) error {
w.mu.Lock()
defer w.mu.Unlock()

err := w.conn.WriteMessage(websocket.TextMessage, msg)
if err != nil {
if websocket.IsCloseError(err) || strings.Contains(string(msg), "EOF") {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling in the WriteResponse method checks for a close error or an "EOF" in the message. However, checking for "EOF" in the message content is not a standard way to handle websocket errors and could lead to false positives if "EOF" is part of a valid message payload. This should be reviewed and potentially refactored.

Copy link
Collaborator

@tudor-malene tudor-malene left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@zkokelj zkokelj merged commit e8b7b50 into main Nov 22, 2023
2 checks passed
@zkokelj zkokelj deleted the ziga/fix_websocket_concurrent_write_bug branch November 22, 2023 09:54
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.

2 participants