Skip to content

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

Closed

Description

@P403n1x87

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

Activity

  1. added a commit that references this issue on Jan 1, 2023
  2. added a commit that references this issue on Jan 1, 2023
  3. added a commit that references this issue on Jan 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions