pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feedRace conditions during parallel worker (unclean) exit
6+ messages / 2 participants
[nested] [flat]
* Race conditions during parallel worker (unclean) exit
@ 2026-09-08 15:38 Antonin Houska <ah@cybertec.at>
0 siblings, 1 reply; 6+ messages in thread
From: Antonin Houska @ 2026-09-08 15:38 UTC (permalink / raw)
To: pgsql-hackers@lists.postgresql.org
In [1] I noted that ERROR like "lost connection to ... worker" (currently
issued in ProcessParallelMessages()) might also be appropriate for REPACK
(CONCURRENTLY). While I'm still not sure about that, I suspect that something
is wrong about handling unclean exits in parallel.c.
According to the header comment, ParallelWorkerShutdown() should handle cases
like direct call of proc_exit(). By adding one like this
inline patch 1: src/backend/access/transam/parallel.c (+1/-0 lines) download inline-1.patch
diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c
index 17fcd246b0c..e8700ce8594 100644
--- a/src/backend/access/transam/parallel.c
+++ b/src/backend/access/transam/parallel.c
@@ -1385,6 +1385,7 @@ ParallelWorkerMain(Datum main_arg)
* Hooray! Primary initialization is complete. Now, we need to set up our
* backend-local state to match the original backend.
*/
+ proc_exit(1);
/*
* Join locking group. We must do this before anything that could try to
and by running the following
begin;
create table a(i int);
set parallel_setup_cost to 0;
set parallel_tuple_cost to 0;
set min_parallel_table_scan_size to 0;
set min_parallel_index_scan_size to 0;
set max_parallel_workers_per_gather to 1;
set parallel_leader_participation to off;
set debug_parallel_query to 'regress';
table a;
rollback;
I can reproduce the expected behavior:
ERROR: lost connection to parallel worker
However, an additional sleep() call in ParallelWorkerShutdown()
inline patch 2: src/backend/access/transam/parallel.c (+1/-1 lines) download inline-2.patch
diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c
index 17fcd246b0c..1a05f16e0bc 100644
--- a/src/backend/access/transam/parallel.c
+++ b/src/backend/access/transam/parallel.c
@@ -1618,7 +1618,7 @@ ParallelWorkerShutdown(int code, Datum arg)
SendProcSignal(ParallelLeaderPid,
PROCSIG_PARALLEL_MESSAGE,
ParallelLeaderProcNumber);
-
+ sleep(1);
dsm_detach((dsm_segment *) DatumGetPointer(arg));
}
appears to cause race conditions. What I see is that the leader process runs
ProcessParallelMessages() while the worker is still attached to the error
queue, so the leader gets SHM_MQ_WOULD_BLOCK when trying to read from the
queue. Thus the ERROR is not raised.
Moreover, the leader, after having seen the worker detached from the tuple
queue (TupleQueueReaderNext) a bit later, starts executor cleanup and, as the
worker does not send signals anymore, it gets stuck in
WaitForParallelWorkersToExit().
Attached is what might be a fix, but I'm not sure if the Terminate message is
appropriate even if the worker in fact didn't finish with success.
[1] https://www.postgresql.org/message-id/32123.1788806780%40localhost
--
Antonin Houska
Web: https://www.cybertec-postgresql.com
Patches and attachments: download all (3 files, .tar.gz)
fix_parallel_worker_unclean_exit.diff (text/x-diff, 1.2K) download
diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c
index 17fcd246b0c..b4802623b80 100644
--- a/src/backend/access/transam/parallel.c
+++ b/src/backend/access/transam/parallel.c
@@ -1597,10 +1598,9 @@ ParallelWorkerReportLastRecEnd(XLogRecPtr last_xlog_end)
}
/*
- * Make sure the leader tries to read from our error queue one more time.
- * This guards against the case where we exit uncleanly without sending an
- * ErrorResponse to the leader, for example because some code calls proc_exit
- * directly.
+ * Make sure the leader knows we're done. This guards against the case where
+ * we exit uncleanly without sending an ErrorResponse to the leader, for
+ * example because some code calls proc_exit directly.
*
* Also explicitly detach from dsm segment so that subsystems using
* on_dsm_detach() have a chance to send stats before the stats subsystem is
@@ -1615,6 +1615,8 @@ ParallelWorkerReportLastRecEnd(XLogRecPtr last_xlog_end)
static void
ParallelWorkerShutdown(int code, Datum arg)
{
+ pq_putmessage(PqMsg_Terminate, NULL, 0);
+
SendProcSignal(ParallelLeaderPid,
PROCSIG_PARALLEL_MESSAGE,
ParallelLeaderProcNumber);
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Race conditions during parallel worker (unclean) exit
@ 2026-09-08 18:36 Sami Imseih <samimseih.pg@gmail.com>
parent: Antonin Houska <ah@cybertec.at>
0 siblings, 1 reply; 6+ messages in thread
From: Sami Imseih @ 2026-09-08 18:36 UTC (permalink / raw)
To: Antonin Houska <ah@cybertec.at>; +Cc: pgsql-hackers@lists.postgresql.org
Hi Antonin,
> Moreover, the leader, after having seen the worker detached from the tuple
> queue (TupleQueueReaderNext) a bit later, starts executor cleanup and, as the
> worker does not send signals anymore, it gets stuck in
> WaitForParallelWorkersToExit().
There have been reports of leader hangs in parallel waits [1], but the one
I found was fixed by not entering parallel mode while interrupts were being
held.
That said, there may still be a good argument here to make the
abnormal-exit path
of a parallel worker more explicit, which your "sleep(1)" case demonstrates.
> Attached is what might be a fix, but I'm not sure if the Terminate message is
> appropriate even if the worker in fact didn't finish with success.
Right. I don't think "PqMsg_Terminate" is the right message here. It currently
represents normal completion:
```
/* Report success. */
pq_putmessage(PqMsg_Terminate, NULL, 0);
```
It seems cleaner to introduce a separate message for this case, maybe
"PqMsg_ParallelAborted", and keep the existing
"ERROR: lost connection to parallel worker" message.
[1] https://www.postgresql.org/message-id/20240920183931.f0.nmisch@google.com
--
Sami Imseih
Amazon Web Services (AWS)
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Race conditions during parallel worker (unclean) exit
@ 2026-09-09 18:42 Antonin Houska <ah@cybertec.at>
parent: Sami Imseih <samimseih.pg@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: Antonin Houska @ 2026-09-09 18:42 UTC (permalink / raw)
To: Sami Imseih <samimseih.pg@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org
Sami Imseih <samimseih.pg@gmail.com> wrote:
> It seems cleaner to introduce a separate message for this case, maybe
> "PqMsg_ParallelAborted", and keep the existing
> "ERROR: lost connection to parallel worker" message.
I'm not sure this is worth adjusting the FE/BE protocol. I'd prefer regular
ERROR message (PqMsg_ErrorResponse), but only in some minimalistic form. The
worker would only send the message, w/o doing any error handling itself.
--
Antonin Houska
Web: https://www.cybertec-postgresql.com
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Race conditions during parallel worker (unclean) exit
@ 2026-09-09 21:38 Sami Imseih <samimseih.pg@gmail.com>
parent: Antonin Houska <ah@cybertec.at>
0 siblings, 1 reply; 6+ messages in thread
From: Sami Imseih @ 2026-09-09 21:38 UTC (permalink / raw)
To: Antonin Houska <ah@cybertec.at>; +Cc: pgsql-hackers@lists.postgresql.org
> > It seems cleaner to introduce a separate message for this case, maybe
> > "PqMsg_ParallelAborted", and keep the existing
> > "ERROR: lost connection to parallel worker" message.
>
> I'm not sure this is worth adjusting the FE/BE protocol. I'd prefer regular
> ERROR message (PqMsg_ErrorResponse), but only in some minimalistic form. The
> worker would only send the message, w/o doing any error handling itself.
Perhaps PqMsg_ErrorResponse could work, although I am not clear what a
minimalistic
form means here. I was thinking of PqMsg_ParallelAborted because we
have precedent
already with PqMsg_Progress which was introduced in f1889729dd3a as
the 'P' message
for parallel index progress reported, but later converted to a macro
in a99cc6c6b4b.
(I think it was wrong to name it PqMsg_Progress, but that's besides
the point here).
--
Sami Imseih
Amazon Web Service (AWS)
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Race conditions during parallel worker (unclean) exit
@ 2026-09-10 12:54 Antonin Houska <ah@cybertec.at>
parent: Sami Imseih <samimseih.pg@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: Antonin Houska @ 2026-09-10 12:54 UTC (permalink / raw)
To: Sami Imseih <samimseih.pg@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org
Sami Imseih <samimseih.pg@gmail.com> wrote:
> > > It seems cleaner to introduce a separate message for this case, maybe
> > > "PqMsg_ParallelAborted", and keep the existing
> > > "ERROR: lost connection to parallel worker" message.
> >
> > I'm not sure this is worth adjusting the FE/BE protocol. I'd prefer regular
> > ERROR message (PqMsg_ErrorResponse), but only in some minimalistic form. The
> > worker would only send the message, w/o doing any error handling itself.
>
> I was thinking of PqMsg_ParallelAborted because we have precedent already
> with PqMsg_Progress which was introduced in f1889729dd3a as the 'P' message
> for parallel index progress reported, but later converted to a macro in
> a99cc6c6b4b.
I realize now that new message type for communication between background
worker and the leader backend actually shouldn't need the protocol version to
be bumped - see commit f1889729dd3a. However, when adding a new message type,
I'd use the opportunity to stop using PqMsg_Terminate in the background
worker: per documentation, this one should be sent by frontend to backend.
A new message, e.g. PqMsg_WorkerExit, would contain status code. The worker
would set zero status code when sending the message at the end of
ParallelWorkerMain(), and non-zero in ParallelWorkerShutdown(). Does that make
sense to you?
--
Antonin Houska
Web: https://www.cybertec-postgresql.com
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Race conditions during parallel worker (unclean) exit
@ 2026-09-10 21:09 Sami Imseih <samimseih.pg@gmail.com>
parent: Antonin Houska <ah@cybertec.at>
0 siblings, 0 replies; 6+ messages in thread
From: Sami Imseih @ 2026-09-10 21:09 UTC (permalink / raw)
To: Antonin Houska <ah@cybertec.at>; +Cc: pgsql-hackers@lists.postgresql.org
> > > > It seems cleaner to introduce a separate message for this case, maybe
> > > > "PqMsg_ParallelAborted", and keep the existing
> > > > "ERROR: lost connection to parallel worker" message.
> > >
> > > I'm not sure this is worth adjusting the FE/BE protocol. I'd prefer regular
> > > ERROR message (PqMsg_ErrorResponse), but only in some minimalistic form. The
> > > worker would only send the message, w/o doing any error handling itself.
> >
>
> > I was thinking of PqMsg_ParallelAborted because we have precedent already
> > with PqMsg_Progress which was introduced in f1889729dd3a as the 'P' message
> > for parallel index progress reported, but later converted to a macro in
> > a99cc6c6b4b.
>
> I realize now that new message type for communication between background
> worker and the leader backend actually shouldn't need the protocol version to
> be bumped - see commit f1889729dd3a.
Right.
> A new message, e.g. PqMsg_WorkerExit, would contain status code. The worker
> would set zero status code when sending the message at the end of
> ParallelWorkerMain(), and non-zero in ParallelWorkerShutdown(). Does that make
> sense to you?
That does sound logical to me. Maybe PqParallelMsg_WorkerExit to match the new
prefix in 8367b63e2cabb.
So, this also means we can be more precise in the error message. Rather than
"lost connection to parallel worker", we can report something like
"parallel worker exited with exit code <n>" when ProcessParallelMessage()
receives a worker exit message indicating that the worker exited
abnormally.
--
Sami Imseih
Amazon Web Services (AWS)
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2026-09-10 21:09 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-08 15:38 Race conditions during parallel worker (unclean) exit Antonin Houska <ah@cybertec.at>
2026-09-08 18:36 ` Sami Imseih <samimseih.pg@gmail.com>
2026-09-09 18:42 ` Antonin Houska <ah@cybertec.at>
2026-09-09 21:38 ` Sami Imseih <samimseih.pg@gmail.com>
2026-09-10 12:54 ` Antonin Houska <ah@cybertec.at>
2026-09-10 21:09 ` Sami Imseih <samimseih.pg@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox