Discord.net: Simultaneous Request Handling

Created on 21 Jul 2018  路  7Comments  路  Source: discord-net/Discord.Net

Because DiscordSocketClient awaits handlers of the ReceivedGatewayEvent, new events are not raised from DiscordSocketClient until the task that is returned from the event handler completes.
In my scenario, I'm handling an incoming message, prompting for user input via Discord reactions and waiting in a task-based async fashion for the ReactionAdded event; of course, the ReactionAdded event is never raised because DiscordSocketClient is still awaiting MessageReceived. For now I'm just leaving my event handler code un-awaited, which is fine for me, since my Discord client implementation does not have a UI. However, I can see how this could cause issues for people who do.

Thanks, love this library 鉂わ笍

question

Most helpful comment

You can return a Task.CompletedTask, and no need to spin off a new Thread if you leave the Task.Run unawaited. Furthermore, mixing hard Thread while working with TAP is ill-advised.

All 7 comments

Our recommendation would be to leave the task unawaited for long-running handler as suggested by our documentation.

For example:

public Constructor(DiscordSocketClient client)
{
    client.MessageReceived += HandleMessageAsync;
}
private Task HandleMessageAsync(SocketMessage msg)
{
    _ = Task.Run(()=>{//...});
    return Task.CompletedTask;
}

But I still need to return a Task, since the return type of the event handler must be Task. My solution is:

        private async static Task Client_MessageReceived(SocketMessage arg)
        {
            //HACK: See Discord.Net/issues/1115
            await Task.Run(() =>
            {
                new Thread(async () =>
                {
                    try
                    {
                        var discordMessage = new DiscordMessageWrapper(arg);
                        await bbb.MessageReceived(discordMessage);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }).Start();
            });
        }

Albeit a bit goofy.

You can return a Task.CompletedTask, and no need to spin off a new Thread if you leave the Task.Run unawaited. Furthermore, mixing hard Thread while working with TAP is ill-advised.

Ah, that's a much nicer solution!

Looks like this has been resolved, feel free to reopen if you have further questions.

This is only a workaround. The library should handle requests in parallel, even if it's only a configurable value on the Discord client.

Might look into helping out with this one when I get a chance.

A solution was proposed in #1073, however other library collaborators disagreed with your sentiment and it was closed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RogueException picture RogueException  路  7Comments

MinisBett picture MinisBett  路  3Comments

aequasi picture aequasi  路  3Comments

AshkoreDracson picture AshkoreDracson  路  8Comments

JKamsker picture JKamsker  路  4Comments