Skip to content

os.execve crashes when used via subprocess on Windows #143327

@chris-eibl

Description

@chris-eibl

Bug report

Bug description:

The small reproducer

import os, sys
import subprocess

code = """
import os, sys
args = [sys.executable, '-c', 'print(4711)']
os.execve(args[0], args, {})
"""

cmd_line = [sys.executable, '-X', 'faulthandler', '-c', code]
env_1 = os.environ.copy()
env_2 = {}
env_2['SYSTEMROOT'] = os.environ['SYSTEMROOT']
proc = subprocess.Popen(
    cmd_line, stdin=subprocess.PIPE,
    stdout=subprocess.PIPE, stderr=subprocess.PIPE,
    env=env_1)
with proc:
    try:
        out, err = proc.communicate()
    finally:
        proc.kill()
        subprocess._cleanup()
print("rc", proc.returncode)
print("out", out)
print("err", err)

crashes using env_1 for me:

['e:\\cpython_clang\\PCbuild\\amd64\\python_d.exe', '-X', 'faulthandler', '-c', "import os, sys;args = [sys.executable, '-c', 'print(4711)'];os.execve(args[0], args, {})"]
rc 3221225477
out b''
err b'Windows fatal exception: access violation\n\nCurrent thread 0x00000b34 (most recent call first):\n  File "<string>", line 1 in <module>\n\nCurrent thread\'s C stack trace (most recent call first):\n  <cannot get C stack on this system>\n'

Found when analyizing broken Windows CI of #143314.

For Python 3.11. 3.12, 3.13 and 3.14 this almost always crashes. Python 3.10 also crashes, but more often succeeds.

Interestingly the "smaller" env_2 works. It seems to be the combination of subprocess and os.execve. Without subprocess, I do not see this problem.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-windowsextension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errortype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions