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 1kTSRI-0001hn-Jz for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Oct 2020 16:18:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kTSRH-0006pG-BY for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Oct 2020 16:18:43 +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 1kTSRH-0006p9-4m for pgsql-hackers@lists.postgresql.org; Fri, 16 Oct 2020 16:18:43 +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 1kTSRA-0000Ps-Cl for pgsql-hackers@lists.postgresql.org; Fri, 16 Oct 2020 16:18:42 +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 09GGIWqU4156858; Fri, 16 Oct 2020 12:18:32 -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: References: <4124040.1598898684@sss.pgh.pa.us> <855416.1600022228@sss.pgh.pa.us> Comments: In-reply-to Alexander Lakhin message dated "Fri, 16 Oct 2020 19:00:00 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <4156856.1602865112.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 16 Oct 2020 12:18:32 -0400 Message-ID: <4156857.1602865112@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alexander Lakhin writes: > I've managed to make a simple reproducer. Please look at the patch attac= hed. > There are two things crucial for reproducing the bug: > =C2=A0=C2=A0=C2=A0 ioctlsocket(sock, FIONBIO, &ioctlsocket_ret); // from= pgwin32_socket() > and > =C2=A0=C2=A0=C2=A0 WSACleanup(); Oh, very interesting. Now that you have it somewhat in captivity, maybe you could determine some things: 1. Is it only stdout that's affected? What of other stdio streams? (Note that testing stderr might be tricky because it's probably line-buffered.) 2. Does an fflush() just before, or just after, WSACleanup() fix it? > I see no solution for this on the postgres side for now, but this > information about Windows quirks could be useful in case someone > stumbled upon it too. Depending on your answers to the above, maybe some hack like this would be acceptable: free(conn); = #ifdef WIN32 + fflush(NULL); WSACleanup(); #endif } It's not very nice for a library to be doing global things like that, but if the alternative is loss of output, maybe we should. But wait a minute: I just looked at Microsoft's docs [1] and found In a multithreaded environment, WSACleanup terminates Windows Sockets operations for all threads. This makes me (a) wonder if that explains the side-effects on stdio, and (b) question why libpq is calling WSACleanup at all. What if we arranged to call WSAStartup just once, during the first libpq connection-open in a process, and then never did WSACleanup? Surely the OS can cope with that, and it eliminates any risk that WSACleanup breaks something. regards, tom lane [1] https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-= wsacleanup