[PATCH] Fix socket handle inheritance on Windows

Поиск
Список
Период
Сортировка
Искать

[PATCH] Fix socket handle inheritance on Windows

От:
Bryan Green <dbryan.green@gmail.com>
Дата:
Вложения:
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.

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

Re: [PATCH] Fix socket handle inheritance on Windows

От:
Bryan Green <dbryan.green@gmail.com>
Дата:
Вложения:
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

Re: [PATCH] Fix socket handle inheritance on Windows

От:
Thomas Munro <thomas.munro@gmail.com>
Дата:
On Fri, Nov 7, 2025 at 7:53 AM Bryan Green  wrote:
> > 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.

Right, this is a problem too and your analysis makes sense.

-    s = WSASocket(af, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED);
+    s = WSASocket(af, type, protocol, NULL, 0,
+                  WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT);

I wonder if we should control this with a new be-more-like-Unix
SOCK_CLOEXEC flag.  In practice we might never want sockets created
this way to be inherited across CreateProcess, so we might always pass
SOCK_CLOEXEC in places like libpq (we already do that).  But in theory
at least, someone might have a reason to want an inheritable socket
(no doubt with some complications), and it seems attractive to work
the same way cross-platform and also mirror the O_CLOEXEC behaviour
your other thread fixes, and I'm also looking further ahead to the
case of accepted sockets that have some additional needs (see below),
so it might be good to be explicit and consistent about the flags.

+#ifdef WIN32
+    /*
+     * On Windows, the client socket inherited from the postmaster becomes
+     * inheritable again in this process. Prevent child processes spawned
+     * by this backend from inheriting it.
+     */
+    if (!SetHandleInformation((HANDLE) port->sock, HANDLE_FLAG_INHERIT, 0))
+        ereport(WARNING,
+                (errmsg_internal("could not disable socket handle
inheritance: error code %lu",
+                                 GetLastError())));
+#endif

On Unix we also need the equivalent, but it's in pq_init():

#ifndef WIN32

    /* Don't give the socket to any subprograms we execute. */
    if (fcntl(port->sock, F_SETFD, FD_CLOEXEC) < 0)
        elog(FATAL, "fcntl(F_SETFD) failed on socket: %m");
#endif

Would it make sense to have a socket_set_cloexec() function, following
the example of socket_set_nonblocking(), so we could harmonise the
calling code?

I think we'll also need an accept4() function for Windows that accepts
SOCK_CLOEXEC and converts it to WSA_FLAG_NO_HANDLE_INHERIT, now that
you've pointed this out.  It's not needed for your problem report
here, and doesn't even make sense for EXEC_BACKEND by definition, but
I think we'll probably want it for multithreaded PostgreSQL on
Windows.  With backends as threads, at least in one experimental
architecture with listener in the main/only sub-postmaster process,
there is a race window between ye olde accept() and
socket_set_cloexec() that leaks handles if a subprocess is created
concurrently by any thread.  Adopting accept4() is pretty trivial
(something like v5-0001[1]), it just wasn't quite compelling enough to
commit before multithreading started heating up as a topic.  I hadn't
previously grokked that Windows will need to be able to reach its
equivalent flag too for the reason you've pointed out.

I mention that as context for my suggestion that we might want an
explicit SOCK_CLOEXEC flag, because it finishes up being conditional
in the accept4() case assuming that design: multi-process mode needs
it except in EXEC_BACKEND builds which have to call
socket_set_cloexit() in the exec'd child by definition, while
in-development multi-threaded mode needs it on all platforms.  (The
fact that macOS alone has so far refused to implement it is a bit
annoying, and potential workarounds are expensive, but that's a topic
for another day.)

[1] https://www.postgresql.org/message-id/flat/CA%2BhUKGKPNFcfBQduqof4-7C%3DavjcSfdkKBGvQoRuAvfocnvY0A%40mail.gmail.com#abe1bf9def93e897827e878aac8a6945


Re: [PATCH] Fix socket handle inheritance on Windows

