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

rpcclient/WS: fix data race on concurrent (un)subscription #3532

Merged
merged 1 commit into from
Jul 26, 2024

Conversation

carpawell
Copy link
Member

Every client's (Un)Subscription call does two things: an RPC call and a subscription map lock (two of maps currently). If we imagine that there is one routine that tries to subscribe (A) and one routine that tries to unsubscribe (B), the following sequence can happen:

  1. Current number of subscriptions is X
  2. B does an RPC and makes number of subscriptions X-1
  3. A does an RPC and makes number of subscriptions X again
  4. A holds subscription locks and rewrites client's subscription state (subscription with ID X now points to a different channel; channel that was registered by B is lost and is not related to any real subscription but is still included in the receivers map)
  5. B holds subscription locks and drops subscription X (first, it is an error and we have just lost a subscription that we think was made successfully second, we have lost a channel in the receivers map, and no corresponding subscription points to it)
  6. X subscription is received by the WS client (in practice it is a new block, 100ms, quite often to be sure this issue happens every hour), we range through the receivers, see no corresponding subscription, and panic.

Closes #3093.

Every client's (Un)Subscription call does two things: an RPC call and a
subscription map lock (two of maps currently). If we imagine that there is
one routine that tries to subscribe (A) and one routine that tries to
unsubscribe (B), the following sequence can happen:

0. Current number of subscriptions is X
1. B does an RPC and makes number of subscriptions X-1
2. A does an RPC and makes number of subscriptions X again
3. A holds subscription locks and rewrites client's subscription state
   (subscription with ID X now points to a different channel; channel that
   was registered by B is lost and is not related to any real subscription
   but is still included in the `receivers` map)
4. B holds subscription locks and drops subscription X (first, it is an
   error and we have just lost a subscription that we think was made
   successfully second, we have lost a channel in the `receivers` map, and
   no corresponding subscription points to it)
5. X subscription is received by the WS client (in practice it is a new
   block, 100ms, quite often to be sure this issue happens every hour), we
   range through the receivers, see no corresponding subscription, and
   panic.

Closes #3093.

Signed-off-by: Pavel Karpy <[email protected]>
@carpawell carpawell self-assigned this Jul 26, 2024
@carpawell
Copy link
Member Author

@roman-khimov, we can try this. It is this comment's implementation. We have tried it in our loadings, no panic was found. Can be acceptable for a patch release. Does not stop us from creating a new issue that fixes it with a design change on the server side.

Copy link

codecov bot commented Jul 26, 2024

Codecov Report

Attention: Patch coverage is 72.72727% with 3 lines in your changes missing coverage. Please review.

Project coverage is 86.08%. Comparing base (4ff2063) to head (d6eaf6e).
Report is 41 commits behind head on master.

Files Patch % Lines
pkg/rpcclient/wsclient.go 72.72% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3532      +/-   ##
==========================================
- Coverage   86.19%   86.08%   -0.12%     
==========================================
  Files         331      330       -1     
  Lines       38494    38658     +164     
==========================================
+ Hits        33179    33277      +98     
- Misses       3785     3839      +54     
- Partials     1530     1542      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@roman-khimov roman-khimov merged commit c6ed92a into master Jul 26, 2024
19 of 21 checks passed
@roman-khimov roman-khimov deleted the fix/ws-client-subscription-panic branch July 26, 2024 16:29
Copy link
Member

@AnnaShaleva AnnaShaleva left a comment

Choose a reason for hiding this comment

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

A nice catch.

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.

NPE in WebSocket client subscriptions
3 participants