From c69378be26cb6a41903e5e6d34f5aed99ca2f027 Mon Sep 17 00:00:00 2001 From: Jingtang Zhang Date: Mon, 16 Nov 2026 14:13:44 +0800 Subject: [PATCH v2 2/2] Avoid blocking on client output when reporting FATAL errors A backend that has decided to exit can still block while sending the fatal error to a client that is not reading. Try to send the error without waiting, then suppress further client output during exit cleanup. Preserve the existing suppression of messages generated during a write by skipping the nested enqueue before it can resize the send buffer. Reviewed-by: Andrey Borodin Reviewed-by: Zsolt Parragi --- src/backend/libpq/pqcomm.c | 3 ++ src/backend/utils/error/elog.c | 20 +++++++++++ src/test/modules/test_misc/t/005_timeouts.pl | 38 ++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c index 3704d121003..b8d3d439fcf 100644 --- a/src/backend/libpq/pqcomm.c +++ b/src/backend/libpq/pqcomm.c @@ -1524,6 +1524,9 @@ socket_putmessage_noblock(char msgtype, const char *s, size_t len) int res PG_USED_FOR_ASSERTS_ONLY; int required; + if (PqCommBusy) + return; + /* * Ensure we have enough space in the output buffer for the message header * as well as the message itself. diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index e215ed053ff..8b9c0da1dce 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -4128,6 +4128,26 @@ send_message_to_frontend(ErrorData *edata) pq_sendbyte(&msgbuf, '\0'); /* terminator */ + /* + * On a client socket, don't let a FATAL report delay process exit. + * Suppress further client output after the send attempt. + * + * Exclude parallel workers because they report errors through a + * shared-memory queue that requires blocking sends. + */ + if (edata->elevel == FATAL && MyProcPort != NULL) + { + pq_putmessage_noblock(msgbuf.cursor, msgbuf.data, msgbuf.len); + pfree(msgbuf.data); + + (void) pq_flush_if_writable(); + + if (whereToSendOutput == DestRemote) + whereToSendOutput = DestNone; + + return; + } + pq_endmessage(&msgbuf); } else diff --git a/src/test/modules/test_misc/t/005_timeouts.pl b/src/test/modules/test_misc/t/005_timeouts.pl index 79ff980f1d6..dfe55a92739 100644 --- a/src/test/modules/test_misc/t/005_timeouts.pl +++ b/src/test/modules/test_misc/t/005_timeouts.pl @@ -203,4 +203,42 @@ ok( $node->poll_query_until( 'transaction timeout terminates the backend during ClientRead'); $psql_session->{run}->finish; +# +# 6. Test that sending a FATAL error cannot block backend exit. +# +$psql_session = $node->background_psql('postgres'); +$pid = $psql_session->query_safe('SELECT pg_backend_pid()'); +$log_offset = -s $node->logfile; + +# Send a query in pipeline mode and leave its results unread. The dynamic +# query text makes the FATAL error context larger than the socket buffers. +# Turn off ON_ERROR_STOP for \flush, since psql treats PQflush's successful +# return of zero as a failure. +$psql_session->query_until( + qr/sleep_sent/, q[ + BEGIN; + SET LOCAL log_error_verbosity = 'terse'; + SET LOCAL transaction_timeout = '5s'; + \startpipeline + DO $$ + BEGIN + EXECUTE 'SELECT pg_sleep(60) /*' || repeat('x', 8 * 1024 * 1024) || '*/'; + END; + $$; + \set ON_ERROR_STOP off + \flush + \echo sleep_sent +]); +$node->wait_for_event('client backend', 'PgSleep'); + +$node->wait_for_log( + qr/\[$pid\].*FATAL:\s+terminating connection due to transaction timeout/, + $log_offset); +ok( $node->poll_query_until( + 'postgres', qq[ + SELECT NOT EXISTS (SELECT FROM pg_stat_activity WHERE pid = $pid); + ]), + 'backend exits even when the client does not read the FATAL error'); +$psql_session->{run}->kill_kill; + done_testing(); -- 2.43.7