От:
Bryan Green <dbryan.green@gmail.com>
Дата:
On 11/6/25 19:03, Thomas Munro wrote:
> On Fri, Nov 7, 2025 at 7:53 AM Bryan Green  wrote:
>>> 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.
> 
> Right, this is a problem too and your analysis makes sense.
> 
> -    s = WSASocket(af, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED);
> +    s = WSASocket(af, type, protocol, NULL, 0,
> +                  WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT);
> 
> I wonder if we should control this with a new be-more-like-Unix
> SOCK_CLOEXEC flag.  In practice we might never want sockets created
> this way to be inherited across CreateProcess, so we might always pass
> SOCK_CLOEXEC in places like libpq (we already do that).  But in theory
> at least, someone might have a reason to want an inheritable socket
> (no doubt with some complications), and it seems attractive to work
> the same way cross-platform and also mirror the O_CLOEXEC behaviour
> your other thread fixes, and I'm also looking further ahead to the
> case of accepted sockets that have some additional needs (see below),
> so it might be good to be explicit and consistent about the flags.
> 
> +#ifdef WIN32
> +    /*
> +     * On Windows, the client socket inherited from the postmaster becomes
> +     * inheritable again in this process. Prevent child processes spawned
> +     * by this backend from inheriting it.
> +     */
> +    if (!SetHandleInformation((HANDLE) port->sock, HANDLE_FLAG_INHERIT, 0))
> +        ereport(WARNING,
> +                (errmsg_internal("could not disable socket handle
> inheritance: error code %lu",
> +                                 GetLastError())));
> +#endif
> 
> On Unix we also need the equivalent, but it's in pq_init():
> 
> #ifndef WIN32
> 
>      /* Don't give the socket to any subprograms we execute. */
>      if (fcntl(port->sock, F_SETFD, FD_CLOEXEC) < 0)
>          elog(FATAL, "fcntl(F_SETFD) failed on socket: %m");
> #endif
> 
> Would it make sense to have a socket_set_cloexec() function, following
> the example of socket_set_nonblocking(), so we could harmonise the
> calling code?
> 

Yes, I think it would.

> I think we'll also need an accept4() function for Windows that accepts
> SOCK_CLOEXEC and converts it to WSA_FLAG_NO_HANDLE_INHERIT, now that
> you've pointed this out.  It's not needed for your problem report
> here, and doesn't even make sense for EXEC_BACKEND by definition, but
> I think we'll probably want it for multithreaded PostgreSQL on
> Windows.  With backends as threads, at least in one experimental
> architecture with listener in the main/only sub-postmaster process,
> there is a race window between ye olde accept() and
> socket_set_cloexec() that leaks handles if a subprocess is created
> concurrently by any thread.  Adopting accept4() is pretty trivial
> (something like v5-0001[1]), it just wasn't quite compelling enough to
> commit before multithreading started heating up as a topic.  I hadn't
> previously grokked that Windows will need to be able to reach its
> equivalent flag too for the reason you've pointed out.
> 

I will look over [1].

> I mention that as context for my suggestion that we might want an
> explicit SOCK_CLOEXEC flag, because it finishes up being conditional
> in the accept4() case assuming that design: multi-process mode needs
> it except in EXEC_BACKEND builds which have to call
> socket_set_cloexit() in the exec'd child by definition, while
> in-development multi-threaded mode needs it on all platforms.  (The
> fact that macOS alone has so far refused to implement it is a bit
> annoying, and potential workarounds are expensive, but that's a topic
> for another day.)
> 

If you agree, and I think you do, I will implement the SOCK_CLOEXEC 
abstraction and the socket_set_cloexec helper for this.  My bug fix will 
be the first user.

I also need to handle the handles for shared memory and pipes.  I will 
probably just write those up tonight and share for review and discussion.

> [1] https://www.postgresql.org/message-id/flat/CA%2BhUKGKPNFcfBQduqof4-7C%3DavjcSfdkKBGvQoRuAvfocnvY0A%40mail.gmail.com#abe1bf9def93e897827e878aac8a6945


Thanks for your excellent review and suggestions.

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




Re: [PATCH] Fix socket handle inheritance on Windows

От:
Thomas Munro <thomas.munro@gmail.com>
Дата:
On Fri, Nov 7, 2025 at 2:35 PM Bryan Green  wrote:
> If you agree, and I think you do, I will implement the SOCK_CLOEXEC
> abstraction and the socket_set_cloexec helper for this.  My bug fix will
> be the first user.

+1

Thanks for working on all this stuff.

> I also need to handle the handles for shared memory and pipes.  I will
> probably just write those up tonight and share for review and discussion.

Cool.


Re: [PATCH] Fix socket handle inheritance on Windows

От:
Harrison Booth <harrisontbooth@gmail.com>
Дата:
Вложения:
Bryan,

I've attached your public patch with no changes. Also attached is a
test-only regression supplement.

On PostgreSQL master 6b9f40d49ae33eb0b4a7c77666fe4971ef7ba087, the
exact ordered series applied with git am. The focused pg_ctl test
passed all nine assertions on hosted Windows x64 and ARM64. On the
unpatched baseline, only the two socket-lifetime assertions failed on
both hosts. The supplement forces a fresh initdb so the test does not
copy the temporary install cluster.

Hosted run: https://github.com/Harrison-Booth/postgresql-windows-ci/actions/runs/39594799011

This was a focused test, not a full regression run. It does not
implement the shared SOCK_CLOEXEC flag or accept4-style API discussed
in the thread. Could you review whether this test-only supplement
belongs with your patch as posted?

