Skip to content

Made the rcts thread safe - #3251

Merged
roji merged 1 commit into
npgsql:mainfrom
vonzshik:resettable-cts-threadsafe-fix
Oct 21, 2020
Merged

roji merged 1 commit into
npgsql:mainfrom
vonzshik:resettable-cts-threadsafe-fix

Conversation

@vonzshik

Copy link
Copy Markdown
Contributor

Closes #3250

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What do you think about this perf-wise? It looks OK to me - this is a lock with very little contention. But wanting to make sure.

But any reason to not just write the new CTS to _cts (which is volatile) and then dispose the older one? That would simply remove the lock altogether.

@vonzshik

Copy link
Copy Markdown
Contributor Author

What do you think about this perf-wise? It looks OK to me - this is a lock with very little contention. But wanting to make sure.

You together with @YohDeadfall definitely know much more about performance then me 😄

But any reason to not just write the new CTS to _cts (which is volatile) and then dispose the older one? That would simply remove the lock altogether.

CancelAfter isn't threadsafe (and so is Dispose).

https://github.com/dotnet/corefx/blob/1b5b5f0bf030bf7250c3258c140fa9e4214325c8/src/Common/src/CoreLib/System/Threading/CancellationTokenSource.cs#L357-L360

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

CancelAfter isn't threadsafe (and so is Dispose).

Good point.

In any case, on the Start/Reset side the lock is only taken if the CTS expired (rare), and the Cancel/CancelAfter is only done when a cancellation is actually requested. So nothing here is on any hot path as far as I can tell.

@roji
roji merged commit 43dcbc3 into npgsql:main Oct 21, 2020
@vonzshik
vonzshik deleted the resettable-cts-threadsafe-fix branch November 2, 2020 14:00
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.

ResettableCancellationTokenSource.Cancel (and CancelAfter) aren't threadsafe

2 participants