Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit407580a

Browse files
committed
In libpq for Windows, call WSAStartup once and WSACleanup not at all.
The Windows documentation insists that every WSAStartup call shouldhave a matching WSACleanup call. However, if that ever had actualrelevance, it wasn't in this century. Every remotely-modern Windowskernel is capable of cleaning up when a process exits without doingthat, and must be so to avoid resource leaks in case of a processcrash. Moreover, Postgres backends have done WSAStartup withoutWSACleanup since commit4cdf51e in 2004, and we've never seen anyindication of a problem with that.libpq's habit of doing WSAStartup during connection start andWSACleanup during shutdown is also rather inefficient, since aseries of non-overlapping connection requests leads to repeated,quite expensive DLL unload/reload cycles. We document a workaroundfor that (having the application call WSAStartup for itself), butthat's just a kluge. It's also worth noting that it's far fromuncommon for applications to exit without doing PQfinish, andwe've not heard reports of trouble from that either.However, the real reason for acting on this is that recentexperiments by Alexander Lakhin show that calling WSACleanupduring PQfinish is triggering the symptom we occasionally seethat a process using libpq fails to emit expected stdio output.Therefore, let's change libpq so that it calls WSAStartup onlyonce per process, during the first connection attempt, and nevercalls WSACleanup at all.While at it, get rid of the only other WSACleanup call in our codetree, in pg_dump/parallel.c; that presumably is equally useless.Back-patch of HEAD commit7d00a6b.Discussion:https://postgr.es/m/ac976d8c-03df-d6b8-025c-15a2de8d9af1@postgrespro.ru
1 parentf0e92bc commit407580a

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
@@ -230,19 +230,6 @@ static char *readMessageFromPipe(int fd);
230230
(strncmp(msg, prefix, strlen(prefix)) == 0)
231231

232232

233-
/*
234-
* Shutdown callback to clean up socket access
235-
*/
236-
#ifdefWIN32
237-
staticvoid
238-
shutdown_parallel_dump_utils(intcode,void*unused)
239-
{
240-
/* Call the cleanup function only from the main thread */
241-
if (mainThreadId==GetCurrentThreadId())
242-
WSACleanup();
243-
}
244-
#endif
245-
246233
/*
247234
* Initialize parallel dump support --- should be called early in process
248235
* startup. (Currently, this is called whether or not we intend parallel
@@ -268,8 +255,7 @@ init_parallel_dump_utils(void)
268255
pg_log_error("WSAStartup failed: %d",err);
269256
exit_nicely(1);
270257
}
271-
/* ... and arrange to shut it down at exit */
272-
on_exit_nicely(shutdown_parallel_dump_utils,NULL);
258+
273259
parallel_init_done= true;
274260
}
275261
#endif

‎src/interfaces/libpq/fe-connect.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3802,23 +3802,30 @@ makeEmptyPGconn(void)
38023802
#ifdefWIN32
38033803

38043804
/*
3805-
* Make sure socket support is up and running.
3805+
* Make sure socket support is up and running in this process.
3806+
*
3807+
* Note: the Windows documentation says that we should eventually do a
3808+
* matching WSACleanup() call, but experience suggests that that is at
3809+
* least as likely to cause problems as fix them. So we don't.
38063810
*/
3807-
WSADATAwsaData;
3811+
staticboolwsastartup_done= false;
38083812

3809-
if (WSAStartup(MAKEWORD(1,1),&wsaData))
3810-
returnNULL;
3813+
if (!wsastartup_done)
3814+
{
3815+
WSADATAwsaData;
3816+
3817+
if (WSAStartup(MAKEWORD(1,1),&wsaData)!=0)
3818+
returnNULL;
3819+
wsastartup_done= true;
3820+
}
3821+
3822+
/* Forget any earlier error */
38113823
WSASetLastError(0);
3812-
#endif
3824+
#endif/* WIN32 */
38133825

38143826
conn= (PGconn*)malloc(sizeof(PGconn));
38153827
if (conn==NULL)
3816-
{
3817-
#ifdefWIN32
3818-
WSACleanup();
3819-
#endif
38203828
returnconn;
3821-
}
38223829

38233830
/* Zero all pointers and booleans */
38243831
MemSet(conn,0,sizeof(PGconn));
@@ -3994,10 +4001,6 @@ freePGconn(PGconn *conn)
39944001
termPQExpBuffer(&conn->workBuffer);
39954002

39964003
free(conn);
3997-
3998-
#ifdefWIN32
3999-
WSACleanup();
4000-
#endif
40014004
}
40024005

40034006
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp