[PATCH] Fix LISTEN startup race with direct advancement

From: "Joel Jacobson" <joel(at)compiler(dot)org>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: [PATCH] Fix LISTEN startup race with direct advancement
Date: 2026-05-19 20:37:56
Message-ID: 9835b0a4-9121-47ac-9c44-427b8b1a7f1b@app.fastmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

I had another pass over the async.c rework committed in 282b1cd,
and found a race that can cause a notification committed after
the listener registered its queue position to be missed entirely.

This can happen in the small time window between PreCommit_Notify(),
where the first LISTEN registers the backend and records its queue
position, and AtCommit_Notify(), where the staged listen action is
made active in the shared channel map by setting listening = true.

If a concurrent NOTIFY commits in that window, SignalBackends() can
see the staged listener entry with listening = false and conclude that
the backend is not interested in the channel. With direct advancement,
that can move the backend's queue pointer past the notification instead
of waking it.

This is distinct from the documented LISTEN startup race in
listen.sgml. The documented race can produce false positives: after
LISTEN returns, an application may receive a notification for work
already observed by its initial database scan. That is harmless. This
race is a false negative: a notification can be missed entirely.

The fix is just to treat staged LISTEN entries as possible listeners
when deciding whom to wake:

```diff
- if (!listeners[j].listening)
- continue; /* ignore not-yet-committed listeners */
```

The attached patches split the report into tests and fix:

0001 Test missed LISTEN startup notification
0002 Test LISTEN startup notification for already-seen work
0003 Fix LISTEN startup race with direct advancement

/Joel

Attachment Content-Type Size
0001-Test-missed-LISTEN-startup-notification.patch application/octet-stream 5.4 KB
0003-Fix-LISTEN-startup-race-with-direct-advancement.patch application/octet-stream 6.5 KB
0002-Test-LISTEN-startup-notification-for-already-seen-wo.patch application/octet-stream 5.1 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2026-05-19 21:18:08 Re: PostgreSQL and OpenSSL 4.0.0
Previous Message Alexander Lakhin 2026-05-19 20:00:00 Re: Implement waiting for wal lsn replay: reloaded