GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — sockets and asyncio on Windows depend on SYSTEMROOT env var · Issue #118234 · python/cpython

sockets and asyncio on Windows depend on SYSTEMROOT env var #118234

New issue
Copy link
New issue
Copy link
Open
#134435
Open
sockets and asyncio on Windows depend on SYSTEMROOT env var#118234
#134435
Copy link
Labels
docsDocumentation in the Doc dirDocumentation in the Doc direasytopic-socketRelated to the socket, socketserver and selectors modulesRelated to the socket, socketserver and selectors modules

Description

@jariwezerhu
jariwezerhu
opened on Apr 24, 2024
Issue body actions

Bug report

Bug description:

os.environ.clear() # THIS WAS THE CAUSE!!!
import asyncio

This causes the following:

Traceback (most recent call last):
  File "c:\Users\user\Documents\Development\main\main.py", line 3, in <module>
    import asyncio
  File "C:\Program Files\Python311\Lib\asyncio\__init__.py", line 42, in <module>
    from .windows_events import *
  File "C:\Program Files\Python311\Lib\asyncio\windows_events.py", line 8, in <module>
    import _overlapped
OSError: [WinError 10106] The requested service provider could not be loaded or initialized

It took me a whole day to figure out that os.environ.clear() causes this bug to occur. I'm not sure if this is supposed to occur, I just thought I would post this in case other people are struggling with this.

CPython versions tested on:

3.8, 3.9, 3.10, 3.11

Operating systems tested on:

Windows

Linked PRs

  • gh-118234: Avoid socket failure with missing SYSTEMROOT env var on Windows #134363
  • gh-118234: Document Python subprocess requirement on SystemRoot env, add RuntimeWarning #134435

Activity

  1. jariwezerhu
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 24, 2024
  2. AlexWaygood
    added
    topic-asyncio
    on Apr 24, 2024
  3. github-project-automation
    added this to asyncioon Apr 24, 2024
  4. github-project-automation
    moved this to Todo in asyncioon Apr 24, 2024
  5. gvanrossum
    changed the title [-]Asyncio doesn't like os.environ.clear()[/-] [+]Asyncio on Windows doesn't like os.environ.clear()[/+] on Apr 24, 2024
  6. gvanrossum commented on Apr 24, 2024

    @gvanrossum
    gvanrossum
    on Apr 24, 2024
    Member
    More actions

    Presumably there are specific environment variables that are used by the _overlapped module. You can determine which using bisection. :-) Maybe @zooba knows which env vars are needed here? I couldn't find any mentions of "env" in overlapped.c.

  7. zware commented on Apr 24, 2024

    @zware
    zware
    on Apr 24, 2024
    Member
    More actions

    A bit closer to the source, _overlapped tries to create a socket on import like the following:

    >>> import os, socket
    >>> os.environ.clear()
    >>> socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Program Files\Python312\Lib\socket.py", line 233, in __init__
        _socket.socket.__init__(self, family, type, proto, fileno)
    OSError: [WinError 10106] The requested service provider could not be loaded or initialized

    So I think asyncio is just a victim here.

  8. gvanrossum commented on Apr 24, 2024

    @gvanrossum
    gvanrossum
    on Apr 24, 2024
    Member
    More actions

    This is not something that's going to be fixed. We may just have to add a note to the docs.

    FWIW through trial and error it appears that SYSTEMROOT is the env var that should not be deleted.

    Reacted by Zachary Ware, Kirill Podoprigora and Jeremy Cheng
  9. Eclips4
    added
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    and removed
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 24, 2024
  10. gvanrossum
    changed the title [-]Asyncio on Windows doesn't like os.environ.clear()[/-] [+]sockets and asyncio on Windows depend on SYSTEMROOT env var[/+] on Apr 24, 2024
  11. gvanrossum commented on Apr 24, 2024

    @gvanrossum
    gvanrossum
    on Apr 24, 2024
    Member
    More actions

    If someone feels particularly helpful they could submit a small doc patch.

  12. gvanrossum
    added
    easy
    on Apr 24, 2024
  13. eryksun commented on Apr 24, 2024

    @eryksun
    eryksun
    on Apr 24, 2024
    Contributor
    More actions

    FWIW through trial and error it appears that SYSTEMROOT is the env var that should not be deleted.

    This is because the path of the provider DLL is stored in the registry as "%SystemRoot%\System32\mswsock.dll", and WinAPI ExpandEnvironmentStringsW() isn't smart enough to call GetSystemDirectoryW() if for some reason the SystemRoot environment variable isn't defined. Since "%SystemRoot%\System32\mswsock.dll" generally doesn't exist, the subsequent LoadLibraryExW() call fails.

    However, note that "%" is a valid filename character, and by default the latter LoadLibraryExW() call isn't restricted to exclude searching the working directory and PATH (i.e. Python doesn't call SetDefaultDllDirectories() to restrict the default search since that could cause problems for legacy scripts/applications). I made a dummy DLL that just prints "Fake mswsock loaded!".

    >>> del os.environ['SystemRoot']
    >>> os.path.exists('%SystemRoot%/System32/mswsock.dll')
    True
    >>> socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
    Fake mswsock loaded!
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Program Files\Python311\Lib\socket.py", line 232, in __init__
        _socket.socket.__init__(self, family, type, proto, fileno)
    OSError: [WinError 10106] The requested service provider could not be loaded or initialized

    A more elaborate example could forward the provider API calls to the real "mswsock.dll".

  14. gvanrossum commented on Apr 24, 2024

    @gvanrossum
    gvanrossum
    on Apr 24, 2024
    Member
    More actions

    @eryksun Thanks for the explanation. The "exploit" is off-topic though, right?

  15. eryksun commented on Apr 24, 2024

    @eryksun
    eryksun
    on Apr 24, 2024
    Contributor
    More actions

    @eryksun Thanks for the explanation. The "exploit" is off-topic though, right?

    My comment was extra information for Windows developers. It's a security issue in Windows, not Python. We could plug the hole by calling SetDefaultDllDirectories() at startup, but restricting the default DLL search path would be an issue for legacy scripts that depend on unsafe behavior. Our own DLL loads for extension modules and ctypes are safe.

  16. zooba commented on Apr 24, 2024

    @zooba
    zooba
    on Apr 24, 2024
    Member
    More actions

    Maybe @zooba knows which env vars are needed here?

    Just the general "don't clear everything". The list here that we've worked on in the past is as good as any.

  17. kumaraditya303
    removed
    topic-asyncio
    on Aug 11, 2024
  18. kumaraditya303
    removed this from asyncioon Aug 11, 2024
  19. jhohm
    added 3 commits that reference this issue on May 20, 2025

    Avoid socket failure with missing SYSTEMROOT env var on Windows

    This issue will close once commit d353545 is merged into the 'main' branch.
    d353545

    Merge branch 'python:main' into pythongh-118234

    This issue will close once commit 76c2645 is merged into the 'main' branch.
    76c2645

    Merge branch 'main' into pythongh-118234-2

    This issue will close once commit 80afcbc is merged into the 'main' branch.
    80afcbc
  20. sklam
    added 2 commits that reference this issue on Aug 14, 2025

    Suppress OSError when importing setuptools

    621b7a5

    Suppress OSError when importing setuptools

    22d1da1
  21. serhiy-storchaka
    added
    topic-socketRelated to the socket, socketserver and selectors modulesRelated to the socket, socketserver and selectors modules
    on Jul 3, 2026
  22. github-project-automation
    added this to docs issueson Jul 3, 2026
  23. github-project-automation
    moved this to Todo in docs issueson Jul 3, 2026
  24. lyuksovannyy
    added 2 commits that reference this issue on Oct 24, 2026

    test(cli,server): keep SystemRoot when a test clears a server's envir…

    04bbf8c

    test(cli,server): keep SystemRoot when a test clears a server's envir…

    54aa911
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirDocumentation in the Doc direasytopic-socketRelated to the socket, socketserver and selectors modulesRelated to the socket, socketserver and selectors modules

    Projects

    • docs issues
      Status
      Todo
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.