pgpool: Fix segfault with CopyOut.

From: Tatsuo Ishii <ishii(at)postgresql(dot)org>
To: pgpool-committers(at)lists(dot)postgresql(dot)org
Subject: pgpool: Fix segfault with CopyOut.
Date: 2025-11-16 07:18:31
Message-ID: E1vKX1r-003Ln8-0x@gothos.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgpool-committers

Fix segfault with CopyOut.

When "COPY relname TO STDOUT" is executed in the extended query
protocol mode, pgpool segfaulted.

When read_kind_from_backend() reads a message from backend, it
extracts the corresponding entry from the pending message queue when
processing extended query protocol messages. However, if the head of
the message queue is an "execute" message, some of incoming message
types are exceptional because other than CommandComplete message
(which means the execute message finishes) may come from backend. This
includes DataRow, ErrorResponse, NoticeMessage. Unfortunately we
overlooked that 'H' (CopyOutResponse) is in the group too. Thus when
CopyOutResponse comes from backend, the execute pending message is
removed. If the next message from frontend is Sync (it's often
happens), read_kind_from_backend() sets session_context->query_context
to NULL, and calls pool_unset_query_in_progress(), which accesses
session_context->query_context and segfaults.

The fix is, to add CopyOutResponse to the exception list. Just in
case, we also add 'd' (CopyData) and 'c' (CopyDone) to the list. This
may not be actually necessary since CopyData and CopyDone are
processced in CopyDataRows() though.

Add regression test case to 126.copy_hang (master and v4.7) or
076.copy_hang (v4.6 or before).

Author: Tatsuo Ishii <ishii(at)postgresql(dot)org>
Reported-by: https://github.com/tetesh
Reviewed-by: Bo Peng <pengbo(at)sraoss(dot)co(dot)jp>
Discussion: https://github.com/pgpool/pgpool2/issues/133
Backpatch-through: v4.2

Branch
------
V4_7_STABLE

Details
-------
https://git.postgresql.org/gitweb?p=pgpool2.git;a=commitdiff;h=b113027dddd0f80b22fabc56af141b57ece6fc1d

Modified Files
--------------
src/protocol/pool_process_query.c | 11 ++++---
.../tests/126.copy_hang/copy-out-expected | 37 ++++++++++++++++++++++
.../tests/126.copy_hang/pgproto-copy-out.data | 9 ++++++
src/test/regression/tests/126.copy_hang/test.sh | 16 ++++++++++
4 files changed, 69 insertions(+), 4 deletions(-)

Browse pgpool-committers by date

  From Date Subject
Next Message Bo Peng 2025-11-18 00:00:52 pgpool: Prepare Release Note.
Previous Message Tatsuo Ishii 2025-11-16 07:18:12 pgpool: Fix segfault with CopyOut.