Skip to content

import torch hangs when running in subprocess with preexec_fn=os.setpgrp, python >=3.13, conda env #159645

@fmamberti-wandb

Description

@fmamberti-wandb

🐛 Describe the bug

Trying to import torch in a subprocess after setting preexec_fn=os.setpgrp in a conda env with Python >= 3.13 causes the code execution to hang indefinitely.
If any of the above is changed, torch import as expected.

See detailed repro here.

Snippet to Repro:

import subprocess
import sys
import os

TIMEOUT_SEC = 20


def test_yes_setpgrp_no_import_torch():
    print("Testing YES preexec_fn=os.setpgrp, NO import torch...")
    try:
        proc = subprocess.Popen(
            [
                sys.executable,
                "-c",
                "print('=== SUCCESS: YES os.setpgrp, NO torch import')",
            ],
            preexec_fn=os.setpgrp,
        )

        stdout, stderr = proc.communicate(timeout=TIMEOUT_SEC)
        print("PASS")

    except Exception as e:
        print(f"ERROR: {e}")


def test_no_setpgrp_yes_torch_import():
    print("Testing NO preexec_fn=os.setpgrp, YES import torch...")
    try:
        proc = subprocess.Popen(
            [
                sys.executable,
                "-c",
                "import torch; print('=== SUCCESS: NO os.setpgrp, YES import torch')",
            ]
        )

        stdout, stderr = proc.communicate(timeout=TIMEOUT_SEC)
        print("PASS")

    except Exception as e:
        print(f"ERROR: {e}")


def test_yes_setpgrp_yes_torch_import():
    print("Testing YES preexec_fn=os.setpgrp, YES import torch...")
    try:
        proc = subprocess.Popen(
            [
                sys.executable,
                "-c",
                "import torch; print('=== SUCCESS: YES os.setpgrp, YES import torch')",
            ],
            preexec_fn=os.setpgrp,
        )

        # Wait with timeout
        try:
            stdout, stderr = proc.communicate(timeout=TIMEOUT_SEC)
            print("PASS")
        except subprocess.TimeoutExpired:
            print(f"FAIL: Process timed out after {TIMEOUT_SEC} seconds")
            proc.kill()

    except Exception as e:
        print(f"ERROR: {e}")


if __name__ == "__main__":
    test_yes_setpgrp_no_import_torch()
    test_no_setpgrp_yes_torch_import()
    test_yes_setpgrp_yes_torch_import()

Versions

torch==2.7.1
python==3.13.5
conda==25.3.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: python versionIssues related to specific Python versionsneeds reproductionEnsure you have actionable steps to reproduce the issue. Someone else needs to confirm the repro.triagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions