Skip to content

Commit a228f2f

Browse files
ilonatommyradical
andauthored
[browser][debugger][MT] Make operations on dictionary thread-safe. (#87405)
* `ConcurrentDicionary` should be thread-safe. Co-authored-by: Ankit Jain <radical@gmail.com> --------- Co-authored-by: Ankit Jain <radical@gmail.com>
1 parent a052348 commit a228f2f

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/mono/wasm/debugger/DebuggerTestSuite/FirefoxInspectorClient.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public override async Task ProcessCommand(Result command, CancellationToken toke
143143
return null;
144144

145145
var messageId = new FirefoxMessageId("", 0, from_str);
146-
if (pending_cmds.Remove(messageId, out var item))
146+
if (pending_cmds.TryRemove(messageId, out var item))
147147
item.SetResult(Result.FromJsonFirefox(res));
148148
else
149149
logger.LogDebug($"HandleMessage: Could not find any pending cmd for {messageId}. msg: {msg}");
@@ -156,7 +156,7 @@ public override async Task ProcessCommand(Result command, CancellationToken toke
156156
return null;
157157

158158
var messageId = new FirefoxMessageId("", 0, from_str);
159-
if (pending_cmds.Remove(messageId, out var item))
159+
if (pending_cmds.TryRemove(messageId, out var item))
160160
{
161161
item.SetResult(Result.FromJsonFirefox(res));
162162
return null;
@@ -234,8 +234,7 @@ public override Task<Result> SendCommand(SessionId sessionId, string method, JOb
234234
throw new Exception($"No 'to' field found in '{args}'");
235235

236236
msgId = new FirefoxMessageId("", 0, to_str);
237-
pending_cmds[msgId] = tcs;
238-
logger.LogTrace($"SendCommand: to: {args}");
237+
pending_cmds.AddOrUpdate(msgId, tcs, (key, oldValue) => tcs);
239238

240239
var msg = args.ToString(Formatting.None);
241240
var bytes = Encoding.UTF8.GetBytes(msg);

src/mono/wasm/debugger/DebuggerTestSuite/InspectorClient.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5-
using System.Collections.Generic;
5+
using System.Collections.Concurrent;
66
using System.Net;
77
using System.Net.Sockets;
88
using System.Net.WebSockets;
@@ -17,7 +17,7 @@ namespace DebuggerTests
1717
{
1818
internal class InspectorClient : DevToolsClient
1919
{
20-
protected Dictionary<MessageId, TaskCompletionSource<Result>> pending_cmds = new Dictionary<MessageId, TaskCompletionSource<Result>>();
20+
protected readonly ConcurrentDictionary<MessageId, TaskCompletionSource<Result>> pending_cmds = new();
2121
protected Func<string, string, JObject, CancellationToken, Task> onEvent;
2222
protected int next_cmd_id;
2323

@@ -39,7 +39,7 @@ protected virtual Task HandleMessage(string msg, CancellationToken token)
3939
return onEvent(res["sessionId"]?.Value<string>(), res["method"].Value<string>(), res["params"] as JObject, token);
4040

4141
var id = res.ToObject<MessageId>();
42-
if (!pending_cmds.Remove(id, out var item))
42+
if (!pending_cmds.TryRemove(id, out var item))
4343
logger.LogError($"Unable to find command {id}");
4444

4545
item.SetResult(Result.FromJson(res));
@@ -95,7 +95,9 @@ public virtual Task<Result> SendCommand(SessionId sessionId, string method, JObj
9595
if (sessionId != SessionId.Null)
9696
o.Add("sessionId", sessionId.sessionId);
9797
var tcs = new TaskCompletionSource<Result>();
98-
pending_cmds[new MessageId(sessionId.sessionId, id)] = tcs;
98+
99+
MessageId msgId = new MessageId(sessionId.sessionId, id);
100+
pending_cmds.AddOrUpdate(msgId, tcs, (key, oldValue) => tcs);
99101

100102
var str = o.ToString();
101103

0 commit comments

Comments
 (0)