Skip to content

dpp::when_any result not returning the index_finished correctly #1413

@rruann

Description

@rruann

On the following example when i call dpp::when_any without adding a sleep raceResult.index() returns 18446744073709551615.
with the line co_await event.from()->creator->co_sleep(1); raceResult.index() returns 0 which is the correct desired value, as it represent the index of the task finished.

The data returned by raceResult.index() is index_finished:

/**
* @brief Index of the awaitable that finished. Initialized to the maximum value of std::size_t.
*/
size_t index_finished = std::numeric_limits<std::size_t>::max();

As its returning 18446744073709551615 which is the initialized value, its not getting the finished task index correctly.

dpp::task<std::string> App::test(const  dpp::slashcommand_t& event)
{
    //co_await event.from()->creator->co_sleep(1);
    co_return "test";
}

dpp::task<void> App::replyThinking(const dpp::slashcommand_t& event, std::function<task<std::string>(const dpp::slashcommand_t&)> func, bool ephemeral)
{
    try
    {
        event.thinking(ephemeral);

        auto raceResult = co_await dpp::when_any(
            func(event),                        // Task 0: actual work
            event.from()->creator->co_sleep(10) // Task 1: timeout timer
        );

        // 0 (work) finished before the timeout
        raceResult.index() == 0 ? event.edit_response(message(raceResult.get<0>()))
                                : event.edit_response(message("Sorry, the operation timed out."));
    }
    catch (const task_cancelled_exception& e)
    {
        event.edit_response(message("Operation cancelled: " + std::string(e.what())));
    }
    catch (const std::exception& e)
    {
        event.edit_response(message("An error occurred: " + std::string(e.what())));
    }
    catch (...)
    {
        event.edit_response(message("An unknown error occurred."));
    }
}

 dpp::task<void> App::onSlashCommand(const slashcommand_t& event)
{
    co_await replyThinking(event, std::bind_front(&App::test, this), true);
    co_return;
}

I'm debugging with Visual Studio 2022 version 17.14.0 Preview 2.0, Win10, the issue reproduces when testing with discord desktop/web
The lib was compiled using vcpkg, dpp:x64-windows 10.1.2

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions