Skip to content

Fix bug where multiple async notifications not delivered properly - #46

Closed
glenebob wants to merge 3 commits into
npgsql:masterfrom
glenebob:bugfix_multiple_notifications
Closed

glenebob wants to merge 3 commits into
npgsql:masterfrom
glenebob:bugfix_multiple_notifications

Conversation

@glenebob

Copy link
Copy Markdown
Contributor

Hi Francisco,

Here's what I did to fix the multiple async notification bug, for discussion.

It might actually be better to roll our own buffered stream, or do what we talked about with binary readers, where we can peek at the buffer. Then if we peek and there's data, go ahead and read notifications, else do the Poll call.

The way I did it here was quite simple, until I ran into the timeout bug.

-Glen

Use a short timeout after receiving a notification for the notification thread.
Add a hack/workaround for a bug in SslStream caused by Socket read timeout.
@ChenHuajun

Copy link
Copy Markdown
Contributor

Hi,glenebob

I am just interested in this issue.Please let me say my viewpoint.
I just wonder why you not implement an own buffered stream to resolve this probleam ,since you said it may be better.
In fact i known another probleam while using the System.IO.BufferedStream(*).
With the new buffered stream,we can known if there is any unreaded data left in the buffer,for example implement a hasUnreadedData() method.

Moreover, i propose that while the notification thread hold the lock, the calling of Socket.Poll() should return as soon as possible , so normal connection thread could has chance to gain the lock soon.
And notification thread should sleep() a while before regain the lock in the next loop,so notification thread will not consume CPU too much.

the pseudo code just like this:
while (true)
{
Thread.Sleep(0);
this.connector._notificationAutoResetEvent.WaitOne();
if (this.connector.Socket.Poll(100, SelectMode.SelectRead))
...
==>
while (true)
{
Thread.Sleep(20);
this.connector._notificationAutoResetEvent.WaitOne();
if ( this.connector.Stream.hasUnreadedData() || this.connector.Socket.Poll(0, SelectMode.SelectRead))
...

*) Occasionally Npgsql will throw “This stream does not support seek operations”exception.The reason is just like the following
http://stackoverflow.com/questions/3095640/why-would-bufferedstream-write-throw-this-stream-does-not-support-seek-operatio

@glenebob

Copy link
Copy Markdown
Contributor Author

Sorry, Francisco and I had a long discussion about this, unfortunately in private.

My comment about implementing our own stream was naive. It doesn't help, because the streams upon which it would rely don't all provide the ability to query for buffered data.

The correct fix is to have only one thread reading the network stream, but rearranging the code to work that way is quite involved. I haven't yet figured out a sensible path from here to there. Essentially, what is now the notification thread would become the One True socket reader. The user thread that calls Npgsql to execute queries would never read the socket, instead it would wait for events delivered by the reader thread. A nice side effect of this, is that we could support async query execution for almost free.

With the current architecture, the best way to fix the problem that I can see is the time out solution I came up with, but as you can see, that is very messy due to the timeout bug in SslStream.

As to your comment about Poll() and Sleep(), look at PR #61. The need for Sleep() is eliminated by using the proper thread synchronization mechanism for the problem. There's a comment there that explains a bit more.

-Glen

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.

2 participants