From 079c3dce7c4580a797267b6e42b33399606b4f9d Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Wed, 17 Nov 2021 21:04:45 +0100 Subject: [PATCH] Windows: Gracefully close the socket on process exit This is to avoid a hard close of the socket with tcp RST flag, which in turn can lead to a dropped error message on a Windows client. --- src/backend/libpq/pqcomm.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c index 9ebba025c..7a81fa913 100644 --- a/src/backend/libpq/pqcomm.c +++ b/src/backend/libpq/pqcomm.c @@ -286,6 +286,18 @@ socket_close(int code, Datum arg) * We do set sock to PGINVALID_SOCKET to prevent any further I/O, * though. */ + +#ifdef WIN32 + /* + * On Windows we still do an explicit close here, because Windows + * closes sockets with RST flag instead of FIN at process termination. + * On the client side it then leads to a WSAECONNRESET error, which + * can (depending on the timing) drop the last error message, leading + * to a valuable information loss for the user. + */ + closesocket(MyProcPort->sock); +#endif + MyProcPort->sock = PGINVALID_SOCKET; } } -- 2.32.0