andreasbuhr / cppcoro

A library of C++ coroutine abstractions for the coroutines TS
MIT License
364 stars 53 forks source link

On MSVC 2017, segfaults occur in test 37 #25

Open andreasbuhr opened 3 years ago

andreasbuhr commented 3 years ago

In: https://github.com/andreasbuhr/cppcoro/runs/1505811235?check_suite_focus=true

we see The following tests FAILED: 37 - async auto reset event tests-multi-threaded (SEGFAULT)

wich happens sometimes.

karzhenkov commented 3 years ago

It seems that the crash occurs due to a stack overflow in startWaiter lambda function. Coroutines awaiting on the async_auto_reset_event are sometimes resumed inside a call to set(), which is a documented behavior. There are multiple coroutines created by startWaiter, so the chain of nested continuations may become too long. https://github.com/andreasbuhr/cppcoro/blob/b9c28b1fd2df2cd76601327964944883cbc85d67/test/async_auto_reset_event_tests.cpp#L100-L106 With a slightly modified startWaiter it is possible to detect nested continuations.

(see code) ```c++ std::atomic depth = 0; auto startWaiter = [&]() -> cppcoro::task<> { co_await tp.schedule(); co_await event; ++value; int d = depth++; assert(d < 3); // if depth is greater than number of threads in the pool, // this is executed as a nested continuation event.set(); depth -= 1; }; ```

Interestingly, when there is only one thread in the thread pool, the test completes successfully.