Skip to content

The native_thread_id field of PyThreadState is not updated after fork #100649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
P403n1x87 opened this issue Dec 31, 2022 · 0 comments
Closed

The native_thread_id field of PyThreadState is not updated after fork #100649

P403n1x87 opened this issue Dec 31, 2022 · 0 comments
Labels
3.11 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@P403n1x87
Copy link
Contributor

P403n1x87 commented Dec 31, 2022

Bug report

The native_thread_id field of the PyThreadState object is not updated after a fork on Linux (at least). This means that child processes spawned by the main thread of the parent process will have a main thread with the parent thread ID.

The native_thread_id is meant to be consumed by tools like Austin and therefore the behaviour is easily observed with these tools. One way to reproduce this is to profile this with Austin

import multiprocessing


def fact(n):
    f = 1
    for i in range(1, n + 1):
        f *= i
    return f


def do(N):
    n = 1
    for _ in range(N):
        fact(n)
        n += 1


if __name__ == "__main__":
    import sys

    try:
        nproc = int(sys.argv[1])
    except Exception:
        nproc = 2

    processes = []
    for _ in range(nproc):
        process = multiprocessing.Process(target=do, args=(3000,))
        process.start()
        processes.append(process)

    for process in processes:
        process.join(timeout=5)

and observe that the reported thread IDs coincide with the parent's PID.

Your environment

  • CPython versions tested on: 3.11.1
  • Operating system and architecture: Ubuntu 22.04 (amd64)

Linked PRs

@P403n1x87 P403n1x87 added the type-bug An unexpected behavior, bug, or error label Dec 31, 2022
@corona10 corona10 added the 3.11 only security fixes label Jan 1, 2023
corona10 pushed a commit that referenced this issue Jan 1, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 1, 2023
Update native_thread_id after fork
(cherry picked from commit d52d494)

Co-authored-by: Gabriele N. Tornetta <[email protected]>
@corona10 corona10 closed this as completed Jan 1, 2023
miss-islington added a commit that referenced this issue Jan 1, 2023
Update native_thread_id after fork
(cherry picked from commit d52d494)

Co-authored-by: Gabriele N. Tornetta <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants