-
Notifications
You must be signed in to change notification settings - Fork 113
Fix lost-wakeup deadlock in asynchronous_sink flush (event primitive) (#255) #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| * Copyright 2026 - The MathWorks, Inc. | ||
| * Distributed under the Boost Software License, Version 1.0. | ||
| * (See accompanying file LICENSE_1_0.txt or copy at | ||
| * http://www.boost.org/LICENSE_1_0.txt) | ||
| */ | ||
| /*! | ||
| * \file sink_async_frontend_flush.cpp | ||
| * \author Conor Burgess | ||
| * \date 08.07.2026 | ||
| * | ||
| * \brief This file contains a test for asynchronous_sink flushing. | ||
| * | ||
| * One thread repeatedly flushes the logging core while another logs, for a few | ||
| * seconds. This stresses the handshake between asynchronous_sink::flush() (via | ||
| * unbounded_fifo_queue::interrupt_dequeue) and the sink feeding thread parked | ||
| * in the internal event. A lost-wakeup regression in that event made flush() | ||
| * hang forever on this configuration. A single run rarely trips it, so the | ||
| * test is meant to be looped; if the wakeup is lost the flush thread never | ||
| * joins and the test hangs, which is itself the failure signal. | ||
| */ | ||
|
|
||
| #define BOOST_TEST_MODULE sink_async_frontend_flush | ||
|
|
||
| #include <boost/log/detail/config.hpp> | ||
|
|
||
| #include <boost/test/unit_test.hpp> | ||
|
|
||
| #if !defined(BOOST_LOG_NO_THREADS) | ||
|
|
||
| #include <atomic> | ||
| #include <chrono> | ||
| #include <thread> | ||
| #include <sstream> | ||
|
|
||
| #include <boost/smart_ptr/make_shared_object.hpp> | ||
| #include <boost/smart_ptr/shared_ptr.hpp> | ||
|
|
||
| #include <boost/log/core/core.hpp> | ||
| #include <boost/log/sinks/async_frontend.hpp> | ||
| #include <boost/log/sinks/text_ostream_backend.hpp> | ||
| #include <boost/log/sources/severity_logger.hpp> | ||
| #include <boost/log/sources/record_ostream.hpp> | ||
|
|
||
| #include "test_barrier.hpp" | ||
|
|
||
| namespace logging = boost::log; | ||
| namespace sinks = boost::log::sinks; | ||
| namespace src = boost::log::sources; | ||
|
|
||
| // One thread flushes the core while another logs, for a few seconds, both | ||
| // started together via a barrier. If the flush/feed handshake loses a wakeup, | ||
| // flush() hangs and the flush thread never joins. | ||
| BOOST_AUTO_TEST_CASE(async_flush_no_lost_wakeup) | ||
| { | ||
| typedef sinks::asynchronous_sink< sinks::text_ostream_backend > sink_t; | ||
|
|
||
| boost::shared_ptr< std::ostream > strm(new std::ostringstream()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, an |
||
| boost::shared_ptr< sink_t > sink = boost::make_shared< sink_t >(); | ||
| sink->locked_backend()->add_stream(strm); | ||
| logging::core::get()->add_sink(sink); | ||
|
|
||
| const int run_seconds = 5; | ||
| std::atomic< bool > stop(false); | ||
| test_barrier barrier(2u); | ||
|
|
||
| std::thread flusher([&]() | ||
| { | ||
| barrier.arrive_and_wait(); | ||
| while (!stop.load(std::memory_order_relaxed)) | ||
| logging::core::get()->flush(); | ||
| }); | ||
|
|
||
| barrier.arrive_and_wait(); | ||
| src::severity_logger< int > lg; | ||
| const std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); | ||
| while (std::chrono::duration_cast< std::chrono::seconds >( | ||
| std::chrono::steady_clock::now() - start).count() < run_seconds) | ||
| { | ||
| BOOST_LOG_SEV(lg, 0) << "stress"; | ||
| std::this_thread::yield(); | ||
| } | ||
|
|
||
| stop.store(true, std::memory_order_relaxed); | ||
| flusher.join(); | ||
|
|
||
| logging::core::get()->remove_all_sinks(); | ||
|
|
||
| BOOST_CHECK(true); | ||
| } | ||
|
|
||
| #else // !defined(BOOST_LOG_NO_THREADS) | ||
|
|
||
| BOOST_AUTO_TEST_CASE(async_flush_no_lost_wakeup) | ||
| { | ||
| BOOST_CHECK(true); | ||
| } | ||
|
|
||
| #endif // !defined(BOOST_LOG_NO_THREADS) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
acq_relis not needed here.Also, the comment above is misleading. Event is a generic component, it has no relation to sinks, or the context in which it is used.
Same applies below, to
winapi_based_event::set_signalled.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, I agree -
releaseis sufficient because we only need to guarantee that writes which occurred before signalling are published and visible to the waiter after it is woken up - we don't need to read anything else after this, so no need foracquire. I also confirmed that, on x86 at least, bothacq_relandreleasecompile to exactly the same assembly - though I believe the same would not be true on a weak memory model architecture.