Skip to content

Fixed possible deadlock when using ExecuteNonQueryAsync - #3666

Merged
vonzshik merged 1 commit into
npgsql:hotfix/4.1.10from
rhintersteininger:hotfix/4.1.10
Apr 29, 2021
Merged

vonzshik merged 1 commit into
npgsql:hotfix/4.1.10from
rhintersteininger:hotfix/4.1.10

Conversation

@rhintersteininger

Copy link
Copy Markdown

The async parameter was not passed to the Close Method of the NpgsqlDataReader. This can cause a deadlock in the Cleanup Method of the DataReader because it is actively waiting for the completion of the "_sendTask".

I have tested this with Npgsql 4.1.9, PostgreSQL 10.3

@rhintersteininger
rhintersteininger requested a review from roji as a code owner April 15, 2021 14:00
@vonzshik

Copy link
Copy Markdown
Contributor

That doesn't make a lot of sense - why _sendTask will not be completed by this point, and how exactly making it async will fix it?

@roji

roji commented Apr 15, 2021

Copy link
Copy Markdown
Member

Note also that the reader is already consumed, since the previous line calls NextResult until it returns false. Unless I'm mistaken, that pretty much guarantees that no I/O will actually happen from within the Close (haven't verified this).

But this change isn't necessarily a bad idea either...

@vonzshik

Copy link
Copy Markdown
Contributor

Unless I'm mistaken, that pretty much guarantees that no I/O will actually happen from within the Close (haven't verified this).

There will be no IO, as by this point we've already send every single query and read every single response.

Also, note that this part was removed in 5.0.

@roji

roji commented Apr 15, 2021

Copy link
Copy Markdown
Member

Also, note that this part was removed in 5.0.

Yep, though note that Dispose could have the same problem (in other contexts, not here). In other words, we need to be careful when doing using in a methods which are both sync and async.

@rhintersteininger

Copy link
Copy Markdown
Author

I have tracked down the issue a little further: The main reason I had a deadlock was because I used a custom LimitedConcurrencyLevelTaskScheduler with a maximum degree of parallelism of 1.
The _sendTask was sometimes not finished yet (it was awaiting await connector.Flush(async);) and so the _sendTask.GetAwaiter().GetResult(); blocked this Thread. As the SynchronisationContext was set to null all continuations of the _sendTask are scheduled with TaskScheduler.Current which in my case was the custom scheduler. This continuations could not get executed because the TaskScheduler can only execute 1 Task at a time and in this case it was waiting for the continuation -> deadlock.
Using TaskScheduler.Default would be an idea but it would not completely solve the problem. The default TaskScheduler also schedules the Task onto the local Queue of the current Thread. This Thread is then waiting for the continuation which was scheduled onto the local queue of itself which would be a deadlock. In fact it is a deadlock if all other ThreadPool Threads are having work to do because then they can not steal the work from the other Thread.

Making reader.Close(); async would fix the Problem because then the Thread is not blocked with waiting for the _sendTask and can execute the continuations of it itself.

Note: I am not 100% sure if my explanation is correct. I am not an expert.

I have attached a little Program to reproduce the deadlock. It basically just tries to execute as many Update Sqls as possible at the same time. At work it only took me 100 requests where about 5-10 deadlocked. At home I needed 1000 requests.

NpgsqlDeadlock.zip

@vonzshik

Copy link
Copy Markdown
Contributor

@rhintersteininger ah, I see - now that makes total sense. In your case, the FlushAsync did complete - it's just there were no threads on a thread pool to complete the continuation after it.

In this case, I do agree with this fix - although, we would have to be a little bit creative for 5.0 (and 6.0). Also, we have to check other places for the same problem (I'm going to do this later).

@roji

roji commented Apr 16, 2021

Copy link
Copy Markdown
Member

Note #1130 which is also a case of a custom TaskScheduler causing issues (in Orleans).

@vonzshik
vonzshik merged commit ec62555 into npgsql:hotfix/4.1.10 Apr 29, 2021
@roji roji linked an issue Apr 29, 2021 that may be closed by this pull request
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.

Pass an async flag to the NpgsqlDataReader.Close in ExecuteNonQuery

3 participants