Make absl::InitializeLog() safe to call more than once (#1656)#2046
Open
Othmane-Ch wants to merge 1 commit intoabseil:masterfrom
Open
Make absl::InitializeLog() safe to call more than once (#1656)#2046Othmane-Ch wants to merge 1 commit intoabseil:masterfrom
Othmane-Ch wants to merge 1 commit intoabseil:masterfrom
Conversation
Previously, calling absl::InitializeLog() more than once caused the program to abort: the underlying absl::log_internal::SetTimeZone() call fires ABSL_RAW_LOG(FATAL, "...has already been called") on its second invocation. The header documented this as "an error to call this function twice", but in real-world systems multiple independent libraries each want to bring up the logging library and have no way to coordinate among themselves -- Google's XLA project being the example cited in abseil#1656. This change wraps the body of InitializeLog() in absl::call_once so that exactly the first invocation runs the underlying initialization and all subsequent calls (including concurrent ones from other threads) become no-ops. The first caller's absl::LocalTimeZone() value wins, matching the existing "only the first call has effect" intent of the documented behaviour, just made safe instead of UB. initialize.h is updated to document the new contract. A new initialize_test.cc covers the repeat-call and concurrent-call cases; without the call_once guard the concurrent test races on the initialization globals or aborts via the FATAL raw log inside SetTimeZone(), which serves as a regression marker for the bug. The new code path takes the call_once flag once on every InitializeLog() call; the cost after the first call is a single relaxed atomic load. Closes abseil#1656.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
- Othmane envió una solicitud de pull para hacer que
absl::InitializeLog() se pueda llamar de forma segura más de una vez.
- Google-cla[bot] indicó que Othmane debe firmar un Contributor License
Agreement (CLA) antes de revisar la solicitud del Ministerio .
El El mié, may 6, 2026 a la(s) 15:11, google-cla[bot] <
***@***.***> escribió:
… *google-cla[bot]* left a comment (abseil/abseil-cpp#2046)
<#2046 (comment)>
Thanks for your pull request! It looks like this may be your first
contribution to a Google open source project. Before we can look at your
pull request, you'll need to sign a Contributor License Agreement (CLA).
View this failed invocation
<https://github.com/abseil/abseil-cpp/pull/2046/checks?check_run_id=74712699193>
of the CLA check for more information.
For the most up to date status, view the checks section at the bottom of
the pull request.
—
Reply to this email directly, view it on GitHub
<#2046 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/B4US5QCWD42CZ6SC7B5FGBT4ZO2AZAVCNFSM6AAAAACYTXKLFCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DGOJSGU3DIOJXGY>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1656.
Previously, calling
absl::InitializeLog()more than once aborted the program: the underlyingabsl::log_internal::SetTimeZone()firesABSL_RAW_LOG(FATAL, "...has already been called")on its second invocation. The header documented this as "an error to call this function twice", but in real-world systems multiple independent libraries each want to bring up the logging library and have no way to coordinate among themselves — Google's XLA project being the example cited in #1656.Change
Wrap the body of
InitializeLog()inabsl::call_once. Exactly the first invocation runs the underlying initialization; all subsequent calls (including concurrent ones from other threads) become no-ops. The first caller'sabsl::LocalTimeZone()value wins, matching the documented "only the first call has effect" intent — just made safe instead of UB.Net diff: +106 / −2 across 5 files.
absl/log/initialize.cc—absl::call_onceguard aroundInitializeLogImpl(absl::LocalTimeZone()).absl/log/initialize.h— docstring updated to document the new contract (safe to call repeatedly; concurrent callers block until the first call completes).absl/log/initialize_test.cc— new gtest covering: first call initializes, second call is a no-op, concurrent calls from N threads are safe.absl/log/BUILD.bazel,absl/log/CMakeLists.txt— wire the new test target and add the//absl/base(CMake:absl::base) dep to thelog_initializelibrary forcall_once.Testing
Built and ran the new test locally with CMake/Ninja + MSVC 19.36 on Windows:
Sibling regression check:
absl_log_globals_testalso passes after the BUILD/CMake edits.Without the
call_onceguard,ConcurrentCallsAreSafeeither races on the initialization globals or aborts via the FATAL raw log insideSetTimeZone(), so it doubles as a regression marker.Notes
InitializeLog()call is oneabsl::call_onceflag check, which after the first call is a single relaxed atomic load on the fast path.ShutdownLog()/ re-init capability (the issue does not request it)