Best,

Harrison

Re: [PATCH] Fix socket handle inheritance on Windows

От:
Bryan Green <dbryan.green@gmail.com>
Дата:
On 10/21/26 08:22, Harrison Booth wrote:
> Bryan,
> 
> I've attached your public patch with no changes. Also attached is a
> test-only regression supplement.
> 
> On PostgreSQL master 6b9f40d49ae33eb0b4a7c77666fe4971ef7ba087, the
> exact ordered series applied with git am. The focused pg_ctl test
> passed all nine assertions on hosted Windows x64 and ARM64. On the
> unpatched baseline, only the two socket-lifetime assertions failed on
> both hosts. The supplement forces a fresh initdb so the test does not
> copy the temporary install cluster.
> 
> Hosted run: https://github.com/Harrison-Booth/postgresql-windows-ci/actions/runs/39594799011
> 
> This was a focused test, not a full regression run. It does not
> implement the shared SOCK_CLOEXEC flag or accept4-style API discussed
> in the thread. Could you review whether this test-only supplement
> belongs with your patch as posted?
> 
> Best,
> 
> Harrison

Harrison,

Thanks for kicking the tires on this.  I had this and several other
windows patches on hold due to another issue that has now evaporated.  I
will be posting some new patches today or tomorrow.  I have made the
test a bit different, but let me post it and then we can discuss.
Really appreciate you taking the time and effort on this!

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


Re: [PATCH] Fix socket handle inheritance on Windows

От:
Bryan Green <dbryan.green@gmail.com>
Дата:
On 11/6/2025 7:59 PM, Thomas Munro wrote:
> On Fri, Nov 7, 2025 at 2:35 PM Bryan Green  wrote:
>> If you agree, and I think you do, I will implement the SOCK_CLOEXEC
>> abstraction and the socket_set_cloexec helper for this.  My bug fix will
>> be the first user.
> 
> +1
> 
> Thanks for working on all this stuff.
> 
>> I also need to handle the handles for shared memory and pipes.  I will
>> probably just write those up tonight and share for review and discussion.
> 
> Cool.

After a long wait... here's v2! It's two patches now. 0002 is the
shared memory fix I said would follow back in November.

0001 does sockets the way Thomas suggested. Backend code on Windows
now gets a SOCK_CLOEXEC, which pgwin32_socket() maps to
WSA_FLAG_NO_HANDLE_INHERIT, so you ask for a non-inheritable socket
the same way you do on Unix instead of v1's blanket change. The listen
sockets ask for it.

The macro is backend-only, since libpq passes SOCK_CLOEXEC whenever
it's defined and Winsock would reject it. I left accept4() out. It
only matters once we have threads.

Regarding 0002, the postmaster made its shared memory handle
inheritable on purpose, so children could use the same handle value.
That also gave it to everything the children run. If the server dies
while one of those is still running, the next postmaster fails with
"pre-existing shared memory block is still in use". Now the
postmaster's handle isn't inheritable, and save_backend_variables()
duplicates it into each child, the way the signal pipe already is.
Neither copy in the child is inheritable. That covers every child, not
just backends.

The test (src/test/postmaster/t/005_handle_inheritance.pl) uses
Harrison's idea of running the program with server-side COPY TO
PROGRAM. v1's test used \copy, which made the program psql's child, so
it couldn't have caught anything. The program is a small Perl script
that outlives an immediate stop. 0001 checks that the client sees its
connection drop and that the port can be bound again-- 0002 adds a
restart. All fail on master and pass with the patches. Tested with
MSVC on Windows 11 (regress, recovery, isolation, postmaster, pg_ctl,
libpq).

Harrison, thanks for looking at this. I didn't need force_initdb here.
If you saw failures without it, what were they?

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


Re: [PATCH] Fix socket handle inheritance on Windows

От:
Bryan Green <dbryan.green@gmail.com>
Дата:
Вложения:
On 11/1/2026 2:03 PM, Bryan Green wrote:
> On 11/6/2025 7:59 PM, Thomas Munro wrote:
>> On Fri, Nov 7, 2025 at 2:35 PM Bryan Green  wrote:
>>> If you agree, and I think you do, I will implement the SOCK_CLOEXEC
>>> abstraction and the socket_set_cloexec helper for this.  My bug fix will
>>> be the first user.
>>
>> +1
>>
>> Thanks for working on all this stuff.
>>
>>> I also need to handle the handles for shared memory and pipes.  I will
>>> probably just write those up tonight and share for review and discussion.
>>
>> Cool.
> 
> After a long wait... here's v2! It's two patches now. 0002 is the
> shared memory fix I said would follow back in November.
> 
> 0001 does sockets the way Thomas suggested. Backend code on Windows
> now gets a SOCK_CLOEXEC, which pgwin32_socket() maps to
> WSA_FLAG_NO_HANDLE_INHERIT, so you ask for a non-inheritable socket
> the same way you do on Unix instead of v1's blanket change. The listen
> sockets ask for it.
> 
> The macro is backend-only, since libpq passes SOCK_CLOEXEC whenever
> it's defined and Winsock would reject it. I left accept4() out. It
> only matters once we have threads.
> 
> Regarding 0002, the postmaster made its shared memory handle
> inheritable on purpose, so children could use the same handle value.
> That also gave it to everything the children run. If the server dies
> while one of those is still running, the next postmaster fails with
> "pre-existing shared memory block is still in use". Now the
> postmaster's handle isn't inheritable, and save_backend_variables()
> duplicates it into each child, the way the signal pipe already is.
> Neither copy in the child is inheritable. That covers every child, not
> just backends.
> 
> The test (src/test/postmaster/t/005_handle_inheritance.pl) uses
> Harrison's idea of running the program with server-side COPY TO
> PROGRAM. v1's test used \copy, which made the program psql's child, so
> it couldn't have caught anything. The program is a small Perl script
> that outlives an immediate stop. 0001 checks that the client sees its
> connection drop and that the port can be bound again-- 0002 adds a
> restart. All fail on master and pass with the patches. Tested with
> MSVC on Windows 11 (regress, recovery, isolation, postmaster, pg_ctl,
> libpq).
> 
> Harrison, thanks for looking at this. I didn't need force_initdb here.
> If you saw failures without it, what were they?
> 
Obviously...here are the patches.

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

Re: [PATCH] Fix socket handle inheritance on Windows

От:
Bryan Green <dbryan.green@gmail.com>
Дата:
Вложения:
On 11/1/2026 2:12 PM, Bryan Green wrote:
> On 11/1/2026 2:03 PM, Bryan Green wrote:
>> On 11/6/2025 7:59 PM, Thomas Munro wrote:
>>> On Fri, Nov 7, 2025 at 2:35 PM Bryan Green  wrote:
>>>> If you agree, and I think you do, I will implement the SOCK_CLOEXEC
>>>> abstraction and the socket_set_cloexec helper for this.  My bug fix will
>>>> be the first user.
>>>
>>> +1
>>>
>>> Thanks for working on all this stuff.
>>>
>>>> I also need to handle the handles for shared memory and pipes.  I will
>>>> probably just write those up tonight and share for review and discussion.
>>>
>>> Cool.
>>
>> After a long wait... here's v2! It's two patches now. 0002 is the
>> shared memory fix I said would follow back in November.
>>
>> 0001 does sockets the way Thomas suggested. Backend code on Windows
>> now gets a SOCK_CLOEXEC, which pgwin32_socket() maps to
>> WSA_FLAG_NO_HANDLE_INHERIT, so you ask for a non-inheritable socket
>> the same way you do on Unix instead of v1's blanket change. The listen
>> sockets ask for it.
>>
>> The macro is backend-only, since libpq passes SOCK_CLOEXEC whenever
>> it's defined and Winsock would reject it. I left accept4() out. It
>> only matters once we have threads.
>>
>> Regarding 0002, the postmaster made its shared memory handle
>> inheritable on purpose, so children could use the same handle value.
>> That also gave it to everything the children run. If the server dies
>> while one of those is still running, the next postmaster fails with
>> "pre-existing shared memory block is still in use". Now the
>> postmaster's handle isn't inheritable, and save_backend_variables()
>> duplicates it into each child, the way the signal pipe already is.
>> Neither copy in the child is inheritable. That covers every child, not
>> just backends.
>>
>> The test (src/test/postmaster/t/005_handle_inheritance.pl) uses
>> Harrison's idea of running the program with server-side COPY TO
>> PROGRAM. v1's test used \copy, which made the program psql's child, so
>> it couldn't have caught anything. The program is a small Perl script
>> that outlives an immediate stop. 0001 checks that the client sees its
>> connection drop and that the port can be bound again-- 0002 adds a
>> restart. All fail on master and pass with the patches. Tested with
>> MSVC on Windows 11 (regress, recovery, isolation, postmaster, pg_ctl,
>> libpq).
>>
>> Harrison, thanks for looking at this. I didn't need force_initdb here.
>> If you saw failures without it, what were they?
>>
> Obviously...here are the patches.
> 
v2's test assumed TCP, and failed on Windows CI, which uses Unix-domain
sockets (PG_TEST_USE_UNIX_SOCKETS). v3 has the node listen on 127.0.0.1
as well and checks that, and lets the program go if the test dies
early. No code changes otherwise. Passes on all CI tasks in my fork.

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