Skip to content

fix thread unsafe call to localtime on linux#835

Open
aleksisch wants to merge 1 commit into
ithewei:masterfrom
aleksisch:make-linux-thread-safe
Open

fix thread unsafe call to localtime on linux#835
aleksisch wants to merge 1 commit into
ithewei:masterfrom
aleksisch:make-linux-thread-safe

Conversation

@aleksisch
Copy link
Copy Markdown

@aleksisch aleksisch commented May 18, 2026

On Windows GetLocalTime is thread-safe. On linux localtime_r is thread-safe, while localtime not. This commit replaces usage of localtime in places not under mutex (only one such place) to localtime_r.

This bug was detected in daScript CI:
https://github.com/GaijinEntertainment/daScript/actions/runs/25911600620/job/76157878377?pr=2668

On Windows GetLocalTime is thread-safe. On linux localtime_r
is thread-safe, while localtime not. This commit replaces usage of
localtime in places not under mutex (only one such place)
to localtime_r.
@aleksisch aleksisch force-pushed the make-linux-thread-safe branch from 938c564 to 343437b Compare May 18, 2026 08:17
aleksisch added a commit to GaijinEntertainment/daScript that referenced this pull request May 18, 2026
MCP requires HV. Let's enable HV by default so MCP server will
be available in default builds.

It requires fixing thread-safe bug in libHV:
ithewei/libhv#835

And fixing our CMakeLists for ASAN on linux.
aleksisch added a commit to GaijinEntertainment/daScript that referenced this pull request May 18, 2026
MCP requires HV. Let's enable HV by default so MCP server will
be available in default builds.

It requires fixing thread-safe bug in libHV:
ithewei/libhv#835

And fixing our CMakeLists for ASAN on linux.
@ithewei ithewei requested a review from Copilot May 20, 2026 11:17
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a thread-safety issue in the logging timestamp formatting on non-Windows platforms by avoiding localtime() (which uses shared static storage) in logger_print, replacing it with the thread-safe localtime_r().

Changes:

  • Replace localtime() with localtime_r() in logger_print on non-Windows builds to avoid data races.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread base/hlog.c
Comment on lines 379 to 383
gettimeofday(&tv, NULL);
time_t tt = tv.tv_sec;
tm = localtime(&tt);
struct tm tm_buf;
tm = localtime_r(&tt, &tm_buf);
year = tm->tm_year + 1900;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants