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

Single lookups into concurrent dictionary #2231

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions lib/PuppeteerSharp/ChromeTargetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,8 @@ public async Task InitializeAsync()

public void AddTargetInterceptor(CDPSession session, TargetInterceptor interceptor)
{
lock (_targetInterceptors)
{
_targetInterceptors.TryGetValue(session, out var interceptors);

if (interceptors == null)
{
interceptors = new List<TargetInterceptor>();
_targetInterceptors.TryAdd(session, interceptors);
}

interceptors.Add(interceptor);
}
var interceptors = _targetInterceptors.GetOrAdd(session, static _ => new());
interceptors.Add(interceptor);
Comment on lines -92 to +93
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kblok Do you recall if the lock in AddTargetInterceptor intended to lock both the usage of _targetInterceptors and adding an element to interceptors?

A colleague of mine pointed out that I might have introduced a race-condition here
If two threads get the same interceptors out of _targetInterceptors and concurrently calls the thread-unsafe List<T>.Add.

On the other hand the other usages of interceptors are also not locked

  • RemoveTargetInterceptor removes an item from the list
  • OnAttachedToTarget enumerates the list

Copy link
Member

Choose a reason for hiding this comment

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

A colleague of mine pointed out that I might have introduced a race condition here

Is that a theory or a suspicion? We are talking about multiple threads, but I don't see how those events coming from the browser could be racy.

But yeah. The goal of the lock was to make those two actions thread-safe.
We could restore the lock. We could also turn the list into a ConcurrentSet.

On the other hand the other usages of interceptors are also not locked

  • RemoveTargetInterceptor removes an item from the list

Again, I don't see how we can get a race in RemoveTargetInterceptor

OnAttachedToTarget enumerates the list

I can see the need for a lock there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A colleague of mine pointed out that I might have introduced a race condition here

Is that a theory or a suspicion? We are talking about multiple threads, but I don't see how those events coming from the browser could be racy.

Before this PR the non-thread safe interceptors.Add(interceptor); was inside a lock and now it isn't.
I didn't notice that slight change of semantics when I created the PR, that's why I'm bringing it to your attention.
We haven't experienced that any problems that I suspect stem from this PR, so for now it's a theoretical race-condition.
I assume that's what you meant by "theory or a suspicion"?

Copy link
Member

Choose a reason for hiding this comment

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

@jnyrup yes. I was curious whether you did got a race condition or you found it could be a problem.
I think we could make it a ConcurrentSet just in case.

}

public void RemoveTargetInterceptor(CDPSession session, TargetInterceptor interceptor)
Expand Down
14 changes: 2 additions & 12 deletions lib/PuppeteerSharp/FirefoxTargetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,8 @@ public FirefoxTargetManager(

public void AddTargetInterceptor(CDPSession session, TargetInterceptor interceptor)
{
lock (_targetInterceptors)
{
_targetInterceptors.TryGetValue(session, out var interceptors);

if (interceptors == null)
{
interceptors = new List<TargetInterceptor>();
_targetInterceptors.TryAdd(session, interceptors);
}

interceptors.Add(interceptor);
}
var interceptors = _targetInterceptors.GetOrAdd(session, static _ => new());
interceptors.Add(interceptor);
}

public void RemoveTargetInterceptor(CDPSession session, TargetInterceptor interceptor)
Expand Down
6 changes: 2 additions & 4 deletions lib/PuppeteerSharp/FrameTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ internal Task<Frame> WaitForFrameAsync(string frameId)
}

var deferred = new TaskCompletionSource<Frame>(TaskCreationOptions.RunContinuationsAsynchronously);
_waitRequests.TryAdd(frameId, new List<TaskCompletionSource<Frame>>());
_waitRequests.TryGetValue(frameId, out var callbacks);
var callbacks = _waitRequests.GetOrAdd(frameId, static _ => new());
callbacks.Add(deferred);

return deferred.Task;
Expand All @@ -58,8 +57,7 @@ internal void AddFrame(Frame frame)
{
_parentIds.TryAdd(frame.Id, frame.ParentId);

_childIds.TryAdd(frame.ParentId, new List<string>());
_childIds.TryGetValue(frame.ParentId, out var childIds);
var childIds = _childIds.GetOrAdd(frame.ParentId, static _ => new());
childIds.Add(frame.Id);
}
else
Expand Down