ucx icon indicating copy to clipboard operation
ucx copied to clipboard

UCS/VFS/LOG: Fix fork() child pid handling when assigning multiple atfork handlers

Open ofirfarjun7 opened this issue 3 years ago • 1 comments

What

Fix UCS logger wrongly handle fork() and prints wrong pid.

Why ?

To reduce calls to getpid() process pid is cached on a static var. After calling to fork() process pid is changed and global vars are duplicated so the cached pid values of the child and parent are equal. UCS Logger handle that by attaching atfork handler to the child process. this handler change the log_pid var value to the child pid, but if the application attach more atfork handlers before UCS they will be called before UCS atfork handler and can cause the logger to run with the wrong pid...

From the Man: "The handlers for each phase are called in a specified order: the prepare handlers are called in reverse order of registration; the parent and child handlers are called in the order of registration"

How ?

By creating "init stage", as long as the init stage didn't complete UCS will call getpid() instead of using the cached value. Each time we call fork both child and parent will enter the init stage.

ofirfarjun7 avatar Sep 11 '22 16:09 ofirfarjun7

TODO - add new application/gtest that attach multiple atfork handlers, use UCS and call fork to test that the logger prints the correct pid.

ofirfarjun7 avatar Sep 11 '22 16:09 ofirfarjun7

/azp run

yosefe avatar Sep 25 '22 16:09 yosefe

Azure Pipelines successfully started running 3 pipeline(s).

azure-pipelines[bot] avatar Sep 25 '22 16:09 azure-pipelines[bot]