From 0b7848b2887505746d35b5b18d13741be6930365 Mon Sep 17 00:00:00 2001 From: "Shawn Yuan (from Dev Box)" Date: Thu, 20 Aug 2026 17:13:03 +0800 Subject: [PATCH 1/4] Handle Linux failures from GNS callbacks --- src/windows/common/WslCoreNetworkingSupport.h | 10 ++++++++++ src/windows/service/exe/MirroredNetworking.cpp | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/windows/common/WslCoreNetworkingSupport.h b/src/windows/common/WslCoreNetworkingSupport.h index f1a0b726ca..13fa6aa0df 100644 --- a/src/windows/common/WslCoreNetworkingSupport.h +++ b/src/windows/common/WslCoreNetworkingSupport.h @@ -136,6 +136,16 @@ inline constexpr auto* c_ipv4TestRequestTargetA = "www.msftconnecttest.com"; inline constexpr auto* c_ipv6TestRequestTarget = L"ipv6.msftconnecttest.com"; inline constexpr auto* c_ipv6TestRequestTargetA = "ipv6.msftconnecttest.com"; +inline HRESULT GetGnsCallbackResult(LX_MESSAGE_TYPE messageType, HRESULT transportResult, int linuxResultCode) noexcept +{ + if (FAILED(transportResult) || messageType == LxGnsMessageConnectTestRequest || linuxResultCode == 0) + { + return transportResult; + } + + return E_UNEXPECTED; +} + inline constexpr GUID c_wslFirewallVmCreatorId = {0x40E0AC32, 0x46A5, 0x438A, {0xA0, 0xB2, 0x2B, 0x47, 0x9E, 0x8F, 0x2E, 0x90}}; inline constexpr auto c_networkAdapterPrefix = L"VirtualMachine/Devices/NetworkAdapters/"; diff --git a/src/windows/service/exe/MirroredNetworking.cpp b/src/windows/service/exe/MirroredNetworking.cpp index 417006ea73..ee396288b9 100644 --- a/src/windows/service/exe/MirroredNetworking.cpp +++ b/src/windows/service/exe/MirroredNetworking.cpp @@ -695,7 +695,8 @@ try TraceLoggingValue(retryCount, "retryCount")); ++retryCount; - return hr; + return networking::GetGnsCallbackResult( + messageType, hr, returnedValueFromGns ? *returnedValueFromGns : 0); }, std::chrono::milliseconds(100), std::chrono::seconds(3)); From 8484b7f7935874b212154b57460cc717da7f5604 Mon Sep 17 00:00:00 2001 From: "Shawn Yuan (from Dev Box)" Date: Thu, 20 Aug 2026 17:19:27 +0800 Subject: [PATCH 2/4] Update tests --- test/windows/NetworkTests.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/windows/NetworkTests.cpp b/test/windows/NetworkTests.cpp index b06aae02b9..2499994f1b 100644 --- a/test/windows/NetworkTests.cpp +++ b/test/windows/NetworkTests.cpp @@ -18,6 +18,7 @@ Module Name: #include "wslpolicies.h" #include "hns_schema.h" #include "WslCoreNetworkEndpointSettings.h" +#include "WslCoreNetworkingSupport.h" #include #include @@ -3950,6 +3951,32 @@ class NetworkTests } }; +class GnsCallbackResultTests +{ + WSL_TEST_CLASS(GnsCallbackResultTests) + + TEST_METHOD(GnsCallbackSuccessfulTransportAndLinuxResultSucceeds) + { + VERIFY_SUCCEEDED(wsl::core::networking::GetGnsCallbackResult(LxGnsMessageDeviceSettingRequest, S_OK, 0)); + } + + TEST_METHOD(GnsCallbackSuccessfulTransportAndLinuxFailureFails) + { + VERIFY_FAILED(wsl::core::networking::GetGnsCallbackResult(LxGnsMessageDeviceSettingRequest, S_OK, -1)); + } + + TEST_METHOD(GnsCallbackTransportFailureFails) + { + VERIFY_ARE_EQUAL( + E_ABORT, wsl::core::networking::GetGnsCallbackResult(LxGnsMessageDeviceSettingRequest, E_ABORT, 0)); + } + + TEST_METHOD(GnsCallbackConnectTestBusinessResultSucceeds) + { + VERIFY_SUCCEEDED(wsl::core::networking::GetGnsCallbackResult(LxGnsMessageConnectTestRequest, S_OK, -1)); + } +}; + class MirroredTests { WSL_TEST_CLASS(MirroredTests) From bd5d63dba1385ea2cc79009416cfccdd92573628 Mon Sep 17 00:00:00 2001 From: "Shawn Yuan (from Dev Box)" Date: Thu, 20 Aug 2026 17:46:13 +0800 Subject: [PATCH 3/4] Address GNS callback review feedback Co-Authored-By: Claude --- src/windows/common/WslCoreNetworkingSupport.h | 2 +- src/windows/service/exe/MirroredNetworking.cpp | 4 ++-- test/windows/NetworkTests.cpp | 5 ++--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/windows/common/WslCoreNetworkingSupport.h b/src/windows/common/WslCoreNetworkingSupport.h index 13fa6aa0df..6e584ac8fd 100644 --- a/src/windows/common/WslCoreNetworkingSupport.h +++ b/src/windows/common/WslCoreNetworkingSupport.h @@ -143,7 +143,7 @@ inline HRESULT GetGnsCallbackResult(LX_MESSAGE_TYPE messageType, HRESULT transpo return transportResult; } - return E_UNEXPECTED; + return E_FAIL; } inline constexpr GUID c_wslFirewallVmCreatorId = {0x40E0AC32, 0x46A5, 0x438A, {0xA0, 0xB2, 0x2B, 0x47, 0x9E, 0x8F, 0x2E, 0x90}}; diff --git a/src/windows/service/exe/MirroredNetworking.cpp b/src/windows/service/exe/MirroredNetworking.cpp index ee396288b9..9d1ba3e406 100644 --- a/src/windows/service/exe/MirroredNetworking.cpp +++ b/src/windows/service/exe/MirroredNetworking.cpp @@ -695,8 +695,8 @@ try TraceLoggingValue(retryCount, "retryCount")); ++retryCount; - return networking::GetGnsCallbackResult( - messageType, hr, returnedValueFromGns ? *returnedValueFromGns : 0); + return THROW_IF_FAILED( + networking::GetGnsCallbackResult(messageType, hr, returnedValueFromGns ? *returnedValueFromGns : 0)); }, std::chrono::milliseconds(100), std::chrono::seconds(3)); diff --git a/test/windows/NetworkTests.cpp b/test/windows/NetworkTests.cpp index 2499994f1b..e7380655fb 100644 --- a/test/windows/NetworkTests.cpp +++ b/test/windows/NetworkTests.cpp @@ -3962,13 +3962,12 @@ class GnsCallbackResultTests TEST_METHOD(GnsCallbackSuccessfulTransportAndLinuxFailureFails) { - VERIFY_FAILED(wsl::core::networking::GetGnsCallbackResult(LxGnsMessageDeviceSettingRequest, S_OK, -1)); + VERIFY_ARE_EQUAL(E_FAIL, wsl::core::networking::GetGnsCallbackResult(LxGnsMessageDeviceSettingRequest, S_OK, -1)); } TEST_METHOD(GnsCallbackTransportFailureFails) { - VERIFY_ARE_EQUAL( - E_ABORT, wsl::core::networking::GetGnsCallbackResult(LxGnsMessageDeviceSettingRequest, E_ABORT, 0)); + VERIFY_ARE_EQUAL(E_ABORT, wsl::core::networking::GetGnsCallbackResult(LxGnsMessageDeviceSettingRequest, E_ABORT, 0)); } TEST_METHOD(GnsCallbackConnectTestBusinessResultSucceeds) From 180ac23d6135ca3e13a5bcb8f8742e1498521f64 Mon Sep 17 00:00:00 2001 From: "Shawn Yuan (from Dev Box)" Date: Thu, 20 Aug 2026 18:31:56 +0800 Subject: [PATCH 4/4] Address GNS callback review comments Co-Authored-By: Claude --- .../service/exe/MirroredNetworking.cpp | 73 ++++++++++--------- test/windows/NetworkTests.cpp | 5 ++ 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/windows/service/exe/MirroredNetworking.cpp b/src/windows/service/exe/MirroredNetworking.cpp index 9d1ba3e406..5cc22319f5 100644 --- a/src/windows/service/exe/MirroredNetworking.cpp +++ b/src/windows/service/exe/MirroredNetworking.cpp @@ -667,42 +667,43 @@ try WI_ASSERT(WI_IsFlagSet(callbackFlags, wsl::core::networking::GnsCallbackFlags::Wait)); } - auto sendGnsMessage = - [this, messageType, capturedNotificationString = std::move(notificationString), callbackFlags, returnedValueFromGns]() mutable { - try - { - auto retryCount = 0ul; - // RetryWithTimeout throws if fails after the timeout has elapsed - which is caught and returned by m_gnsMessageQueue below - return wsl::shared::retry::RetryWithTimeout( - [&]() { - const auto hr = wil::ResultFromException([&] { - if (returnedValueFromGns && WI_IsFlagSet(callbackFlags, wsl::core::networking::GnsCallbackFlags::Wait)) - { - *returnedValueFromGns = - m_gnsChannel.SendNetworkDeviceMessageReturnResult(messageType, capturedNotificationString.c_str()); - } - else - { - m_gnsChannel.SendNetworkDeviceMessage(messageType, capturedNotificationString.c_str()); - } - }); - WSL_LOG( - "MirroredNetworking::NetworkManagerGnsMessageCallback", - TraceLoggingValue(ToString(messageType), "messageType"), - TraceLoggingValue(capturedNotificationString.c_str(), "notificationString"), - TraceLoggingValue(hr, "hr"), - TraceLoggingValue(returnedValueFromGns ? *returnedValueFromGns : 0xFFFFFFFF, "returnedValueFromGns"), - TraceLoggingValue(retryCount, "retryCount")); - - ++retryCount; - return THROW_IF_FAILED( - networking::GetGnsCallbackResult(messageType, hr, returnedValueFromGns ? *returnedValueFromGns : 0)); - }, - std::chrono::milliseconds(100), - std::chrono::seconds(3)); - } - CATCH_RETURN() - }; + auto sendGnsMessage = [this, messageType, capturedNotificationString = std::move(notificationString), callbackFlags, returnedValueFromGns]() mutable { + try + { + auto retryCount = 0ul; + auto sendMessage = [&]() { + const auto hr = wil::ResultFromException([&] { + if (returnedValueFromGns && WI_IsFlagSet(callbackFlags, wsl::core::networking::GnsCallbackFlags::Wait)) + { + *returnedValueFromGns = + m_gnsChannel.SendNetworkDeviceMessageReturnResult(messageType, capturedNotificationString.c_str()); + } + else + { + m_gnsChannel.SendNetworkDeviceMessage(messageType, capturedNotificationString.c_str()); + } + }); + const bool hasLinuxResult = returnedValueFromGns != nullptr; + const int linuxResultCode = hasLinuxResult ? *returnedValueFromGns : 0; + WSL_LOG( + "MirroredNetworking::NetworkManagerGnsMessageCallback", + TraceLoggingValue(ToString(messageType), "messageType"), + TraceLoggingValue(capturedNotificationString.c_str(), "notificationString"), + TraceLoggingValue(hr, "hr"), + TraceLoggingValue(hasLinuxResult, "hasLinuxResult"), + TraceLoggingValue(linuxResultCode, "linuxResultCode"), + TraceLoggingValue(retryCount, "retryCount")); + + ++retryCount; + THROW_IF_FAILED(hr); + return networking::GetGnsCallbackResult(messageType, hr, linuxResultCode); + }; + + // RetryWithTimeout throws if transport fails after the timeout has elapsed. + return wsl::shared::retry::RetryWithTimeout(sendMessage, std::chrono::milliseconds(100), std::chrono::seconds(3)); + } + CATCH_RETURN() + }; if (WI_IsFlagSet(callbackFlags, wsl::core::networking::GnsCallbackFlags::Wait)) { diff --git a/test/windows/NetworkTests.cpp b/test/windows/NetworkTests.cpp index e7380655fb..65d603dd11 100644 --- a/test/windows/NetworkTests.cpp +++ b/test/windows/NetworkTests.cpp @@ -3974,6 +3974,11 @@ class GnsCallbackResultTests { VERIFY_SUCCEEDED(wsl::core::networking::GetGnsCallbackResult(LxGnsMessageConnectTestRequest, S_OK, -1)); } + + TEST_METHOD(GnsCallbackConnectTestTransportFailureFails) + { + VERIFY_ARE_EQUAL(E_ABORT, wsl::core::networking::GetGnsCallbackResult(LxGnsMessageConnectTestRequest, E_ABORT, -1)); + } }; class MirroredTests