pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
007_pre_auth.pl fails if injection_points_wakeup hits unintended process
5+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2026-11-03 590788ad0b 6059658609 a02de147ab  Improve stability of authentication test 007_pre_auth
* 007_pre_auth.pl fails if injection_points_wakeup hits unintended process
@ 2026-11-01 13:00  Alexander Lakhin <exclusion@gmail.com>
  0 siblings, 3 replies; 5+ messages in thread

From: Alexander Lakhin @ 2026-11-01 13:00 UTC (permalink / raw)
  To: pgsql-hackers; Michael Paquier <michael@paquier.xyz>

Hello hackers,

A recent failure of auth, pre_auth test produced by culicidae:
[19:20:02.147](0.003s) ok 1 - authenticating connections are recorded in pg_stat_activity
[19:20:02.147](0.000s) # issuing query 4 via background psql: SELECT injection_points_wakeup('init-pre-auth');
[19:20:02.151](0.004s) # results query 4:
# {
#   'stderr' => 'background_psql: QUERY_SEPARATOR 4:
# ',
#   'stdout' => '
# background_psql: QUERY_SEPARATOR 4:
# '
# }
[19:23:02.313](180.162s) # die: IPC::Run: timeout on timer #2 at /usr/share/perl5/IPC/Run.pm line 3361.
[19:23:02.319](0.006s) 1..1
IPC::Run: timeout on timer #2 at /usr/share/perl5/IPC/Run.pm line 3361.

indicates that the test is not bulletproof. I reproduced this locally when
running multiple tests in parallel, and with the following diagnostic
added:
@@ -753,3 +753,5 @@ InitPostgres(const char *in_dbname, Oid dboid,
                 pgstat_bestart_initial();
+elog(LOG, "!!!before injection_point init-pre-auth");
                 INJECTION_POINT("init-pre-auth", NULL);
+elog(LOG, "!!!after injection_point init-pre-auth");
         }
observed:
23      t/007_pre_auth.pl (Wstat: 7424 (exited 29) Tests: 1 Failed: 0)
23        Non-zero exit status: 29
23      Files=1, Tests=1, 236 wallclock secs ( 0.07 usr  0.02 sys + 0.55 cusr  2.25 csys =  2.89 CPU)
23      Result: FAIL
23      # die: IPC::Run: timeout on timer #2 at /usr/share/perl5/IPC/Run.pm line 3361.
23      # Looks like your test exited with 29 just after 1.
23      make: *** [Makefile:21: check] Error 1

2026-11-01 11:20:41.510 UTC [478942][client backend][2/0:0] LOG: !!!before injection_point init-pre-auth
2026-11-01 11:20:41.510 UTC [478942][client backend][2/0:0] LOG: !!!after injection_point init-pre-auth
2026-11-01 11:20:44.597 UTC [478942][client backend][2/2:0] LOG: statement: SELECT 
injection_points_attach('init-pre-auth', 'wait')
2026-11-01 11:20:45.539 UTC [479057][autovacuum worker][101/0:0] LOG:  !!!before injection_point init-pre-auth
2026-11-01 11:20:47.346 UTC [479096][client backend][3/0:0] LOG: !!!before injection_point init-pre-auth
2026-11-01 11:20:50.028 UTC [478942][client backend][2/11:0] LOG: statement: SELECT 
injection_points_wakeup('init-pre-auth');
2026-11-01 11:20:50.029 UTC [479057][autovacuum worker][101/0:0] LOG:  !!!after injection_point init-pre-auth
2026-11-01 11:21:13.777 UTC [479226][autovacuum worker][102/0:0] LOG:  !!!before injection_point init-pre-auth

or:
2026-11-01 07:19:21.187 UTC [426308][autovacuum launcher][100/0:0] LOG:  !!!before injection_point init-pre-auth
2026-11-01 07:19:21.187 UTC [426308][autovacuum launcher][100/0:0] LOG:  !!!after injection_point init-pre-auth
2026-11-01 07:19:21.244 UTC [426365][client backend][2/2:0] LOG: statement: SELECT 
injection_points_attach('init-pre-auth', 'wait')
2026-11-01 07:19:21.612 UTC [426309][logical replication launcher][118/0:0] LOG:  !!!before injection_point init-pre-auth
2026-11-01 07:19:23.750 UTC [426479][client backend][3/0:0] LOG: !!!before injection_point init-pre-auth
2026-11-01 07:19:25.194 UTC [426365][client backend][2/5:0] LOG: statement: SELECT injection_points_wakeup('init-pre-auth');
2026-11-01 07:19:25.232 UTC [426309][logical replication launcher][118/0:0] LOG:  !!!after injection_point init-pre-auth
2026-11-01 07:19:52.568 UTC [427022][autovacuum worker][101/0:0] LOG:  !!!before injection_point init-pre-auth

That is, the wakeup can hit autovacuum worker or logical replication
launcher, not the intended client backend. This test failure can be easily
reproduced with:
@@ -753,2 +753,3 @@ InitPostgres(const char *in_dbname, Oid dboid,
          pgstat_bestart_initial();
+if (AmRegularBackendProcess()) pg_usleep(1000000);
          INJECTION_POINT("init-pre-auth", NULL);

and
echo "autovacuum_naptime = 1s" >/tmp/temp.config
TEMP_CONFIG=/tmp/temp.config PROVE_TESTS="t/007*" make -s check -C src/test/authentication

Reproduced starting from e2080261c.

[1] https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=culicidae&dt=2026-10-30%2018%3A10%3A12

Best regards,
Alexander






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: 007_pre_auth.pl fails if injection_points_wakeup hits unintended process
@ 2026-11-01 18:21  Andrey Borodin <x4mmm@yandex-team.ru>
  parent: Alexander Lakhin <exclusion@gmail.com>
  2 siblings, 0 replies; 5+ messages in thread

From: Andrey Borodin @ 2026-11-01 18:21 UTC (permalink / raw)
  To: Alexander Lakhin <exclusion@gmail.com>; +Cc: pgsql-hackers; Michael Paquier <michael@paquier.xyz>

Hi Alexander,

On 1 Nov 2026, Alexander Lakhin wrote:
> the wakeup can hit autovacuum worker or logical replication
> launcher, not the intended client backend.

For a small fix, I'd propose restricting "init-pre-auth" to
AmRegularBackendProcess().


/* Advertising of a related patch starts here */

I also prepared a variant using the filesystem-controlled wait points
from [0]. Attached are two patches: 0001 rebases that interface onto
current master, and 0002 uses it in 007_pre_auth.pl.

The test still attaches the point through SQL and finds the client's
PID in pg_stat_activity. It then waits for
pg_injection_points/init-pre-auth/<pid> and removes that file to release
only the intended client. Waiting for the file matters because the
wait event is reported before the marker is created.

The test also parks another session at the same point before starting
the client, and checks that it remains blocked after the client reaches
idle. This makes the multiple-waiter case deterministic.

Thank you!


Best regards, Andrey Borodin.

[0] https://www.postgresql.org/message-id/AE31E3B7-A21B-4C1C-B75B-8D542A9DC633%40yandex-team.ru

Attachments: download all (2 files, .tar.gz)

v1-0001-Allow-filesystem-control-of-wait-injection-points.patch (application/octet-stream, 19.8K) download From a2e265ccf08a7bfda295a2339272eef8c3038d63 Mon Sep 17 00:00:00 2001 From: Andrey Borodin <amborodin@acm.org> Date: Sun, 1 Nov 2026 20:42:19 +0400 Subject: [PATCH v1 1/2] Allow filesystem control of wait injection points. Creating pg_injection_points/<point>/ before shared-memory initialization attaches a wait point. Each waiter publishes a PID file in that directory, and removing the file releases that process. A running server can also attach the point through SQL after the controller creates the directory. Clear stale waiter files when shared memory is recreated and remove live markers on normal release or error. SQL-only waits leave no filesystem state. Add coverage for startup attachment, file-based release, cleanup, and coexistence with the SQL interface. Discussion: https://postgr.es/m/AE31E3B7-A21B-4C1C-B75B-8D542A9DC633@yandex-team.ru --- src/test/modules/injection_points/Makefile | 2 + .../injection_points/injection_points.c | 310 +++++++++++++++++- src/test/modules/injection_points/meson.build | 8 + .../t/001_wait_without_sql.pl | 127 +++++++ 4 files changed, 441 insertions(+), 6 deletions(-) create mode 100644 src/test/modules/injection_points/t/001_wait_without_sql.pl diff --git a/src/test/modules/injection_points/Makefile b/src/test/modules/injection_points/Makefile index 8e2dfaa2384..2a3aa19b86e 100644 --- a/src/test/modules/injection_points/Makefile +++ b/src/test/modules/injection_points/Makefile @@ -42,6 +42,8 @@ ISOLATION = basic \ # some isolation tests require wal_level=replica ISOLATION_OPTS = --temp-config $(top_srcdir)/src/test/modules/injection_points/extra.conf +TAP_TESTS = 1 + # The injection points are cluster-wide, so disable installcheck NO_INSTALLCHECK = 1 diff --git a/src/test/modules/injection_points/injection_points.c b/src/test/modules/injection_points/injection_points.c index 5e1bbc2f5c9..1cc65c285ae 100644 --- a/src/test/modules/injection_points/injection_points.c +++ b/src/test/modules/injection_points/injection_points.c @@ -17,6 +17,10 @@ #include "postgres.h" +#include <fcntl.h> +#include <sys/stat.h> + +#include "common/file_utils.h" #include "fmgr.h" #include "funcapi.h" #include "injection_points.h" @@ -24,6 +28,7 @@ #include "nodes/pg_list.h" #include "nodes/value.h" #include "storage/dsm_registry.h" +#include "storage/fd.h" #include "storage/ipc.h" #include "storage/lwlock.h" #include "storage/shmem.h" @@ -45,6 +50,106 @@ PG_MODULE_MAGIC; #define INJ_WAIT_INITIAL_US 10 /* 10us */ #define INJ_WAIT_MAX_US 100000 /* 100ms */ +/* + * Filesystem markers used to drive wait points without any SQL connection, for + * code paths that run before the server can answer SQL (e.g. early postmaster + * startup). They live under a directory in the data directory, one + * subdirectory per attached point and one file per parked process: + * + * pg_injection_points/<point>/ present = <point> attached as a wait; + * pg_injection_points/<point>/<pid> present = that process is parked here. + * + * A test attaches a point by creating its directory (scanned at startup), sees + * who is waiting by listing the directory, and wakes a specific waiter by + * removing its PID file. For a running server, it can create the directory + * and attach the point through SQL instead. Everything is plain filesystem + * state checked with stat(), so the controller needs no platform-specific + * logic or direct access to shared memory. Paths are relative to the data + * directory, the working directory of the postmaster and all backends. + */ +#define INJ_POINTS_DIR "pg_injection_points" + +/* "pg_injection_points/<point>" */ +static void +injection_point_dir(char *buf, size_t bufsize, const char *name) +{ + snprintf(buf, bufsize, "%s/%s", INJ_POINTS_DIR, name); +} + +/* "pg_injection_points/<point>/<pid>" */ +static void +injection_point_waiter_path(char *buf, size_t bufsize, + const char *name, int pid) +{ + snprintf(buf, bufsize, "%s/%s/%d", INJ_POINTS_DIR, name, pid); +} + +/* + * A point's name becomes a path component under INJ_POINTS_DIR, so only names + * built from a portable, separator-free character set get a filesystem marker. + * Points with other names still work through the SQL wakeup path; they just do + * not publish a marker. This also bounds the name to a single path component. + */ +static bool +injection_point_name_is_fs_safe(const char *name) +{ + if (name[0] == '\0' || strlen(name) >= INJ_NAME_MAXLEN) + return false; + + for (const char *p = name; *p != '\0'; p++) + { + if (!((*p >= 'A' && *p <= 'Z') || + (*p >= 'a' && *p <= 'z') || + (*p >= '0' && *p <= '9') || + *p == '_' || *p == '-')) + return false; + } + return true; +} + +/* + * Remove any leftover "<pid>" waiter files from a point directory. Called from + * the preload scan, which runs in the postmaster before any child is (re)forked, + * so this clears markers left by processes that were killed without running + * their cleanup - most importantly across a crash-and-restart cycle, where the + * directory is deliberately kept. Without this an out-of-process controller + * could mistake a dead process's marker for a freshly parked waiter. + */ +static void +injection_points_clear_waiters(const char *dirpath) +{ + DIR *dir; + struct dirent *de; + + dir = AllocateDir(dirpath); + if (dir == NULL) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open directory \"%s\": %m", dirpath))); + + while ((de = ReadDir(dir, dirpath)) != NULL) + { + char path[MAXPGPATH]; + const char *p; + + /* Waiter files are named after a PID, i.e. all digits. */ + for (p = de->d_name; *p != '\0'; p++) + { + if (*p < '0' || *p > '9') + break; + } + if (p == de->d_name || *p != '\0') + continue; + + snprintf(path, sizeof(path), "%s/%s", dirpath, de->d_name); + if (unlink(path) != 0 && errno != ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not remove file \"%s\": %m", path))); + } + FreeDir(dir); +} + /* * List of injection points stored in TopMemoryContext attached * locally to this process. @@ -85,6 +190,15 @@ extern PGDLLEXPORT void injection_wait(const char *name, /* track if injection points attached in this process are linked to it */ static bool injection_point_local = false; +/* + * Path of the filesystem marker this process published for a wait point (see + * injection_wait()), so the wait's cleanup callback can remove it on error or + * interrupt as well as on a normal wakeup. A process waits at one point at a + * time, so a single slot is enough. + */ +static char injection_waiter_path[MAXPGPATH]; +static bool injection_have_waiter_file = false; + static void injection_shmem_request(void *arg); static void injection_shmem_init(void *arg); @@ -117,14 +231,117 @@ injection_shmem_request(void *arg) ); } +/* + * Scan INJ_POINTS_DIR for point subdirectories and attach each as a wait point. + * Called from the shared-memory init callback, which runs in the postmaster (or + * a standalone backend) each time shared memory is (re)created - at initial + * startup and again after a crash-and-restart cycle - so the points are in + * place before any child reaches them, all without an SQL connection. A point + * can therefore be attached, by creating its directory, before the server is + * even started, and it survives crash restart because it is re-read here. + */ +static void +injection_points_preload(void) +{ + DIR *dir; + struct dirent *de; + + dir = AllocateDir(INJ_POINTS_DIR); + if (dir == NULL) + { + if (errno == ENOENT) + return; /* no directory means nothing attached */ + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open directory \"%s\": %m", + INJ_POINTS_DIR))); + } + + while ((de = ReadDir(dir, INJ_POINTS_DIR)) != NULL) + { + InjectionPointCondition condition = {.type = INJ_CONDITION_NONE}; + char path[MAXPGPATH]; + struct stat st; + + if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) + continue; + + /* + * Only names we would ourselves turn into a marker path are accepted, + * which also rejects anything that is not a single path component. + */ + if (!injection_point_name_is_fs_safe(de->d_name)) + continue; + + /* Each subdirectory names a wait point to attach. */ + injection_point_dir(path, sizeof(path), de->d_name); + if (stat(path, &st) != 0) + { + if (errno == ENOENT) + continue; + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not stat file \"%s\": %m", path))); + } + if (!S_ISDIR(st.st_mode)) + continue; + + /* + * Drop any stale waiter files before the replacement children run, so + * a marker left by a process killed without cleanup is not mistaken + * for a new waiter. + */ + injection_points_clear_waiters(path); + + InjectionPointAttach(de->d_name, "injection_points", "injection_wait", + &condition, sizeof(condition)); + } + FreeDir(dir); +} + +/* + * proc_exit callback that removes INJ_POINTS_DIR so markers do not survive the + * cluster. Forked children inherit this callback but must not run it, hence + * the IsUnderPostmaster guard: only the lifecycle owner (postmaster, or a + * standalone backend) cleans up. + */ +static void +injection_points_dir_cleanup(int code, Datum arg) +{ + struct stat st; + + if (!IsUnderPostmaster && stat(INJ_POINTS_DIR, &st) == 0) + (void) rmtree(INJ_POINTS_DIR, true); +} + static void injection_shmem_init(void *arg) { + static bool cleanup_registered = false; + /* * First time through, so initialize. This is shared with the dynamic * initialization using a DSM. */ injection_point_init_state(inj_state, NULL); + + /* + * Attach any wait points requested out of band through marker + * directories. + */ + injection_points_preload(); + + /* + * Make sure the marker directory does not outlive the cluster. This + * callback runs in the postmaster, which persists across crash restarts + * and calls us again each time, so register it only once to avoid + * exhausting the on_proc_exit slots. + */ + if (!cleanup_registered) + { + on_proc_exit(injection_points_dir_cleanup, 0); + cleanup_registered = true; + } } /* @@ -233,9 +450,20 @@ injection_wait_cleanup(int code, Datum arg) SpinLockAcquire(&inj_state->lock); inj_state->name[index][0] = '\0'; SpinLockRelease(&inj_state->lock); + + /* + * Remove our filesystem marker too. This runs both on the normal path + * and from PG_ENSURE_ERROR_CLEANUP on error or interrupt, so a parked + * process that gets cancelled does not leave a stale <pid> file behind. + */ + if (injection_have_waiter_file) + { + (void) unlink(injection_waiter_path); + injection_have_waiter_file = false; + } } -/* Wait until injection_points_wakeup() is called */ +/* Wait until released by injection_points_wakeup() or a removed marker file */ void injection_wait(const char *name, const void *private_data, void *arg) { @@ -245,6 +473,7 @@ injection_wait(const char *name, const void *private_data, void *arg) const InjectionPointCondition *condition = private_data; char *argstr = arg; int delay_us = 0; + struct stat st; /* * Check the condition before attaching to the shared state: attaching @@ -285,18 +514,84 @@ injection_wait(const char *name, const void *private_data, void *arg) name); /* - * Wait until the counter is bumped by injection_points_wakeup(). + * Set up the wait. Register the error-cleanup callback first, then + * publish our presence and loop, so that a failure while creating the + * marker still releases the shared-memory slot and removes any file we + * managed to create. * - * This loop starts with a short delay for responsiveness, enlarged to - * ease the CPU workload in slower environments. + * The loop starts with a short delay for responsiveness, enlarged to ease + * the CPU workload in slower environments. */ delay_us = INJ_WAIT_INITIAL_US; pgstat_report_wait_start(injection_wait_event); PG_ENSURE_ERROR_CLEANUP(injection_wait_cleanup, Int32GetDatum(index)); { - while (pg_atomic_read_u32(&inj_state->wait_counts[index]) == old_wait_counts) + /* + * If the controller created a directory for this point, publish our + * presence there so it can see that we are parked here and release + * this process. This also works for points attached through SQL. Do + * not create the directory ourselves: a point attached only through + * SQL must not leave a directory that would reattach it after a crash + * restart. + */ + if (injection_point_name_is_fs_safe(name)) + { + char dirpath[MAXPGPATH]; + struct stat dirst; + + injection_point_dir(dirpath, sizeof(dirpath), name); + if (stat(dirpath, &dirst) != 0) + { + if (errno != ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not stat file \"%s\": %m", dirpath))); + } + else if (S_ISDIR(dirst.st_mode)) + { + int fd; + + injection_point_waiter_path(injection_waiter_path, + sizeof(injection_waiter_path), name, + MyProcPid); + fd = OpenTransientFile(injection_waiter_path, + O_RDWR | O_CREAT | O_TRUNC); + if (fd < 0) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not create file \"%s\": %m", + injection_waiter_path))); + CloseTransientFile(fd); + injection_have_waiter_file = true; + } + } + + /* + * Wait until released, either by injection_points_wakeup() bumping + * the counter or by our waiter file being removed. Only the file + * actually disappearing (ENOENT) counts as a release; any other + * stat() failure is unexpected and raised, so the wait cannot end for + * a spurious reason. + */ + for (;;) { + if (pg_atomic_read_u32(&inj_state->wait_counts[index]) != old_wait_counts) + break; + + if (injection_have_waiter_file) + { + if (stat(injection_waiter_path, &st) != 0) + { + if (errno == ENOENT) + break; + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not stat file \"%s\": %m", + injection_waiter_path))); + } + } + CHECK_FOR_INTERRUPTS(); pg_usleep(delay_us); if (delay_us < INJ_WAIT_MAX_US) @@ -306,7 +601,10 @@ injection_wait(const char *name, const void *private_data, void *arg) PG_END_ENSURE_ERROR_CLEANUP(injection_wait_cleanup, Int32GetDatum(index)); pgstat_report_wait_end(); - /* Remove this injection point from the waiters. */ + /* + * Remove this injection point from the waiters. This also unlinks our + * filesystem marker, which may still exist after a counter wakeup. + */ injection_wait_cleanup(0, Int32GetDatum(index)); } diff --git a/src/test/modules/injection_points/meson.build b/src/test/modules/injection_points/meson.build index 97e7a736282..b6df9a820f9 100644 --- a/src/test/modules/injection_points/meson.build +++ b/src/test/modules/injection_points/meson.build @@ -79,4 +79,12 @@ tests += { '--temp-config', files('extra.conf'), ], }, + 'tap': { + 'env': { + 'enable_injection_points': get_option('injection_points') ? 'yes' : 'no', + }, + 'tests': [ + 't/001_wait_without_sql.pl', + ], + }, } diff --git a/src/test/modules/injection_points/t/001_wait_without_sql.pl b/src/test/modules/injection_points/t/001_wait_without_sql.pl new file mode 100644 index 00000000000..c3af70f2d06 --- /dev/null +++ b/src/test/modules/injection_points/t/001_wait_without_sql.pl @@ -0,0 +1,127 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Exercise driving a wait injection point purely through the filesystem, with +# no SQL used for the coordination itself. This is meant for code paths that +# run before the server can answer queries (e.g. early postmaster startup). +# +# Layout under the data directory: +# pg_injection_points/<point>/ present -> <point> attached as a wait +# pg_injection_points/<point>/<pid> present -> that backend is parked here +# Removing the <pid> file wakes that backend. + +use strict; +use warnings FATAL => 'all'; + +use Time::HiRes qw(usleep); + +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +if ($ENV{enable_injection_points} ne 'yes') +{ + plan skip_all => 'Injection points not supported by this build'; +} + +my $point = 'no-sql-wait'; + +my $node = PostgreSQL::Test::Cluster->new('node'); +$node->init; +$node->append_conf('postgresql.conf', + "shared_preload_libraries = 'injection_points'"); + +# Attach the wait point before the server is even running, without any SQL: +# just create its directory. The module scans these at startup. +my $root = $node->data_dir . '/pg_injection_points'; +my $pdir = "$root/$point"; +mkdir $root or die "could not create $root: $!"; +mkdir $pdir or die "could not create $pdir: $!"; + +# Plant a stale waiter file, as a process killed without running its cleanup +# would leave behind. The startup scan must remove it, or it could be mistaken +# for a live waiter below. +my $stale = "$pdir/99999999"; +open(my $fh, '>', $stale) or die "could not create $stale: $!"; +close($fh); + +$node->start; + +ok(!-e $stale, 'stale waiter file removed by the startup scan'); + +# The module attached the point from its directory at startup; we never called +# injection_points_attach(). +$node->safe_psql('postgres', 'CREATE EXTENSION injection_points;'); + +my $listed = $node->safe_psql('postgres', + "SELECT count(*) FROM injection_points_list() WHERE point_name = '$point';" +); +is($listed, '1', 'wait point attached from its directory, without SQL'); + +# Fire the point in a background session; it must block in injection_wait(). +my $bg = $node->background_psql('postgres'); +$bg->query_until( + qr/start/, qq[ +\\echo start +SELECT injection_points_run('$point'); +]); + +# Detect that the backend is parked, purely through the filesystem: it +# publishes a file named after its PID inside the point directory. +my $waiter; +foreach my $i (1 .. 10 * $PostgreSQL::Test::Utils::timeout_default) +{ + if (opendir(my $dh, $pdir)) + { + ($waiter) = grep { /^\d+\z/ } readdir($dh); + closedir($dh); + last if defined $waiter; + } + usleep(100_000); +} +if (!ok(defined $waiter, + 'backend published its waiter file (filesystem-observable)')) +{ + # Nothing to release; the rest of the sequence would test something else. + # An immediate stop also unblocks the parked background session. + $node->stop('immediate'); + done_testing(); + exit; +} + +# Wake that specific backend without any SQL: remove its waiter file. +unlink "$pdir/$waiter" or die "could not remove waiter file: $!"; + +# The blocked statement now finishes, proving the filesystem wakeup worked. +like($bg->query_safe('SELECT 1;'), + qr/^1$/m, 'backend released by removing its waiter file, without SQL'); + +# A point attached only through SQL must not create filesystem state. In +# particular, leaving a point directory behind would reattach the point without +# its original conditions after a crash restart. +my $sql_point = 'sql-only-wait'; +$node->safe_psql('postgres', + "SELECT injection_points_attach('$sql_point', 'wait');"); +$bg->query_until( + qr/sql-start/, qq[ +\\echo sql-start +SELECT injection_points_run('$sql_point'); +]); +ok( $node->poll_query_until( + 'postgres', + "SELECT count(*) = 1 FROM pg_stat_activity " + . "WHERE wait_event_type = 'InjectionPoint' " + . "AND wait_event = '$sql_point';"), + 'SQL-attached injection point reached its wait'); +ok(!-e "$root/$sql_point", + 'SQL-attached wait did not create filesystem control state'); +$node->safe_psql('postgres', "SELECT injection_points_wakeup('$sql_point');"); +like($bg->query_safe('SELECT 1;'), + qr/^1$/m, 'SQL-attached wait released through SQL'); +$node->safe_psql('postgres', "SELECT injection_points_detach('$sql_point');"); +$bg->quit; + +# The directory does not survive the cluster. +$node->stop; +ok(!-d $root, 'injection points directory removed at shutdown'); + +done_testing(); base-commit: 0b12a5a7435a3b50cccea85347d9e3cf8128d88a -- That's all, folks. May the source be with you. =
v1-0002-Wake-the-pre-auth-test-s-intended-backend-by-PID.patch (application/octet-stream, 4.9K) download From c70d1a32e7999c53826fef8b7609a4e969211c32 Mon Sep 17 00:00:00 2001 From: Andrey Borodin <amborodin@acm.org> Date: Sun, 1 Nov 2026 20:42:19 +0400 Subject: [PATCH v1 2/2] Wake the pre-auth test's intended backend by PID. The init-pre-auth injection point can also stop an autovacuum worker or the logical replication launcher. A name-only wakeup may release that process instead of the client selected by the test. Wait for the client's PID file and remove it to release that particular backend. Park another session at the point first to exercise the multiple-waiter case deterministically. Bound the existing polling loops and stop the server before cleaning up the marker directory. Reported-by: Alexander Lakhin <exclusion@gmail.com> Discussion: https://postgr.es/m/c984ed89-cac1-47a9-91fa-4d3287722203@gmail.com --- src/test/authentication/t/007_pre_auth.pl | 59 ++++++++++++++++++----- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/src/test/authentication/t/007_pre_auth.pl b/src/test/authentication/t/007_pre_auth.pl index 04063f4721d..a31b7579caa 100644 --- a/src/test/authentication/t/007_pre_auth.pl +++ b/src/test/authentication/t/007_pre_auth.pl @@ -6,6 +6,7 @@ use strict; use warnings FATAL => 'all'; +use File::Path qw(make_path remove_tree); use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Time::HiRes qw(usleep); @@ -35,20 +36,37 @@ if (!$node->check_extension('injection_points')) $node->safe_psql('postgres', 'CREATE EXTENSION injection_points'); -# Connect to the server and inject a waitpoint. +# Establish the control connections before attaching the waitpoint. my $psql = $node->background_psql('postgres'); +my $other = $node->background_psql('postgres'); +my $other_pid = $other->query_safe('SELECT pg_backend_pid()'); + +# Creating the directory opts this SQL-attached point into publishing a +# separate file for each waiter. Removing a file releases only that PID. +my $wait_root = $node->data_dir . '/pg_injection_points'; +my $wait_dir = "$wait_root/init-pre-auth"; +make_path($wait_dir); $psql->query_safe("SELECT injection_points_attach('init-pre-auth', 'wait')"); +# Model an unrelated process reaching the point before the new client. +# A name-only wakeup would release this waiter instead of the client. +$other->query_until( + qr/other-waiter/, qq[ +\\echo other-waiter +SELECT injection_points_run('init-pre-auth'); +]); +wait_for_file("$wait_dir/$other_pid", qr/\A/); + # From this point on, all new connections will hang during startup, just before # authentication. Use the $psql connection handle for server interaction. my $conn = $node->background_psql('postgres', wait => 0); # Wait for the connection to show up in pg_stat_activity, with the wait_event # of the injection point. -my $pid; -while (1) +my $pid = ''; +for (1 .. 10 * $PostgreSQL::Test::Utils::timeout_default) { - $pid = $psql->query( + $pid = $psql->query_safe( qq{SELECT pid FROM pg_stat_activity WHERE backend_type = 'client backend' AND state = 'starting' @@ -59,27 +77,44 @@ while (1) } note "backend $pid is authenticating"; -ok(1, 'authenticating connections are recorded in pg_stat_activity'); - -# Detach the waitpoint and wait for the connection to complete. -$psql->query_safe("SELECT injection_points_wakeup('init-pre-auth');"); +like($pid, qr/^\d+$/, + 'authenticating connections are recorded in pg_stat_activity') + or die 'client did not reach the pre-authentication waitpoint'; + +# The wait event is reported before the waiter file is published. Wait for +# the file too, then release this client without waking an unrelated waiter. +wait_for_file("$wait_dir/$pid", qr/\A/); +unlink "$wait_dir/$pid" or die "could not release backend $pid: $!"; $conn->wait_connect(); # Make sure the pgstat entry is updated eventually. -while (1) +my $state = ''; +for (1 .. 10 * $PostgreSQL::Test::Utils::timeout_default) { - my $state = - $psql->query("SELECT state FROM pg_stat_activity WHERE pid = $pid;"); + $state = $psql->query_safe( + "SELECT state FROM pg_stat_activity WHERE pid = $pid;"); last if $state eq "idle"; note "state for backend $pid is '$state'; waiting for 'idle'..."; usleep(100_000); } -ok(1, 'authenticated connections reach idle state in pg_stat_activity'); +is($state, 'idle', + 'authenticated connections reach idle state in pg_stat_activity'); +ok(-f "$wait_dir/$other_pid", 'unrelated waiter remains blocked'); $psql->query_safe("SELECT injection_points_detach('init-pre-auth');"); +unlink "$wait_dir/$other_pid" + or die "could not release backend $other_pid: $!"; +$other->query_safe('SELECT 1'); +$other->quit(); $psql->quit(); $conn->quit(); +# Other processes may also have reached the point. Some background processes +# handle SIGTERM only after initialization, so use immediate shutdown before +# removing the markers. The module was loaded through SQL, not preloaded. +$node->stop('immediate'); +remove_tree($wait_root); + done_testing(); -- That's all, folks. May the source be with you. =
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: 007_pre_auth.pl fails if injection_points_wakeup hits unintended process
@ 2026-11-02 07:08  Michael Paquier <michael@paquier.xyz>
  parent: Alexander Lakhin <exclusion@gmail.com>
  2 siblings, 1 reply; 5+ messages in thread

From: Michael Paquier @ 2026-11-02 07:08 UTC (permalink / raw)
  To: Alexander Lakhin <exclusion@gmail.com>; +Cc: pgsql-hackers

On Sun, Nov 01, 2026 at 03:00:00PM +0200, Alexander Lakhin wrote:
> That is, the wakeup can hit autovacuum worker or logical replication
> launcher, not the intended client backend. This test failure can be easily
> reproduced with:
> @@ -753,2 +753,3 @@ InitPostgres(const char *in_dbname, Oid dboid,
>          pgstat_bestart_initial();
> +if (AmRegularBackendProcess()) pg_usleep(1000000);
>          INJECTION_POINT("init-pre-auth", NULL);
> 
> and
> echo "autovacuum_naptime = 1s" >/tmp/temp.config
> TEMP_CONFIG=/tmp/temp.config PROVE_TESTS="t/007*" make -s check -C src/test/authentication
> 
> Reproduced starting from e2080261c.

Thanks for the report.  I am planning to tackle this one with the
following tweak to the test's conf:
autovacuum = off
max_logical_replication_workers = 0

Does that work for you?
--
Michael

Attachments:

preauth-test.patch (text/plain, 638B) download diff --git a/src/test/authentication/t/007_pre_auth.pl b/src/test/authentication/t/007_pre_auth.pl index 04063f4721d9..9c2e3341d768 100644 --- a/src/test/authentication/t/007_pre_auth.pl +++ b/src/test/authentication/t/007_pre_auth.pl @@ -18,9 +18,14 @@ if ($ENV{enable_injection_points} ne 'yes') my $node = PostgreSQL::Test::Cluster->new('primary'); $node->init; + +# Disable autovacuum and logical replication workers; these could reach +# the injection point wait. $node->append_conf( 'postgresql.conf', q[ log_connections = 'receipt,authentication' +autovacuum = off +max_logical_replication_workers = 0 ]); $node->start;
signature.asc (application/pgp-signature, 832B) download ^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: 007_pre_auth.pl fails if injection_points_wakeup hits unintended process
@ 2026-11-02 07:36  Andrey Borodin <x4mmm@yandex-team.ru>
  parent: Michael Paquier <michael@paquier.xyz>
  0 siblings, 0 replies; 5+ messages in thread

From: Andrey Borodin @ 2026-11-02 07:36 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Alexander Lakhin <exclusion@gmail.com>; pgsql-hackers



> On 2 Nov 2026, at 11:08, Michael Paquier <michael@paquier.xyz> wrote:
> 
> Thanks for the report.  I am planning to tackle this one with the
> following tweak to the test's conf:
> autovacuum = off
> max_logical_replication_workers = 0
> 
> Does that work for you?

I think it's fine. But when we will have new bgworker hitting this point we might
need to change this test again.


Best regards, Andrey Borodin.





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: 007_pre_auth.pl fails if injection_points_wakeup hits unintended process
@ 2026-11-03 23:45  Michael Paquier <michael@paquier.xyz>
  parent: Alexander Lakhin <exclusion@gmail.com>
  2 siblings, 0 replies; 5+ messages in thread

From: Michael Paquier @ 2026-11-03 23:45 UTC (permalink / raw)
  To: Alexander Lakhin <exclusion@gmail.com>; +Cc: pgsql-hackers

On Sun, Nov 01, 2026 at 03:00:00PM +0200, Alexander Lakhin wrote:
> Reproduced starting from e2080261c.

And this one should be fixed as of 590788ad0bff.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2026-11-03 23:45 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-01 13:00 007_pre_auth.pl fails if injection_points_wakeup hits unintended process Alexander Lakhin <exclusion@gmail.com>
2026-11-01 18:21 ` Andrey Borodin <x4mmm@yandex-team.ru>
2026-11-02 07:08 ` Michael Paquier <michael@paquier.xyz>
2026-11-02 07:36   ` Andrey Borodin <x4mmm@yandex-team.ru>
2026-11-03 23:45 ` Michael Paquier <michael@paquier.xyz>

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