In PostgreSql, I wrote generic code to be notify on table update.
Doing so, I have to wait for the connection. Not waiting, I receive no notification from the Database. So I wait like this:
conn.Wait(Timeout.Infinite);
I could have used Wait without any parameter in a loop be I was afraid to miss some notifications by concurrency. For example when 2 notifications enter simultanously, when wait is called without parameter and is released by receiving the first notification, we can miss the second one (before returning in the wait state). So I used "Timeout.Infinite".
To stop th connection from another thread, I tried to close it with:
conn.Close()
But I receive an exception:
Npgsql.NpgsqlOperationInProgressException: 'The connection is already in state 'Waiting''
So the question is :
Is there any way to stop the connection from being in waiting state and meanwhile ensure to not loose any notifications when waiting?
Note: I think (only my opinion) it is missing a CancellationToken from the PostgreSql "Connection.Wait" function. We should be able to cancel the waiting either when waiting for the next change.
Use the
WaitAsyncfunction, which accepts aCancellationToken.Obviously this must be done in an
asyncfunction, which should never be called using.Waitor.Resultas you could deadlock. Instead do async all the way down.