From 93abfb5653d56afad69fb258bda10794538fce52 Mon Sep 17 00:00:00 2001
From: Lucas Givord <lucas.givord@kitware.com>
Date: Mon, 25 Nov 2024 09:57:26 +0100
Subject: [PATCH] vtkSocket: fix win32 error handling

Previously when encountering an error during a Receive() call, server
hangs indefinitely as only WSAENOBUFS was checked.

For WSAECONNABORTED we cannot know if the disconnection is expected or
not so we don't output an error but log a trace for debugging purpose.

(cherry picked from commit c86e3aaaf9b3f2125764b40cc8b40b80b8700b99)
---
 Common/System/vtkSocket.cxx | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/Common/System/vtkSocket.cxx b/Common/System/vtkSocket.cxx
index 6272c386c73..8d37aed59ee 100644
--- a/Common/System/vtkSocket.cxx
+++ b/Common/System/vtkSocket.cxx
@@ -2,6 +2,7 @@
 // SPDX-License-Identifier: BSD-3-Clause
 #include "vtkSocket.h"
 
+#include "vtkLogger.h"
 #include "vtkObjectFactory.h"
 
 // The VTK_SOCKET_FAKE_API definition is given to the compiler
@@ -605,15 +606,27 @@ int vtkSocket::Receive(void* data, int length, int readFully /*=1*/)
     }
 
 #if defined(_WIN32) && !defined(__CYGWIN__)
-    if ((nRecvd == vtkSocketErrorReturnMacro) && (WSAGetLastError() == WSAENOBUFS))
+    if ((nRecvd == vtkSocketErrorReturnMacro))
     {
-      // On long messages, Windows recv sometimes fails with WSAENOBUFS, but
-      // will work if you try again.
-      if ((tries++ < 1000))
+      int lastError = WSAGetLastError();
+      if (lastError == WSAECONNABORTED)
       {
-        Sleep(1);
-        continue;
+        // From the receiver we cannot know if the connection abort is expected or not, this is why
+        // we output a trace instead of an error.
+        vtkLog(TRACE, "Socket error: connection abort.");
+        return 0;
       }
+      else if (lastError == WSAENOBUFS)
+      {
+        // On long messages, Windows recv sometimes fails with WSAENOBUFS, but
+        // will work if you try again.
+        if ((tries++ < 1000))
+        {
+          Sleep(1);
+          continue;
+        }
+      }
+
       vtkSocketErrorMacro(vtkErrnoMacro, "Socket error in call to recv.");
       return 0;
     }
-- 
GitLab