Skip to content

Race condition in resume_foreground(DispatcherQueue) #370

@oldnewthing

Description

@oldnewthing
    [[nodiscard]] inline auto resume_foreground(
        Windows::System::DispatcherQueue const& dispatcher,
        Windows::System::DispatcherQueuePriority const priority = Windows::System::DispatcherQueuePriority::Normal) noexcept
    {
        struct awaitable
        {
...
            bool await_suspend(std::experimental::coroutine_handle<> handle)
            {
                m_queued = m_dispatcher.TryEnqueue(m_priority, [handle]
                    {
                        handle();
                    });

                return m_queued;
            }

There is a race condition here: If TryEnqueue succeeds, the coroutine may resume before TryEnqueue returns. In that case, the coroutine has been destructed, and our attempt to use m_queued will AV.

Unit tests randomly fail on my machine due to this bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions