Skip to content

Python bootstrapper fails on Windows after recent windows update #3721

@carpenterjc

Description

@carpenterjc

🐞 bug report

Affected Rule

Its the the bootstrapper.

Is this a regression?

No, I believe a windows update as exposed a previously unfound bug which was there for a while .

Description

I see a KeyError stack trace sometimes on windows when we try an run an executable.
Example exception KeyError: 'ProductType'
I have also seen a different key be unavailable: KeyError: 'ServicePackMajorVersion' just to show this seems to be random MSI keys being unavailable.

🔬 Minimal Reproduction

This is a non-determenistic bug which only started happening after we applied windows patches last week, and even then it will work perfectly most of the time.

🔥 Exception or Error




 Traceback (most recent call last):
    File "", line 206, in addpackage
    File "", line 1, in 
    File "d:\.bzl\root\x5haxcyn\execroot\_main\bazel-out\x64_windows-opt-exec-st-43a81393d292\bin\tools\src\foo\bah.exe.runfiles\_main\tools\src\foo\_bah.venv\lib\python3.12\site-packages\_bazel_site_init.py", line 236, in 
      COVERAGE_SETUP = _setup_sys_path()
                       ^^^^^^^^^^^^^^^^^
    File "d:\.bzl\root\x5haxcyn\execroot\_main\bazel-out\x64_windows-opt-exec-st-43a81393d292\bin\tools\src\foo\bah.exe.runfiles\_main\tools\src\foo\_bah.venv\lib\python3.12\site-packages\_bazel_site_init.py", line 160, in _setup_sys_path
      _maybe_add_path(abs_path)
    File "d:\.bzl\root\x5haxcyn\execroot\_main\bazel-out\x64_windows-opt-exec-st-43a81393d292\bin\tools\src\foo\bah.exe.runfiles\_main\tools\src\foo\_bah.venv\lib\python3.12\site-packages\_bazel_site_init.py", line 139, in _maybe_add_path
      path = _get_windows_path_with_unc_prefix(path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "d:\.bzl\root\x5haxcyn\execroot\_main\bazel-out\x64_windows-opt-exec-st-43a81393d292\bin\tools\src\foo\bah.exe.runfiles\_main\tools\src\foo\_bah.venv\lib\python3.12\site-packages\_bazel_site_init.py", line 100, in _get_windows_path_with_unc_prefix
      if platform.win32_ver()[1] >= "10.0.14393":
         ^^^^^^^^^^^^^^^^^^^^
    File "d:\.bzl\root\x5haxcyn\execroot\_main\bazel-out\x64_windows-opt-exec-st-43a81393d292\bin\tools\src\foo\bah.exe.runfiles\rules_python++python+python_3_12_x86_64-pc-windows-msvc\Lib\platform.py", line 449, in win32_ver
      version, csd, ptype, is_client = _win32_ver(version, csd, ptype)
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "d:\.bzl\root\x5haxcyn\execroot\_main\bazel-out\x64_windows-opt-exec-st-43a81393d292\bin\tools\src\foo\bah.exe.runfiles\rules_python++python+python_3_12_x86_64-pc-windows-msvc\Lib\platform.py", line 387, in _win32_ver
      (version, product_type, ptype, spmajor, spminor)  = _wmi_query(
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "d:\.bzl\root\x5haxcyn\execroot\_main\bazel-out\x64_windows-opt-exec-st-43a81393d292\bin\tools\src\foo\bah.exe.runfiles\rules_python++python+python_3_12_x86_64-pc-windows-msvc\Lib\platform.py", line 333, in 
      return (dict_data[k] for k in keys)
              ~~~~~~~~~^^^
  KeyError: 'ProductType'

The python code in question looks like this; from Lib/platform.py:


try:
    import _wmi
except ImportError:
    def _wmi_query(*keys):
        raise OSError("not supported")
else:
    def _wmi_query(table, *keys):
        table = {
            "OS": "Win32_OperatingSystem",
            "CPU": "Win32_Processor",
        }[table]
        data = _wmi.exec_query("SELECT {} FROM {}".format(
            ",".join(keys),
            table,
        )).split("\0")
        split_data = (i.partition("=") for i in data)
        dict_data = {i[0]: i[2] for i in split_data}
        return (dict_data[k] for k in keys)
 

That's basically saying that one of the passed-in keys is not in the WMI response.

The place which failed for me was this caller:

def _win32_ver(version, csd, ptype):
# Try using WMI first, as this is the canonical source of data
try:
(version, product_type, ptype, spmajor, spminor) = _wmi_query(
'OS',
'Version',
'ProductType',
'BuildType',
'ServicePackMajorVersion',
'ServicePackMinorVersion',
)

Interestingly there's an except OSError, but this isn't an OSError, it's a KeyError. So we just fail, instead of the fallback code:


    except OSError:
        pass
    # Fall back to a combination of sys.getwindowsversion and "ver"
    try:
        from sys import getwindowsversion
    except ImportError:
        return version, csd, ptype, True
    winver = getwindowsversion()
    is_client = (getattr(winver, 'product_type', 1) == 1)
    try:
        version = _syscmd_ver()[2]
        major, minor, build = map(int, version.split('.'))
    except ValueError:
        major, minor, build = winver.platform_version or winver[:3]
        version = '{0}.{1}.{2}'.format(major, minor, build)
 

🌍 Your Environment

Operating System:

  
Windows
  

Output of bazel version:

  
8.6.0
  

Rules_python version:

  
1.8.5
  

Anything else relevant?

I asked AI about what updates have happened last week: KB5083769 (Windows 11 cumulative OS update): High likelihood of possible WMI impact

  • KB5083769 (Windows 11 cumulative OS update): High likelihood of possible WMI impact
    Reason: It is a full OS cumulative update (OS builds 26200.8246 / 26100.8246). Even without explicit WMI notes, cumulative OS updates can change WBEM/WMI-adjacent components and behavior.
  • KB5088467 (Servicing Stack Update): Medium indirect likelihood
    Reason: SSU primarily affects update installation stack, not WMI runtime directly, but it changes servicing infrastructure and can influence system component state.
  • KB5082417 (.NET Framework 3.5/4.8.1 cumulative update): Medium likelihood if your tooling uses managed WMI APIs
    Reason: This update affects .NET runtime/libraries. If your apps/scripts use System.Management or managed WMI consumers, behaviour can change indirectly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions