From d353545efdc67973cd18fbbddbbf81aac7d52ca7 Mon Sep 17 00:00:00 2001 From: John Keith Hohm Date: Tue, 20 May 2025 12:25:22 -0400 Subject: [PATCH 1/3] Avoid socket failure with missing SYSTEMROOT env var on Windows Resolves #118234 by checking for the SystemRoot environment variable and, if it is missing, setting it based on GetWindowsDirectoryW. As described in the issue, the default path of the provider DLL is stored in the registry as "%SystemRoot%\System32\mswsock.dll" which does not resolve without SystemRoot set (typically to "C:\Windows"). --- Modules/socketmodule.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 92c9aa8b510dca..f2499bbbe79572 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -403,6 +403,21 @@ remove_unusable_flags(PyObject *m) return 0; } +/* issue #118234, avoid WinError 10106 with empty environment */ +static void ensure_system_root() { + LPCWSTR name = L"SystemRoot"; + DWORD size = GetEnvironmentVariableW(name, NULL, 0); + if (size) { + return; + } + wchar_t root[4096]; + UINT len = GetWindowsDirectoryW(root, 4096); + if (len == 0 || len >= 4096) { + return; + } + SetEnvironmentVariableW(name, root); +} + #endif #include @@ -5618,6 +5633,10 @@ sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto, SOCKET_T fd = INVALID_SOCKET; socket_state *state = find_module_state_by_def(Py_TYPE(self)); +#ifdef MS_WINDOWS + ensure_system_root(); +#endif + #ifndef MS_WINDOWS #ifdef SOCK_CLOEXEC int *atomic_flag_works = &sock_cloexec_works; From bf892338b5ba77e032ed18f1c3a4c80b96d3c24a Mon Sep 17 00:00:00 2001 From: John Keith Hohm Date: Tue, 20 May 2025 12:35:47 -0400 Subject: [PATCH 2/3] blurb --- .../next/Windows/2025-05-20-12-35-33.gh-issue-118234.HjbkIh.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Windows/2025-05-20-12-35-33.gh-issue-118234.HjbkIh.rst diff --git a/Misc/NEWS.d/next/Windows/2025-05-20-12-35-33.gh-issue-118234.HjbkIh.rst b/Misc/NEWS.d/next/Windows/2025-05-20-12-35-33.gh-issue-118234.HjbkIh.rst new file mode 100644 index 00000000000000..a1b1ef84db8d93 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2025-05-20-12-35-33.gh-issue-118234.HjbkIh.rst @@ -0,0 +1,2 @@ +Avoid socket failure with missing SYSTEMROOT env var on Windows. Patch by +John Keith Hohm. From 674ec21b9956b09b88118228218c8a2bbda3e3d4 Mon Sep 17 00:00:00 2001 From: John Keith Hohm Date: Tue, 20 May 2025 12:50:14 -0400 Subject: [PATCH 3/3] Fix indentation to conform to PEP 7. --- Modules/socketmodule.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index f2499bbbe79572..c4877dd790aa72 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -405,17 +405,17 @@ remove_unusable_flags(PyObject *m) /* issue #118234, avoid WinError 10106 with empty environment */ static void ensure_system_root() { - LPCWSTR name = L"SystemRoot"; - DWORD size = GetEnvironmentVariableW(name, NULL, 0); - if (size) { - return; - } - wchar_t root[4096]; - UINT len = GetWindowsDirectoryW(root, 4096); - if (len == 0 || len >= 4096) { - return; - } - SetEnvironmentVariableW(name, root); + LPCWSTR name = L"SystemRoot"; + DWORD size = GetEnvironmentVariableW(name, NULL, 0); + if (size) { + return; + } + wchar_t root[4096]; + UINT len = GetWindowsDirectoryW(root, 4096); + if (len == 0 || len >= 4096) { + return; + } + SetEnvironmentVariableW(name, root); } #endif