Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kTrC9-0006l3-LK for pgsql-hackers@arkaria.postgresql.org; Sat, 17 Oct 2020 18:44:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kTrC8-00057P-Gn for pgsql-hackers@arkaria.postgresql.org; Sat, 17 Oct 2020 18:44:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kTrC8-00057I-9s for pgsql-hackers@lists.postgresql.org; Sat, 17 Oct 2020 18:44:44 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kTrC5-0007Jd-7t for pgsql-hackers@lists.postgresql.org; Sat, 17 Oct 2020 18:44:43 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 09HIibNi132800; Sat, 17 Oct 2020 14:44:37 -0400 From: Tom Lane To: Alexander Lakhin cc: Victor Spirin , pgsql-hackers@lists.postgresql.org Subject: Re: Sometimes the output to the stdout in Windows disappears In-reply-to: <117919.1602949857@sss.pgh.pa.us> References: <4124040.1598898684@sss.pgh.pa.us> <855416.1600022228@sss.pgh.pa.us> <4156857.1602865112@sss.pgh.pa.us> <117919.1602949857@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Sat, 17 Oct 2020 11:50:57 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <132667.1602960105.0@sss.pgh.pa.us> Date: Sat, 17 Oct 2020 14:44:37 -0400 Message-ID: <132799.1602960277@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <132667.1602960105.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable I wrote: > Alexander Lakhin writes: >> What bothers me is: >> There must be a call to *WSACleanup* for each successful call to >> WSAStartup >> . > Yeah, that is a very odd statement. Surely, the Windows kernel manages > to cope if a program crashes without having done that. So what exactly > is the downside of intentionally not doing it? A bit of grepping showed me that the backend, initdb, and pg_regress all call WSAStartup without ever doing WSACleanup, and we've seen no ill effects from that. So it seems clear that this documentation can safely be ignored. I propose the attached patch. If this doesn't cause buildfarm problems, perhaps we should back-patch it. BTW, I notice that libpq is asking WSAStartup for Winsock version 1.1, which is remarkably ancient. Almost everyplace else is asking for version 2.2, which has been current for a decade or two. Shouldn't we update that? (It occurs to me to wonder if this in itself is some kind of problem; I wonder how well Winsock works when there are requests for different API versions in the same program.) regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="remove-WSACleanup-calls.patch"; charset="us-ascii" Content-ID: <132667.1602960105.2@sss.pgh.pa.us> Content-Description: remove-WSACleanup-calls.patch Content-Transfer-Encoding: quoted-printable diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 3315f1dd05..de60281fcb 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -91,21 +91,6 @@ = - - - On Windows, there is a way to improve performance if a single - database connection is repeatedly started and shutdown. Internally, - libpq calls WSAStartup() and WSAClean= up() for connection startup - and shutdown, respectively. WSAStartup() incre= ments an internal - Windows library reference count which is decremented by WS= ACleanup(). - When the reference count is just one, calling WSACleanup()= frees - all resources and all DLLs are unloaded. This is an expensive - operation. To avoid this, an application can manually call - WSAStartup() so resources will not be freed whe= n the last database - connection is closed. - - - PQconnectdbParamsPQco= nnectdbParams diff --git a/src/bin/pg_dump/parallel.c b/src/bin/pg_dump/parallel.c index a967e11378..b51cc76c7d 100644 --- a/src/bin/pg_dump/parallel.c +++ b/src/bin/pg_dump/parallel.c @@ -229,19 +229,6 @@ static char *readMessageFromPipe(int fd); (strncmp(msg, prefix, strlen(prefix)) =3D=3D 0) = = -/* - * Shutdown callback to clean up socket access - */ -#ifdef WIN32 -static void -shutdown_parallel_dump_utils(int code, void *unused) -{ - /* Call the cleanup function only from the main thread */ - if (mainThreadId =3D=3D GetCurrentThreadId()) - WSACleanup(); -} -#endif - /* * Initialize parallel dump support --- should be called early in process * startup. (Currently, this is called whether or not we intend parallel @@ -267,8 +254,7 @@ init_parallel_dump_utils(void) pg_log_error("WSAStartup failed: %d", err); exit_nicely(1); } - /* ... and arrange to shut it down at exit */ - on_exit_nicely(shutdown_parallel_dump_utils, NULL); + parallel_init_done =3D true; } #endif diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-c= onnect.c index af27fee6b5..704c9e2f79 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -3871,23 +3871,30 @@ makeEmptyPGconn(void) #ifdef WIN32 = /* - * Make sure socket support is up and running. + * Make sure socket support is up and running in this process. + * + * Note: the Windows documentation says that we should eventually do a + * matching WSACleanup() call, but experience suggests that that is at + * least as likely to cause problems as fix them. So we don't. */ - WSADATA wsaData; + static bool wsastartup_done =3D false; = - if (WSAStartup(MAKEWORD(1, 1), &wsaData)) - return NULL; + if (!wsastartup_done) + { + WSADATA wsaData; + + if (WSAStartup(MAKEWORD(1, 1), &wsaData) !=3D 0) + return NULL; + wsastartup_done =3D true; + } + + /* Forget any earlier error */ WSASetLastError(0); -#endif +#endif /* WIN32 */ = conn =3D (PGconn *) malloc(sizeof(PGconn)); if (conn =3D=3D NULL) - { -#ifdef WIN32 - WSACleanup(); -#endif return conn; - } = /* Zero all pointers and booleans */ MemSet(conn, 0, sizeof(PGconn)); @@ -4080,10 +4087,6 @@ freePGconn(PGconn *conn) termPQExpBuffer(&conn->workBuffer); = free(conn); - -#ifdef WIN32 - WSACleanup(); -#endif } = /* ------- =_aaaaaaaaaa0--