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

fix panic: concurrent write to websocket connection #3908

Merged
merged 6 commits into from
Aug 1, 2023

Conversation

millken
Copy link
Contributor

@millken millken commented Jul 27, 2023

Description

as title

Fixes #3907

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • [] New feature (non-breaking change which adds functionality)
  • [] Code refactor or improvement
  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)
  • [] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • [] make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • [] My code follows the style guidelines of this project
  • [] I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [] My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • [] New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

@codecov
Copy link

codecov bot commented Jul 27, 2023

Codecov Report

Merging #3908 (03a9c95) into master (e1f0636) will increase coverage by 0.67%.
Report is 74 commits behind head on master.
The diff coverage is 73.19%.

❗ Current head 03a9c95 differs from pull request most recent head b8eb929. Consider uploading reports for the commit b8eb929 to get more accurate results

@@            Coverage Diff             @@
##           master    #3908      +/-   ##
==========================================
+ Coverage   75.38%   76.05%   +0.67%     
==========================================
  Files         303      327      +24     
  Lines       25923    27855    +1932     
==========================================
+ Hits        19541    21186    +1645     
- Misses       5360     5578     +218     
- Partials     1022     1091      +69     
Files Changed Coverage Δ
action/protocol/execution/evm/evm.go 43.52% <0.00%> (-2.95%) ⬇️
action/protocol/execution/evm/evmstatedbadapter.go 66.77% <ø> (ø)
action/protocol/poll/consortium.go 0.00% <0.00%> (ø)
action/protocol/poll/staking_committee.go 43.85% <0.00%> (ø)
...tion/protocol/staking/contractstake_bucket_type.go 0.00% <0.00%> (ø)
api/grpcserver.go 86.40% <0.00%> (ø)
api/loglistener.go 25.00% <0.00%> (ø)
api/web3server_marshal.go 93.21% <ø> (ø)
api/websocket.go 4.10% <0.00%> (-1.07%) ⬇️
blockchain/config.go 74.54% <ø> (ø)
... and 53 more

... and 5 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@@ -36,6 +37,41 @@ var upgrader = websocket.Upgrader{
WriteBufferSize: 1024,
}

// type safeWebsocketConn wraps websocket.Conn with a mutex
Copy link
Member

Choose a reason for hiding this comment

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

@Liuhaai
Copy link
Member

Liuhaai commented Jul 31, 2023

good finding

@@ -70,7 +104,8 @@ func (wsSvr *WebsocketHandler) handleConnection(ctx context.Context, ws *websock
})

ctx, cancel := context.WithCancel(ctx)
go ping(ctx, ws, cancel)
safeWs := &safeWebsocketConn{ws: ws}
go ping(ctx, safeWs, cancel)
Copy link
Member

Choose a reason for hiding this comment

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

  1. according to https://pkg.go.dev/github.com/gorilla/websocket#hdr-Concurrency, Reader method also need concurrent control, like SetReadDeadline, SetReadLimit
  2. L95-103 above, SetReadDeadline called 2 times, is that a duplicate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. see websocket code, panic is only triggered on write(WriteMessage, WriteJSON), Set* will not
	if c.isWriting {
		panic("concurrent write to websocket connection")
	}
  1. the first one is used to set the initial read deadline, ensuring that data is read from the WebSocket connection within the specified time. The second one is used to reset the read deadline after receiving a heartbeat message.

Copy link
Member

Choose a reason for hiding this comment

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

but pkg.go.dev clearly stated:

"Applications are responsible for ensuring that no more than one goroutine calls the write methods (NextWriter, SetWriteDeadline, WriteMessage, WriteJSON, EnableWriteCompression, SetCompressionLevel) concurrently and that no more than one goroutine calls the read methods (NextReader, SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler, SetPingHandler) concurrently."

@sonarcloud
Copy link

sonarcloud bot commented Aug 1, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@millken millken merged commit 789336a into iotexproject:master Aug 1, 2023
3 checks passed
@millken millken deleted the issue3907 branch August 1, 2023 03:24
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.

[api] concurrent write to websocket connection
3 participants