Skip to content

Commit 773e851

Browse files
authored
Switch browser contexts from Dictionary to ConcurrentDictionary (#1899)
1 parent d86d6a9 commit 773e851

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

lib/PuppeteerSharp/Browser.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ internal Browser(
5656
TargetsMap = new ConcurrentDictionary<string, Target>();
5757
ScreenshotTaskQueue = new TaskQueue();
5858
DefaultContext = new BrowserContext(Connection, this, null);
59-
_contexts = contextIds.ToDictionary(
59+
_contexts = new ConcurrentDictionary<string, BrowserContext>(contextIds.ToDictionary(
6060
contextId => contextId,
61-
contextId => new BrowserContext(Connection, this, contextId));
62-
61+
contextId => new BrowserContext(Connection, this, contextId)));
6362
Connection.Disconnected += Connection_Disconnected;
6463
Connection.MessageReceived += Connect_MessageReceived;
6564

@@ -72,7 +71,7 @@ internal Browser(
7271

7372
internal IDictionary<string, Target> TargetsMap { get; }
7473

75-
private readonly Dictionary<string, BrowserContext> _contexts;
74+
private readonly ConcurrentDictionary<string, BrowserContext> _contexts;
7675
private readonly ILogger<Browser> _logger;
7776
private readonly Func<TargetInfo, bool> _targetFilterCallback;
7877
private Task _closeTask;
@@ -212,7 +211,7 @@ public async Task<BrowserContext> CreateIncognitoBrowserContextAsync()
212211
{
213212
var response = await Connection.SendAsync<CreateBrowserContextResponse>("Target.createBrowserContext", null).ConfigureAwait(false);
214213
var context = new BrowserContext(Connection, this, response.BrowserContextId);
215-
_contexts[response.BrowserContextId] = context;
214+
_contexts.TryAdd(response.BrowserContextId, context);
216215
return context;
217216
}
218217

@@ -403,7 +402,7 @@ internal async Task DisposeContextAsync(string contextId)
403402
{
404403
BrowserContextId = contextId
405404
}).ConfigureAwait(false);
406-
_contexts.Remove(contextId);
405+
_contexts.TryRemove(contextId, out var _);
407406
}
408407

409408
private async void Connection_Disconnected(object sender, EventArgs e)

0 commit comments

Comments
 (0)