Skip to content

Comments

fix(bwrap): close pdeathsig parent race#730

Open
danielchristiancazares wants to merge 1 commit intocontainers:mainfrom
danielchristiancazares:fix/pdeathsig-race-check
Open

fix(bwrap): close pdeathsig parent race#730
danielchristiancazares wants to merge 1 commit intocontainers:mainfrom
danielchristiancazares:fix/pdeathsig-race-check

Conversation

@danielchristiancazares
Copy link

@danielchristiancazares danielchristiancazares commented Feb 23, 2026

At startup, the child process needs to die if the parent dies, so the child process sets PR_SET_PDEATHSIG so the kernel will SIGKILL it if the parent dies. Which is the whole setting up a parent-death signal with prctl. The problem is that if the parent dies after fork but before or during the prctl call, the child won't receive it because the parent is gone already so nothing will trigger it. So we end up with a VERY narrow window for failure after the fork/exec that can cause potential issues if the parent dies unexpectedly. The kill and _exit calls are there to handle the process safely if one of those windows is missed. I used _exit instead of exit so cleanup handlers don't run in post-forked code. It's a well known race and systemd and other projects use similar patterns to close it out.

As for the how it goes:
parent spawns child
child executes but hasn't called prctl(PR_SET_PDEATHSIG)
parent dies or exits
child then calls prctl(PR_SET_PDEATHSIG) but by now its too late

We grab ppid, set PDEATHSIG, then check later if getppid() isn't what what ppid() was before which means the parent is gone or swapped (like to PID 1 or something else) and this part is why it works at all. It's the reference point to compare against with later. It hardly ever happens, but it's documented and has a known a fix.

Capture parent pid before PR_SET_PDEATHSIG and exit if parent changed after prctl.
@swick
Copy link
Contributor

swick commented Feb 23, 2026

This needs a lot more explanation, including what the race actually is, why _exit is used, why it calls kill on itself instead of relying on exit. The PR description also suspiciously looks vibe-coded.

@danielchristiancazares
Copy link
Author

Updated the description with a cleaner how and why.

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.

2 participants