#604·viztracer

TLS should be updated when the syscall fork is invoked

Author: Chang-LeHungCreated May 24, 2025Updated May 24, 2025

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

python
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'

python
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()

Source: gaogaotiantian/viztracer