Within win_iocp_io_context::shutdown() deadlock on GetQueuedCompletionStatus
- Dominant language
- C++
- Stars
- 1.6k
- Forks
- 485
- PR merge metrics
- No merged PRs in 30d
Description
The following code causes GetQueuedCompletionStatus(...) within win_iocp_io_context::shutdown() to deadlock.
```c++
#include
#include
using namespace std;
using namespace boost::asio;
class AsioTimer
{
public:
explicit AsioTimer(int value) : m_value(value), m_timer(m_context, 1ms), m_thread(&AsioTimer::run, this)
{
m_timer.async_wait(bind(&AsioTimer::timeout, this));
}
~AsioTimer()
{
auto running = true;
if (m_running.compare_exchange_strong(running, false))
{
if (m_timer.cancel() < 1)
{
cerr << "AsioTimer(" << m_value << ") timer cancel return 0" << endl;
}
m_context.stop();
if (m_thread.joinable())
{
m_thread.join();
}
}
}
private:
void run()
{
while (m_running)
{
try
{
m_context.run();
}
catch (const exception &ex)
{
cerr << "run error: " << ex.what() << endl;
}
}
}
void timeout() { m_timer.async_wait(bind(&AsioTimer::timeout, this)); }
int m_value = 0;
io_context m_context;
steady_timer m_timer;
thread m_thread;
atomic_bool m_running = true;
};
TEST(AsioTimerCancel, test)
{
auto count = 10000;
for (auto i = 0; i < count; ++i)
{
AsioTimer timer(i);
this_thread::sleep_for(1ms);
}
}
```
[Associated](https://github.com/chriskohlhoff/asio/issues/1393)
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the supplied C++ reproduction and trace win_iocp_io_context::shutdown(), focusing on the GetQueuedCompletionStatus call and the sequence of timer cancellation, io_context.stop(), and thread joining. Confirm the deadlock by running the 10,000-iteration AsioTimerCancel test, then determine what shutdown behavior allows the test to complete without hanging.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100