-
Notifications
You must be signed in to change notification settings - Fork 402
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
Handle concurrent WebSockets messages #6210
Changes from 9 commits
a05a74a
8b44f59
99a3200
0da4e8b
9c3ec2d
fe571ac
4a085cd
9478eae
a9f7700
bc619e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -22,7 +22,7 @@ protected Subscription(IJsonRpcDuplexClient jsonRpcDuplexClient) | |||||||||||||
public string Id { get; } | ||||||||||||||
public abstract string Type { get; } | ||||||||||||||
public IJsonRpcDuplexClient JsonRpcDuplexClient { get; } | ||||||||||||||
private Channel<Action> SendChannel { get; } = Channel.CreateUnbounded<Action>(new UnboundedChannelOptions() { SingleReader = true }); | ||||||||||||||
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. Would making the channel static - shared between subscriptions - fix the problem? 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.
Unfortunately, this also does not work. We still have the same original concurrency issues (web socket gets in a bad state). 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. Why making the channel static didn't fix the problem? Then it is shared by all subscriptions and should have make all messages sequential? 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. Maybe channel should be on web socket itself and not on subscription if that is the case. 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. Or if we want to keep the lock we don't need channels at all? 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.
I think the reason is that each subscription instance starts its own nethermind/src/Nethermind/Nethermind.JsonRpc/Modules/Subscribe/Subscription.cs Lines 15 to 20 in fe2b14e
This means that we ensure no overlapping processing per subscription, but there are no guarantees of synchronization across them. A possible solution would be to put the lock in the 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.
Channels were introduced in #3458 to ensure ordering of messages. I believe that we could remove them and it should not affect the behavior due to locking, but I didn't want to introduce such change in this PR. 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. Need to double check, but I don't think that when two thread is waiting for a lock, there is a guarentee that they are released in order. 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. Also you can have communication on websocket that isn't subscriptions but normal request-response. I think we could downscale to having 1 channel per all subscriptions though, which would make it cheaper. |
||||||||||||||
private Channel<Func<Task>> SendChannel { get; } = Channel.CreateUnbounded<Func<Task>>(new UnboundedChannelOptions { SingleReader = true }); | ||||||||||||||
|
||||||||||||||
public virtual void Dispose() | ||||||||||||||
{ | ||||||||||||||
|
@@ -42,7 +42,7 @@ protected JsonRpcResult CreateSubscriptionMessage(object result) | |||||||||||||
}, default); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
protected void ScheduleAction(Action action) | ||||||||||||||
protected void ScheduleAction(Func<Task> action) | ||||||||||||||
{ | ||||||||||||||
SendChannel.Writer.TryWrite(action); | ||||||||||||||
} | ||||||||||||||
|
@@ -55,11 +55,11 @@ private void ProcessMessages() | |||||||||||||
{ | ||||||||||||||
while (await SendChannel.Reader.WaitToReadAsync()) | ||||||||||||||
{ | ||||||||||||||
while (SendChannel.Reader.TryRead(out Action action)) | ||||||||||||||
while (SendChannel.Reader.TryRead(out Func<Task> action)) | ||||||||||||||
{ | ||||||||||||||
try | ||||||||||||||
{ | ||||||||||||||
action(); | ||||||||||||||
await action(); | ||||||||||||||
} | ||||||||||||||
catch (Exception e) | ||||||||||||||
{ | ||||||||||||||
|
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.
Can you make test with competing subscriptions (same/different channel)
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.
Added test and it fails: since each subscription handles it's own queue of tasks we end up with the same problem.