Skip to content
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

Mutex unlock can potentially overflow the stack. Provide an async unlock to allow users to avoid the issue. #65

Open
FunMiles opened this issue Feb 9, 2023 · 1 comment

Comments

@FunMiles
Copy link

FunMiles commented Feb 9, 2023

Reading the code of unlock for the async_mutex, my understanding is that if there's a long list of awaiters currently for the mutex, the mutex's holder's call to unlock will call resume() the first awaiter, whose unlock call will call resume() of the next awaiter and so on and so on, potentially running out of stack space.
This potential situation might be avoided by use of an asynchronous unlock, by which co_await mutex.unlock() would suspend the lock holder, schedule it in some way for later resumption and make the first awaiter be resumed immediately by the symmetric return mechanism of await_suspend(...). Thus there would be chained calls to resume() consuming the stack.

@FunMiles FunMiles changed the title Mutex unlock can potentially overflow the stack. Provide an async unlock to solve the issue. Mutex unlock can potentially overflow the stack. Provide an async unlock to allow users to avoid the issue. Feb 9, 2023
@andreasbuhr
Copy link
Owner

I am unsure whether this problem actually exists.

The resume call you are referring to in

waitersHead->m_awaiter.resume();
is the last statement in that function. It seems to me that no destructors have to be called at the end of this function. I would guess the compiler can do tail-call optimization, thus not needing extra stack space for the call.

It would be very interesting to see whether one can create that stack overflow you are describing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants