[PG19] A canceled notify read leaves the NOTIFY queue stuck

Started by shihao zhongabout 1 month ago8 messageshackers
Jump to latest
#1shihao zhong
zhong950419@gmail.com

Hi,

If a listener fails while taking its snapshot in
asyncQueueReadAllNotifications(), its isAdvancing flag stays set. After
that no NOTIFY wakes or advances it, so the queue stops shrinking and
fills up. This is new in 19, from 282b1cde9de.

A cancel while the listener waits for a safe snapshot is enough:

L listens, W holds a serializable transaction, N sends notifications

set default_transaction_isolation = serializable; -- L
set default_transaction_read_only = on; -- L
set default_transaction_deferrable = on; -- L
listen cfg; -- L
begin isolation level serializable; select 1; -- W
begin isolation level read committed read write not deferrable; -- L
notify cfg; -- N
commit; -- L, waits on SafeSnapshot, cancel it
rollback; -- W
select pg_notify('other', repeat('x', 7000)); -- N, 16 times

The attached patch clears the flag on abort.

This is the third low item in my list [1]/messages/by-id/CAGRkXqSFdjqZP_kwRz7dv6LHX-CSrkDnynNXgntRsCEnQTKxmg@mail.gmail.com.

[1]: /messages/by-id/CAGRkXqSFdjqZP_kwRz7dv6LHX-CSrkDnynNXgntRsCEnQTKxmg@mail.gmail.com
/messages/by-id/CAGRkXqSFdjqZP_kwRz7dv6LHX-CSrkDnynNXgntRsCEnQTKxmg@mail.gmail.com

Thanks,
Shihao

Attachments:

t254258_1
v1-0001-Clear-isAdvancing-when-a-notify-read-is-aborted.patchapplication/octet-stream; name=v1-0001-Clear-isAdvancing-when-a-notify-read-is-aborted.patchDownload+12-1
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: shihao zhong (#1)
Re: [PG19] A canceled notify read leaves the NOTIFY queue stuck

shihao zhong <zhong950419@gmail.com> writes:

If a listener fails while taking its snapshot in
asyncQueueReadAllNotifications(), its isAdvancing flag stays set. After
that no NOTIFY wakes or advances it, so the queue stops shrinking and
fills up. This is new in 19, from 282b1cde9de.

Ooops.

The attached patch clears the flag on abort.

Playing around with this example, it avoids the queue-filling problem,
but there's still an undesirable aspect: the N process has sent the L
process a NOTIFY, but the L process does not see that, at least not
till another NOTIFY is sent (and then it reports both at once).
That's because the L process already cleared notifyInterruptPending,
so it won't re-enter ProcessIncomingNotify. This is measurably worse
than pre-v19: before, L would get the notify message when it next did
something, even without another NOTIFY. (I'm not immediately sure
why that changed.) The old behavior is still not great though;
ideally it'd report the notify right after finishing the aborted
COMMIT.

I tried to fix this by re-setting notifyInterruptPending in the new
block of code, but that's totally disastrous in the given example.
Having gotten out of the transaction, we immediately try to do
ProcessIncomingNotify again, and again we block in
GetTransactionSnapshot(). The user-visible effect is that you cannot
cancel the L process' COMMIT, even with multiple control-Cs, until the
W process commits or aborts. There might be other reasons why doing
this would turn into an endless failure loop, so on the whole that's
probably a bad idea. But I'd like to understand why v18 didn't
require receipt of a new NOTIFY before reporting the pending one.

Also, this business about GetTransactionSnapshot possibly blocking is
something that nobody foresaw when the async.c code was written.
I wonder if we should modify async.c to avoid it. Do we really need a
serializable snapshot for reading the notify queue? If we do, can we
acquire it before making any important state changes?

Anyway, the proposed patch is clearly an improvement so I'll go
ahead and push it. But we seem to have more to do here.

regards, tom lane

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#2)
Re: [PG19] A canceled notify read leaves the NOTIFY queue stuck

I wrote:

Playing around with this example, it avoids the queue-filling problem,
but there's still an undesirable aspect: the N process has sent the L
process a NOTIFY, but the L process does not see that, at least not
till another NOTIFY is sent (and then it reports both at once).
That's because the L process already cleared notifyInterruptPending,
so it won't re-enter ProcessIncomingNotify. This is measurably worse
than pre-v19: before, L would get the notify message when it next did
something, even without another NOTIFY. (I'm not immediately sure
why that changed.)

Oh, I must have fat-fingered the test case somehow, because when
I try this again, v18 behaves like HEAD. So never mind that bit.

I'm still interested in whether we can avoid blocking on a safe
snapshot, but that's clearly material for future development.
So we can close this open item for v19.

regards, tom lane

#4shihao zhong
zhong950419@gmail.com
In reply to: Tom Lane (#3)
Re: [PG19] A canceled notify read leaves the NOTIFY queue stuck

Hi Tom,

Thanks for committing that. Let's close the open item for v19.

I'm still interested in whether we can avoid blocking on a safe
snapshot, but that's clearly material for future development.

I can spend some time tonight to see what we can do about it.

Looks like I owe you quite a few patches, varno0, the WIN1252
validator, and this one. I will get them sorted out for you soon.

Thanks,
Shihao

#5shihao zhong
zhong950419@gmail.com
In reply to: shihao zhong (#4)
Re: [PG19] A canceled notify read leaves the NOTIFY queue stuck

Hi Tom,

Do we really need a serializable snapshot for reading the notify
queue? If we do, can we acquire it before making any important state
changes?

I did some investigation today and I don't think we need a serializable
snapshot.

If I understand correctly, the read only uses the snapshot to ask
XidInMVCCSnapshot() whether the sender is still running. But
GetLatestSnapshot() sets up the transaction snapshot when there is none
yet.
In a serializable read only deferrable transaction that waits for a safe
snapshot.

So we just need to call GetSnapshotData(), but that needs a snapshot.
I created a static one in async.c because feels like we should not
touch snapmgr for encapsulation. If you'd rather keep this in
snapmgr.c, a small wrapper there would work too.

Thanks,
Shihao

Attachments:

t254258_5
v2-0001-Don-t-wait-for-a-safe-snapshot-when-reading-the-n.patchapplication/octet-stream; name=v2-0001-Don-t-wait-for-a-safe-snapshot-when-reading-the-n.patchDownload+9-2
#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: shihao zhong (#5)
Re: [PG19] A canceled notify read leaves the NOTIFY queue stuck

shihao zhong <zhong950419@gmail.com> writes:

Do we really need a serializable snapshot for reading the notify
queue? If we do, can we acquire it before making any important state
changes?

I did some investigation today and I don't think we need a serializable
snapshot.

I looked at this, and I agree: all we care about in async.c is which
transactions are still running, and there's no particular need to wait
for a safe snapshot before deciding that. (A different way to say
that, perhaps, is that NOTIFY messages are delivered on the same basis
as READ COMMITTED operations, even if your default transaction mode
is higher.)

So we just need to call GetSnapshotData(), but that needs a snapshot.
I created a static one in async.c because feels like we should not
touch snapmgr for encapsulation. If you'd rather keep this in
snapmgr.c, a small wrapper there would work too.

I don't like that choice at all. The only callers of
GetSnapshotData() are in snapmgr.c and predicate.c, and there are
enough weird optimization-driven assumptions around snapshot
management that I think any new caller had better be in one of those
modules so that people working in that area will notice it. I wrote
a wrapper that documents two of the assumptions I think are involved
here, and there could well be more. See what you think.

regards, tom lane

Attachments:

t254258_6
v3-0001-Don-t-wait-for-a-safe-snapshot-when-reading-the-n.patchtext/x-diff; charset=us-ascii; name*0=v3-0001-Don-t-wait-for-a-safe-snapshot-when-reading-the-n.p; name*1=atchDownload+37-1
#7shihao zhong
zhong950419@gmail.com
In reply to: Tom Lane (#6)
Re: [PG19] A canceled notify read leaves the NOTIFY queue stuck

Hi Tom,

I don't like that choice at all. The only callers of
GetSnapshotData() are in snapmgr.c and predicate.c, and there are
enough weird optimization-driven assumptions around snapshot
management that I think any new caller had better be in one of those
modules so that people working in that area will notice it.

It seems we had different understandings of encapsulation. After some
careful thought and looking through some previous commits in snapmgr.c
(with Claude), I agree that centralizing snapshot management is better than
having every caller maintain its own snapshot object.

So I think we should go with your patch.

Thanks,
Shihao

#8Tom Lane
tgl@sss.pgh.pa.us
In reply to: shihao zhong (#7)
Re: [PG19] A canceled notify read leaves the NOTIFY queue stuck

shihao zhong <zhong950419@gmail.com> writes:

So I think we should go with your patch.

Done that way, then.

regards, tom lane