Re: [PATCH] Fix socket handle inheritance on Windows

From: Bryan Green <dbryan(dot)green(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: [PATCH] Fix socket handle inheritance on Windows
Date: 2025-11-06 18:53:47
Message-ID: ed75593c-ec17-45bb-a044-87f8595b6af1@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 11/5/2025 11:06 PM, Bryan Green wrote:
> Greetings,
>
> I've discovered that PostgreSQL on Windows has a handle inheritance
> problem that prevents clean restarts after the postmaster is killed
> while child processes are running.
>
> The issue is that Windows handles (files, sockets, pipes, shared memory)
> are inheritable by default. When backends spawn child processes
> archive_command, COPY TO PROGRAM, etc.—those children inherit all the
> backend's handles. Windows uses reference counting, so inherited handles
> keep resources alive even after the owning process exits.
>
> I reproduced this with sockets:
>
> 1. Started PostgreSQL on port 6565
> 2. Connected with psql and ran:
> \copy (select 1) to program 'powershell -Command "Start-Sleep 300"'
> 3. Used Sysinternals handle64.exe to examine handles:
> - PowerShell had inherited socket handles (\Device\Afd)
> - Same handle values in both processes (proving inheritance, not
> separate opens)
> 4. Killed the postmaster
> 5. netstat showed port 6565 still LISTENING on the dead postmaster PID
> 6. Restart failed: "Address already in use"
> 7. Port only freed after killing PowerShell
>
> The socket fix adds WSA_FLAG_NO_HANDLE_INHERIT to WSASocket() in
> pgwin32_socket(), and calls SetHandleInformation() in
> BackendInitialize() to mark the inherited client socket non-inheritable.
> The latter is needed because handles passed to children become
> inheritable again on Windows.
>
> TAP test included that verifies the port is freed immediately after
> postmaster exit rather than remaining in a zombie state.
>
> The problem affects multiple handle types:
>
> Files: https://commitfest.postgresql.org/patch/6197/
> Sockets: Fixed by attached patch
> Pipes: Not yet addressed
> Shared memory: Not yet addressed (causes "pre-existing shared memory
> block" errors)
>
> Patches for pipes and shared memory will follow over the next couple of
> days.
>
Incorrect extension on the patch. Attached is the correct patch.

--
Bryan Green
EDB: https://www.enterprisedb.com

Attachment Content-Type Size
0001-Fix-socket-handle-inheritance-on-Windows-preventing-.patch text/plain 7.6 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Álvaro Herrera 2025-11-06 19:48:11 Re: Consistently use the XLogRecPtrIsInvalid() macro
Previous Message Masahiko Sawada 2025-11-06 18:36:12 Re: Assertion failure in SnapBuildInitialSnapshot()