Re: psql single-step mode woes

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Rui Zhao <zhaorui126(at)gmail(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>, Jelte Fennema-Nio <postgres(at)jeltef(dot)nl>
Subject: Re: psql single-step mode woes
Date: 2026-12-01 15:29:42
Message-ID: 7b8694b1-5186-422d-931a-b782515a6884@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 18/10/2026 13:01, Rui Zhao wrote:
> 1. Ctrl-C in a later \prompt can crash psql.
>
> I used the prompt.sql case from t/042_singlestep.pl, containing one
> line:
>
> SELECT 111; \prompt 'ANSWER: ' answer
>
> Then, in an interactive psql session:
>
> \set SINGLESTEP on
> \i prompt.sql
>
> I pressed Enter at the single-step confirmation to execute SELECT 111,
> then Ctrl-C at the ANSWER: input requested by \prompt. With your
> original patch, psql aborts with:
>
> double free or corruption (out)
>
> Without your patch, the same sequence cancels the script and leaves the
> interactive psql session ready for another command.
>
> The reason is that MainLoop() calls singlestep_verify() through
> SendQuery(). MainLoop() and singlestep_verify() both call
> sigsetjmp(sigint_interrupt_jmp, 1), using the same global variable.
> singlestep_verify() overwrites MainLoop()'s saved context and leaves its
> own context in the buffer when it returns.
>
> MainLoop() is still processing the same input line, so it has not
> reached the next iteration's sigsetjmp(). The following \prompt calls
> gets_fromFile(stdin), which sets sigint_interrupt_enabled to true. On
> Ctrl-C, psql_cancel_callback() calls siglongjmp() using the context
> saved by singlestep_verify(), but that function has already returned. In
> GDB, psql aborts at pg_free(first_prompt) in singlestep_verify().
>
> I initially fixed only singlestep_verify(), using a local sigjmp_buf
> variable and a global pointer to select the sigjmp_buf for siglongjmp().
> The function saves the previous pointer and restores it before
> returning. That fixed this case. When reviewing the other calls to
> sigsetjmp() in src/bin/psql/, I noticed that nested MainLoop() calls
> also use the same global variable. To check this on unpatched master, I
> used an interactive session with SINGLESTEP off. This is the nested-script
> case in t/040_interrupt.pl: inner.sql contains SELECT 111;, and outer.sql
> has this single line:
>
> \i inner.sql \prompt 'OUTER: ' answer
>
> I ran \i outer.sql, then pressed Ctrl-C at OUTER:.
>
> Both MainLoop() calls use sigsetjmp(sigint_interrupt_jmp, 1). The inner
> call overwrites the outer call's saved context and then returns. Ctrl-C
> at OUTER: makes siglongjmp() use the inner call's context after that
> call has returned, and psql exits unexpectedly.

Good find; thanks for the analysis.

> 0001 replaces the global sigjmp_buf with local sigjmp_buf variables and
> a global PG_exception_stack pointer, as with nested PG_TRY() blocks.
> Each function saves the previous pointer, sets it to its local variable,
> and restores it before returning. The SIGINT callback calls
> siglongjmp(*PG_exception_stack, 1). A later Ctrl-C therefore uses the
> calling function's saved context, not the context of a function that has
> returned. This covers MainLoop(), handleCopyIn(), and the callers of
> simple_prompt_extended(). 0003 applies the same change to
> singlestep_verify().

Hmm, we're really only using longjmp to jump out of a few very specific
function calls. It's not used as a generic error handling mechanism
(like in the backend). I feel it would be more robust to push the
sigsetjmp() calls as close to the function calls that we want to jump
out of, instead of trying to ensure that the global stack is in the
correct state at all times. Concretely, I suggest that we add a
PromptInterruptContext argument to gets_interactive() and
gets_fromFile(), so that they handle cancellation similar to
simple_prompt_extended(). See attached.

The test feels a bit complicated, I think I'll leave that out of the
version I'll commit. But thanks for a concrete repro. I didn't look at
the other patches yet.

- Heikki

Attachment Content-Type Size
v2-0001-psql-Fix-crash-on-Ctrl-C-in-a-prompt-in-a-nested-.patch text/x-patch 20.7 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2026-12-01 15:46:23 Re: Should we add timing output to pg_upgrade?
Previous Message Greg Burd 2026-12-01 15:27:09 Re: nbtree: opt-in leaf key prefix compression