Skip to content

Fix race condition in SingleThreadSynchronizationContext#4468

Merged
vonzshik merged 6 commits into
mainfrom
4465-sync-context-concurrency-fix
Jun 19, 2022
Merged

Fix race condition in SingleThreadSynchronizationContext#4468
vonzshik merged 6 commits into
mainfrom
4465-sync-context-concurrency-fix

Conversation

@vonzshik

Copy link
Copy Markdown
Contributor

Fixes #4465

I think it should be fine...

@vonzshik vonzshik requested a review from roji as a code owner May 18, 2022 16:08

@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.

Apart from the exception flow, LGTM - I can't see a problem. Though seeing the complexity here - for code which isn't supposed to be perf-critical - I tend to agree with @NinoFloris's offline comment that we could sacrifice extreme perf here and go with a locking approach in order to have simpler/safer code. If needed I think I'd also be OK with just keeping the thread around (i.e. removing the timeout), which would obviously simplify this a lot.

But am also OK with keeping this lock-free code if we really want to (after the exception flow is fixed).

@NinoFloris can we have your pair of eyes on this too?

Comment thread src/Npgsql/SingleThreadSynchronizationContext.cs Outdated
Comment thread src/Npgsql/SingleThreadSynchronizationContext.cs Outdated
Comment thread src/Npgsql/SingleThreadSynchronizationContext.cs Outdated
Comment thread src/Npgsql/SingleThreadSynchronizationContext.cs Outdated
Comment thread src/Npgsql/SingleThreadSynchronizationContext.cs Outdated
@vonzshik vonzshik requested review from NinoFloris and roji June 13, 2022 18:02
@vonzshik

Copy link
Copy Markdown
Contributor Author

@roji @NinoFloris thinking a bit more, I do agree that making this as simple as possible is a much better solution (we can always rewrite, if for some reason it's going to be a bottleneck). I changed it to a lock, should be a bit more 'stable' overall.

@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.

Thanks for simplifying - this looks great and far less magical/tricky :)

Looks safe to me - see minor comments. @NinoFloris can you give this a review too please?

Comment thread src/Npgsql/SingleThreadSynchronizationContext.cs Outdated
Comment thread src/Npgsql/SingleThreadSynchronizationContext.cs Outdated
Comment thread src/Npgsql/SingleThreadSynchronizationContext.cs Outdated

@NinoFloris NinoFloris 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.

Modulo the feedback by @roji LGTM!

@vonzshik vonzshik enabled auto-merge (squash) June 19, 2022 11:58
@vonzshik vonzshik merged commit 4e7f2a7 into main Jun 19, 2022
@vonzshik vonzshik deleted the 4465-sync-context-concurrency-fix branch June 19, 2022 12:05
vonzshik added a commit that referenced this pull request Jun 19, 2022
vonzshik added a commit that referenced this pull request Jun 19, 2022
vonzshik added a commit that referenced this pull request Jun 19, 2022
vonzshik added a commit that referenced this pull request Jun 19, 2022
@vonzshik

vonzshik commented Jun 19, 2022

Copy link
Copy Markdown
Contributor Author

Backported to 6.0.5 via d50f836, 5.0.14 via 2ae3e3a, 4.1.12 via 94aa6a4, 4.0.13 via 3af871d.

vonzshik added a commit that referenced this pull request Jun 19, 2022
vonzshik added a commit that referenced this pull request Jun 19, 2022
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.

Deadlock/hung at NpgsqlDataReader.Cleanup

3 participants