pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Alexander Lakhin <exclusion@gmail.com>
Cc: Victor Spirin <v.spirin@postgrespro.ru>
Cc: pgsql-hackers@lists.postgresql.org
Subject: Re: Sometimes the output to the stdout in Windows disappears
Date: Fri, 16 Oct 2020 12:18:32 -0400
Message-ID: <4156857.1602865112@sss.pgh.pa.us> (raw)
In-Reply-To: <ee02eaa2-03f7-74ea-bbdf-3196e506bae3@gmail.com>
References: <ac976d8c-03df-d6b8-025c-15a2de8d9af1@postgrespro.ru>
	<4124040.1598898684@sss.pgh.pa.us>
	<b92a7e09-9a60-d76b-e226-7e191fdc6f34@postgrespro.ru>
	<855416.1600022228@sss.pgh.pa.us>
	<ee02eaa2-03f7-74ea-bbdf-3196e506bae3@gmail.com>

Alexander Lakhin <exclusion@gmail.com> writes:
> I've managed to make a simple reproducer. Please look at the patch attached.
> There are two things crucial for reproducing the bug:
>     ioctlsocket(sock, FIONBIO, &ioctlsocket_ret); // from pgwin32_socket()
> and
>     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





view thread (16+ messages)  latest in thread

Message-ID: <4156857.1602865112@sss.pgh.pa.us>
Permalink:  ../4156857.1602865112@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/4156857.1602865112@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, exclusion@gmail.com, v.spirin@postgrespro.ru, pgsql-hackers@lists.postgresql.org
  Subject: Re: Sometimes the output to the stdout in Windows disappears
  In-Reply-To: <4156857.1602865112@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox