Skip to content

Calling tracemalloc.start() causes InterpreterPoolExecutor.submit() to hang on second call #146089

@djakubiec

Description

@djakubiec

Bug report

Bug description:

After enabling tracemalloc.start(), the first call to InterpreterPoolExecutor.submit() works, but the second one hangs.

It hangs on this line at threading.py:939:

self._os_thread_handle = _ThreadHandle()

This code reproduces the problem:

import concurrent.futures
import tracemalloc

# Set this to True to reproduce the hang below
enableTracemalloc = False
print(f"{enableTracemalloc=}")
if enableTracemalloc:
    tracemalloc.start()

with concurrent.futures.InterpreterPoolExecutor() as executor:
    futures = []
    for id in range(1, 3):
        print(f"Submitting {id=} {enableTracemalloc=}")

        # The second call to this hangs if tracemalloc.start() has been called
        futures.append(executor.submit(lambda x: x, x=id))
        print(f"Submitted {id=} {enableTracemalloc=}")

    results = [
        future.result()
        for future in concurrent.futures.as_completed(futures)
    ]
    print(f"{results=}")

When tracemalloc is disabled it works:

enableTracemalloc=False
Submitting id=1 enableTracemalloc=False
Submitted id=1 enableTracemalloc=False
Submitting id=2 enableTracemalloc=False
Submitted id=2 enableTracemalloc=False
results=[1, 2]

When tracemalloc is enabled it hangs:

enableTracemalloc=True
Submitting id=1 enableTracemalloc=True
Submitted id=1 enableTracemalloc=True
Submitting id=2 enableTracemalloc=True
<hangs forever>

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions