Skip to content

GH-49433: [C++] Buffer ARROW_LOG output to prevent thread interleaving#49663

Merged
pitrou merged 3 commits intoapache:mainfrom
Shockp:fix-thread-safe-logging
Apr 22, 2026
Merged

GH-49433: [C++] Buffer ARROW_LOG output to prevent thread interleaving#49663
pitrou merged 3 commits intoapache:mainfrom
Shockp:fix-thread-safe-logging

Conversation

@Shockp
Copy link
Copy Markdown
Contributor

@Shockp Shockp commented Apr 5, 2026

Rationale for this change

When using the C++ ARROW_LOG macros from multiple threads, messages often get mingled together in stderr because the operator<< writes directly to the global stream piece-by-piece, which is not thread-safe.

What changes are included in this PR?

This PR introduces an internal std::ostringstream buffer to the fallback CerrLog class.
Instead of writing to std::cerr immediately on every << operation, the messages are accumulated locally within the CerrLog instance. The completed string is then flushed atomically to std::cerr upon the object's destruction.

Are these changes tested?

Yes. I built the C++ project locally and ran the test suite (ctest) to ensure no existing logging behavior or IPC functionality was broken.

Are there any user-facing changes?

No API changes. Users will simply notice that multi-threaded ARROW_LOG console output is now cleanly separated per line instead of interleaved.

Closes #49433

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 5, 2026

⚠️ GitHub issue #49433 has been automatically assigned in GitHub to PR creator.

Comment thread cpp/src/arrow/util/logging.cc Outdated
@github-actions github-actions Bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Apr 21, 2026
@Shockp Shockp force-pushed the fix-thread-safe-logging branch from 3566c7e to ea6bbf5 Compare April 21, 2026 09:43
@Shockp
Copy link
Copy Markdown
Contributor Author

Shockp commented Apr 21, 2026

Added the static std::mutex lock to the destuctor to protect the final flush, and verified it locally with a new 10-thread concurrent test in logging_test.cc.

Shockp and others added 2 commits April 22, 2026 12:03
…leaving

When using the ARROW_LOG macros from multiple threads, messages were getting
mingled together in stderr because the operator<< was writing directly to
the global stream piece-by-piece.

This commit introduces an internal std::ostringstream buffer to CerrLog so
that messages are accumulated locally and flushed atomically upon destruction.
@pitrou pitrou force-pushed the fix-thread-safe-logging branch from ea6bbf5 to fdb92d6 Compare April 22, 2026 10:07
Copy link
Copy Markdown
Member

@pitrou pitrou left a comment

Choose a reason for hiding this comment

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

Thanks a lot @Shockp ! I made two minor changes but this otherwise looks good to me. I'll wait for CI and then merge.

@pitrou
Copy link
Copy Markdown
Member

pitrou commented Apr 22, 2026

Ok, it looks like std::stringstream::view is not universally supported, so I'll revert that.

@pitrou pitrou merged commit 5633a18 into apache:main Apr 22, 2026
63 of 64 checks passed
@pitrou pitrou removed the awaiting committer review Awaiting committer review label Apr 22, 2026
@Shockp Shockp deleted the fix-thread-safe-logging branch April 22, 2026 13:06
@Shockp
Copy link
Copy Markdown
Contributor Author

Shockp commented Apr 22, 2026

Thank you so much for the review and the final tweaks, @pitrou! I really appreciate the guidance on my first PR. I will keep contributing more.

@conbench-apache-arrow
Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 3 benchmarking runs that have been run so far on merge-commit 5633a18.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 5 possible false positives for unstable benchmarks that are known to sometimes produce them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++] Multi-threaded logging can mix up messages

2 participants