Skip to content

TLS should be updated when the syscall fork is invoked #604

@Chang-LeHung

Description

@Chang-LeHung

After fork, the thread_info should be updated, because the tid is inherited from its parent process. So the following happened:

Image

This happens if we use:

  • os.fork.
  • multiprocessing with the start method fork.

forkserver will not lead to this, because we only patch, but not trace.
spwan will not lead to this as well, because we use exec.

I've fixed it:

Image
I'll open a PR later.

Code to reproduce:
fork

import os


def hello():
    print("Hello World", os.getpid())


def child():
    print("Child process")
    hello()


def parent():
    print("Parent process")
    hello()


print("Hello World")
pid = os.fork()
if pid == 0:
    child()
else:
    parent()
    os.waitpid(pid, 0)

multiprocessing with 'fork'

import multiprocessing
import os


def hello():
    print("Hello World", os.getpid())


def child():
    print("Child process")
    hello()


def parent():
    print("Parent process")
    hello()


print("Hello World")


if __name__ == "__main__":
    import multiprocessing as mp

    mp.set_start_method("fork")
    p = multiprocessing.Process(target=child)
    p.start()
    p.join()
    parent()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions