Skip to content

Commit acfde3d

Browse files
committed
fix(logging): fsync crash logs before _Exit() to prevent data loss
When a SIGSEGV occurs, the signal handler logs "Segmentation fault encountered" and then calls _Exit() which terminates the process immediately. Without fsync(), kernel write buffers may not be flushed to disk before termination, causing a race condition where the error log file is sometimes not created. This fix adds fsync() on Unix/Linux and _commit() on Windows after write() in ddtrace_log_with_time() to ensure crash logs persist to disk before process termination. The issue affects production (rare but possible during power loss, kernel panic, or I/O errors) and causes consistent test failures where tests check for log files immediately after crashes (before kernel writeback completes). Fixes flaky test_metrics SigSegVTest::testGet failures on Kubernetes where dd_php_error.log was not being created consistently.
1 parent 7f7b873 commit acfde3d

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

ext/logging.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ int ddtrace_log_with_time(int fd, const char *msg, int msg_len) {
160160

161161
int ret = write(fd, msgbuf, p - msgbuf);
162162

163+
// Flush to disk to ensure data persists even if process terminates immediately
164+
if (ret > 0) {
165+
#ifndef _WIN32
166+
fsync(fd);
167+
#else
168+
_commit(fd);
169+
#endif
170+
}
171+
163172
free(msgbuf);
164173
return ret;
165174
}

0 commit comments

Comments
 (0)