From bd7bafcb18b509c17f57c84252e93fb159168a54 Mon Sep 17 00:00:00 2001 From: Alex Sweet Date: Sun, 31 Aug 2025 19:26:12 -0700 Subject: [PATCH 1/2] wslrelay KD: stay open after first disconnect When the relay is running in kernel debug mode, stay open after the client disconnects and continue accepting connections. --- src/windows/wslrelay/main.cpp | 37 ++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/windows/wslrelay/main.cpp b/src/windows/wslrelay/main.cpp index c3fad7e59..4e88dd70d 100644 --- a/src/windows/wslrelay/main.cpp +++ b/src/windows/wslrelay/main.cpp @@ -101,7 +101,7 @@ try // Ensure that the other end of the pipe has connected. wsl::windows::common::helpers::ConnectPipe(pipe.get(), (15 * 1000), {exitEvent.get()}); - // Bind, listen, and accept a connection on the specified port. + // Bind and listen on the specified port (once) const wil::unique_socket listenSocket(WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, nullptr, 0, WSA_FLAG_OVERLAPPED)); THROW_LAST_ERROR_IF(!listenSocket); @@ -113,14 +113,33 @@ try THROW_LAST_ERROR_IF(listen(listenSocket.get(), 1) == SOCKET_ERROR); - const wil::unique_socket socket(WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, nullptr, 0, WSA_FLAG_OVERLAPPED)); - THROW_LAST_ERROR_IF(!socket); - - wsl::windows::common::socket::Accept(listenSocket.get(), socket.get(), INFINITE, exitEvent.get()); - - // Begin the relay. - wsl::windows::common::relay::BidirectionalRelay( - reinterpret_cast(socket.get()), pipe.get(), 0x1000, wsl::windows::common::relay::RelayFlags::LeftIsSocket); + for (;;) + { + if (exitEvent && WaitForSingleObject(exitEvent.get(), 0) == WAIT_OBJECT_0) + { + break; + } + + const wil::unique_socket socket(WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, nullptr, 0, WSA_FLAG_OVERLAPPED)); + THROW_LAST_ERROR_IF(!socket); + + try + { + wsl::windows::common::socket::Accept(listenSocket.get(), socket.get(), INFINITE, exitEvent.get()); + + // Begin the relay for this connection + wsl::windows::common::relay::BidirectionalRelay( + reinterpret_cast(socket.get()), pipe.get(), 0x1000, + wsl::windows::common::relay::RelayFlags::LeftIsSocket); + } + catch (...) + { + LOG_CAUGHT_EXCEPTION(); + + // Small delay to prevent tight loop on persistent errors + Sleep(100); + } + } break; } From 5d0f62a7ad1619b3a69a40b63370e9aecc5e9f89 Mon Sep 17 00:00:00 2001 From: Alex Sweet <110490696+asweet-confluent@users.noreply.github.com> Date: Wed, 3 Sep 2025 16:25:39 -0700 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/windows/wslrelay/main.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/windows/wslrelay/main.cpp b/src/windows/wslrelay/main.cpp index 4e88dd70d..a3636ed7f 100644 --- a/src/windows/wslrelay/main.cpp +++ b/src/windows/wslrelay/main.cpp @@ -115,10 +115,6 @@ try for (;;) { - if (exitEvent && WaitForSingleObject(exitEvent.get(), 0) == WAIT_OBJECT_0) - { - break; - } const wil::unique_socket socket(WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, nullptr, 0, WSA_FLAG_OVERLAPPED)); THROW_LAST_ERROR_IF(!socket);