Skip to content

[#1703] Stop child workers before pid-file removal - #1721

Open
manishpatel00 wants to merge 1 commit into
pgmoneta:mainfrom
manishpatel00:issue1703_child_shutdown
Open

manishpatel00 wants to merge 1 commit into
pgmoneta:mainfrom
manishpatel00:issue1703_child_shutdown

Conversation

@manishpatel00

@manishpatel00 manishpatel00 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1703

Forked workers inherit the server's libev signal handlers without running the parent event loop, so targeted SIGTERM can leave foreground workers alive. The server now resets SIGTERM, SIGINT, and SIGCHLD dispositions in forked children and records worker PIDs at server fork sites. Shutdown signals tracked children directly in every run mode, waits up to five seconds, then sends SIGKILL and allows a further bounded reap interval; any remaining PIDs are logged before cleanup proceeds.

The pid file and shared memory are cleaned up only after the bounded child wait. The existing process-group signal remains limited to daemon/management-stop shutdowns.

Validation: cmake --build /tmp/pgmoneta-1703-build --target pgmoneta-bin pgmoneta-test --parallel 4 passed. The full pgmoneta-test runner stalled after AES fault-test output; container tests could not run because the Docker daemon was unavailable.

@shahmeerabdul

Copy link
Copy Markdown

i'd offered to work on this in #1703 and was waiting for @jesperpedersen to confirm the direction, so a heads-up first would have been nice. since i traced it there, here's a review.

the ordering looks right: signal, wait, then remove the pid file and destroy shared memory, on both the normal and the error path. resetting the dispositions in the child also fixes the ctrl-c reproduction from the issue, since the terminal sends SIGINT to the whole foreground group.

two gaps from the issue are still open though:

  1. foreground runs still don't signal the children. kill(0, SIGTERM) is still behind if (daemon || stop) (main.c:1045 and 1111), so a SIGTERM to main alone reaches no child: kill <pid>, or docker stop, since the images in contrib/docker run pgmoneta in the foreground as pid 1. main then blocks in wait_for_children() until a running backup next checks config->running. before this change main exited right away, now docker stop can run into its 10s grace and SIGKILL. kill(0, ...) isn't safe in the foreground (started from a script, pgmoneta shares the script's process group), so main probably has to signal its own children by pid, e.g. by recording them at the fork sites.

  2. wait_for_children() (main.c:2660) waits with no limit and logs nothing. a child stuck on i/o (a storage engine upload, a server that stopped answering) hangs the shutdown with nothing in the log saying why. the issue asked for a timeout that says so, e.g. a WNOHANG loop with a deadline that logs the pids still running and then gives up or SIGKILLs them.

smaller: the atfork handler resets all seven watched signals (main.c:2637), so SIGHUP, SIGALRM and SIGUSR1 now terminate a child instead of being swallowed. as far as i can see nothing sends those to children today, so it's probably fine, but limiting it to SIGTERM/SIGINT (plus SIGCHLD) keeps the change to what the issue needs.

the red static-analysis and clang debug jobs fail the same way on main right now, so they're not from this.

@ecleon26

Copy link
Copy Markdown
Collaborator

@manishpatel00 remember to squash

@ecleon26

Copy link
Copy Markdown
Collaborator

remove the pid file before kill(0, SIGTERM) on the error path, or stop the parent signalling itself

@ecleon26

Copy link
Copy Markdown
Collaborator

@manishpatel00 rebase onto pgmoneta/pgmoneta#1723 and drop the art.c 17 → 18 baseline change.

@manishpatel00
manishpatel00 force-pushed the issue1703_child_shutdown branch from a5387d8 to 39e1f7a Compare November 28, 2026 20:32
@ecleon26

Copy link
Copy Markdown
Collaborator

@manishpatel00 remember to squash

@manishpatel00
manishpatel00 force-pushed the issue1703_child_shutdown branch from 39e1f7a to b9320cb Compare November 28, 2026 20:37
@manishpatel00

manishpatel00 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Done — rebased onto the latest upstream/main and squashed into exactly one signed commit.

The unrelated art.c cppcheck baseline change was removed. Revalidated clang-format, cppcheck, git diff --check, and the pgmoneta-bin/pgmoneta-test builds.

@ecleon26

Copy link
Copy Markdown
Collaborator

@manishpatel00 LGTM now

Forked workers inherit the server signal dispositions but do not run the parent libev loop, so shutdown signals can leave direct workers alive. Track every server child, restore the required signal defaults after fork, and shut children down directly in every run mode with bounded waiting and SIGKILL escalation.

Remove the pid file before the error-path process-group signal so kill(0, SIGTERM) cannot leave stale parent identity during cleanup. Keep shared-memory cleanup after child shutdown.

Fixes pgmoneta#1703

Signed-off-by: Manish Kumar <[email protected]>
@manishpatel00
manishpatel00 force-pushed the issue1703_child_shutdown branch from b9320cb to 99186d1 Compare November 29, 2026 19:23
@ecleon26

ecleon26 commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

@manishpatel00 fix the pr title

@manishpatel00 manishpatel00 changed the title [pgmoneta#1703] Stop child workers before pid-file removal [#1703] Stop child workers before pid-file removal Dec 2, 2026
@manishpatel00

Copy link
Copy Markdown
Contributor Author

@ecleon26 Fixed the title to use the [#1703] prefix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Children keep running after main shuts down

3 participants