gai_strerror() is not thread-safe on Windows

From: Thomas Munro <thomas(dot)munro(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: gai_strerror() is not thread-safe on Windows
Date: 2023-12-04 03:21:24
Message-ID: CA+hUKGKz+F9d2PTiXwfYV7qJw+Wg2jzACgSDgPizUw7UG=i58A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

Commit 5579388d removed a bunch of dead code, formerly needed for old
systems that lacked getaddrinfo() in the early days of IPv6. We
already used the system getaddrinfo() via either configure-time tests
(Unix) or runtime tests (Windows using attempt-to-find-with-dlsym that
always succeeded on modern systems), so no modern system needed the
fallback code, except for one small detail:

getaddrinfo() has a companion function to spit out human readable
error messages, and although Windows has that too, it's not thread
safe[1]. libpq shouldn't call it, or else an unlucky multi-threaded
program might see an error message messed up by another thread.

Here's a patch to put that bit back. It's simpler than before: the
original replacement had a bunch of #ifdefs for various historical
reasons, but now we can just handle the 8 documented EAI errors on
Windows.

Noticed while wondering why the list of symbols reported in bug #18219
didn't include gai_strerrorA. That turned out to be because it is
static inline in ws2tcpip.h, and its definition set alarm bells
ringing. Avoid.

[1] https://learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfo

Attachment Content-Type Size
0001-Fix-gai_strerror-thread-safety-on-Windows.patch text/x-patch 4.2 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Zhijie Hou (Fujitsu) 2023-12-04 03:33:36 RE: Synchronizing slots from primary to standby
Previous Message Tom Lane 2023-12-04 03:10:11 Re: connection timeout hint