Skip to content

Commit 8832264

Browse files
committed
In libpq for Windows, call WSAStartup once and WSACleanup not at all.
The Windows documentation insists that every WSAStartup call should have a matching WSACleanup call. However, if that ever had actual relevance, it wasn't in this century. Every remotely-modern Windows kernel is capable of cleaning up when a process exits without doing that, and must be so to avoid resource leaks in case of a process crash. Moreover, Postgres backends have done WSAStartup without WSACleanup since commit 4cdf51e in 2004, and we've never seen any indication of a problem with that. libpq's habit of doing WSAStartup during connection start and WSACleanup during shutdown is also rather inefficient, since a series of non-overlapping connection requests leads to repeated, quite expensive DLL unload/reload cycles. We document a workaround for that (having the application call WSAStartup for itself), but that's just a kluge. It's also worth noting that it's far from uncommon for applications to exit without doing PQfinish, and we've not heard reports of trouble from that either. However, the real reason for acting on this is that recent experiments by Alexander Lakhin show that calling WSACleanup during PQfinish is triggering the symptom we occasionally see that a process using libpq fails to emit expected stdio output. Therefore, let's change libpq so that it calls WSAStartup only once per process, during the first connection attempt, and never calls WSACleanup at all. While at it, get rid of the only other WSACleanup call in our code tree, in pg_dump/parallel.c; that presumably is equally useless. Back-patch of HEAD commit 7d00a6b. Discussion: https://postgr.es/m/ac976d8c-03df-d6b8-025c-15a2de8d9af1@postgrespro.ru
1 parent aa560d3 commit 8832264

File tree

3 files changed

+18
-44
lines changed

3 files changed

+18
-44
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,6 @@
9191
</para>
9292
</warning>
9393

94-
<note>
95-
<para>
96-
On Windows, there is a way to improve performance if a single
97-
database connection is repeatedly started and shutdown. Internally,
98-
libpq calls <function>WSAStartup()</function> and <function>WSACleanup()</function> for connection startup
99-
and shutdown, respectively. <function>WSAStartup()</function> increments an internal
100-
Windows library reference count which is decremented by <function>WSACleanup()</function>.
101-
When the reference count is just one, calling <function>WSACleanup()</function> frees
102-
all resources and all DLLs are unloaded. This is an expensive
103-
operation. To avoid this, an application can manually call
104-
<function>WSAStartup()</function> so resources will not be freed when the last database
105-
connection is closed.
106-
</para>
107-
</note>
108-
10994
<variablelist>
11095
<varlistentry id="libpq-pqconnectdbparams">
11196
<term><function>PQconnectdbParams</function><indexterm><primary>PQconnectdbParams</primary></indexterm></term>

src/bin/pg_dump/parallel.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,6 @@ static char *readMessageFromPipe(int fd);
234234
(strcmp(msg, pattern) == 0)
235235

236236

237-
/*
238-
* Shutdown callback to clean up socket access
239-
*/
240-
#ifdef WIN32
241-
static void
242-
shutdown_parallel_dump_utils(int code, void *unused)
243-
{
244-
/* Call the cleanup function only from the main thread */
245-
if (mainThreadId == GetCurrentThreadId())
246-
WSACleanup();
247-
}
248-
#endif
249-
250237
/*
251238
* Initialize parallel dump support --- should be called early in process
252239
* startup. (Currently, this is called whether or not we intend parallel
@@ -272,8 +259,7 @@ init_parallel_dump_utils(void)
272259
fprintf(stderr, _("%s: WSAStartup failed: %d\n"), progname, err);
273260
exit_nicely(1);
274261
}
275-
/* ... and arrange to shut it down at exit */
276-
on_exit_nicely(shutdown_parallel_dump_utils, NULL);
262+
277263
parallel_init_done = true;
278264
}
279265
#endif

src/interfaces/libpq/fe-connect.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,23 +3437,30 @@ makeEmptyPGconn(void)
34373437
#ifdef WIN32
34383438

34393439
/*
3440-
* Make sure socket support is up and running.
3440+
* Make sure socket support is up and running in this process.
3441+
*
3442+
* Note: the Windows documentation says that we should eventually do a
3443+
* matching WSACleanup() call, but experience suggests that that is at
3444+
* least as likely to cause problems as fix them. So we don't.
34413445
*/
3442-
WSADATA wsaData;
3446+
static bool wsastartup_done = false;
34433447

3444-
if (WSAStartup(MAKEWORD(1, 1), &wsaData))
3445-
return NULL;
3448+
if (!wsastartup_done)
3449+
{
3450+
WSADATA wsaData;
3451+
3452+
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
3453+
return NULL;
3454+
wsastartup_done = true;
3455+
}
3456+
3457+
/* Forget any earlier error */
34463458
WSASetLastError(0);
3447-
#endif
3459+
#endif /* WIN32 */
34483460

34493461
conn = (PGconn *) malloc(sizeof(PGconn));
34503462
if (conn == NULL)
3451-
{
3452-
#ifdef WIN32
3453-
WSACleanup();
3454-
#endif
34553463
return conn;
3456-
}
34573464

34583465
/* Zero all pointers and booleans */
34593466
MemSet(conn, 0, sizeof(PGconn));
@@ -3621,10 +3628,6 @@ freePGconn(PGconn *conn)
36213628
termPQExpBuffer(&conn->workBuffer);
36223629

36233630
free(conn);
3624-
3625-
#ifdef WIN32
3626-
WSACleanup();
3627-
#endif
36283631
}
36293632

36303633
/*

0 commit comments

Comments
 (0)