Skip to content

Commit 78737f1

Browse files
sraikund16facebook-github-bot
authored andcommitted
Do Not Cache PID/TID in Logging (#994)
Summary: Pull Request resolved: #994 S451588 was caused by the LOG macro caching values that would then be copied to other processes via forking. In general, we should probably use fork handlers to clear out said variables, but from a hygiene point of view we should also not be changing control flow based on logging. For this reason, the pid/tid retrieval in logging should get the cached variable if it exists, but never do the caching itself. Reviewed By: aaronenyeshi Differential Revision: D63668265 fbshipit-source-id: 6817c743248056464213be28f562d07752ec3283
1 parent 86f1deb commit 78737f1

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

libkineto/include/ThreadUtil.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
namespace libkineto {
1717

18-
int32_t systemThreadId();
18+
int32_t systemThreadId(bool cache=true);
1919
int32_t threadId();
2020
bool setThreadName(const std::string& name);
2121
std::string getThreadName();
2222

23-
int32_t processId();
23+
int32_t processId(bool cache=true);
2424
std::string processName(int32_t pid);
2525

2626
// Return a list of pids and process names for the current process

libkineto/src/Logger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Logger::Logger(int severity, int line, const char* filePath, int errnum)
3939
std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
4040
const char* file = strrchr(filePath, '/');
4141
buf_ << fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(tt)) << " "
42-
<< processId() << ":" << systemThreadId() << " "
42+
<< processId(false) << ":" << systemThreadId(false) << " "
4343
<< (file ? file + 1 : filePath) << ":" << line << "] ";
4444
}
4545

libkineto/src/ThreadUtil.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,38 @@ thread_local int32_t _tid = 0;
3939
thread_local int32_t _sysTid = 0;
4040
}
4141

42-
int32_t processId() {
42+
int32_t processId(bool cache) {
43+
int32_t pid = 0;
4344
if (!_pid) {
4445
#ifndef _WIN32
45-
_pid = (int32_t)getpid();
46+
pid = (int32_t)getpid();
4647
#else
47-
_pid = (int32_t)GetCurrentProcessId();
48+
pid = (int32_t)GetCurrentProcessId();
4849
#endif
50+
if (cache) {
51+
_pid = pid;
52+
}
53+
return pid;
4954
}
5055
return _pid;
5156
}
5257

53-
int32_t systemThreadId() {
58+
int32_t systemThreadId(bool cache) {
59+
int32_t sysTid = 0;
5460
if (!_sysTid) {
5561
#ifdef __APPLE__
56-
_sysTid = (int32_t)syscall(SYS_thread_selfid);
62+
sysTid = (int32_t)syscall(SYS_thread_selfid);
5763
#elif defined _WIN32
58-
_sysTid = (int32_t)GetCurrentThreadId();
64+
sysTid = (int32_t)GetCurrentThreadId();
5965
#elif defined __FreeBSD__
60-
syscall(SYS_thr_self, &_sysTid);
66+
syscall(SYS_thr_self, &sysTid);
6167
#else
62-
_sysTid = (int32_t)syscall(SYS_gettid);
68+
sysTid = (int32_t)syscall(SYS_gettid);
6369
#endif
70+
if (cache) {
71+
_sysTid = sysTid;
72+
}
73+
return sysTid;
6474
}
6575
return _sysTid;
6676
}

0 commit comments

Comments
 (0)