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

The ShowTypingMiddleware throws System.ObjectDisposedException when an exception occurs in the bot #6752

Closed
rliberoff opened this issue Feb 21, 2024 · 5 comments
Assignees
Labels
bug Indicates an unexpected problem or an unintended behavior.

Comments

@rliberoff
Copy link

rliberoff commented Feb 21, 2024

Version

I'm using 4.22.2

Describe the bug

When using the ShowTypingMiddleware in a CloudAdapter, it thows an System.ObjectDisposedException with message Cannot access a disposed object. Object name: 'Get'. after an exception has been handled by the OnTurnError error handler.

This is the stack trace:

at Microsoft.Bot.Builder.TurnContextStateCollection.Get[T](String key)
at Microsoft.Bot.Builder.TurnContextStateCollection.Get[T]()
at Microsoft.Bot.Builder.CloudAdapterBase.<SendActivitiesAsync>d__9.MoveNext()
at Microsoft.Bot.Builder.ShowTypingMiddleware.<SendTypingActivityAsync>d__7.MoveNext()
at Microsoft.Bot.Builder.ShowTypingMiddleware.<SendTypingAsync>d__6.MoveNext()
at Microsoft.Bot.Builder.ShowTypingMiddleware.<FinishTypingTaskAsync>d__9.MoveNext()
at Microsoft.Bot.Builder.ShowTypingMiddleware.<ProcessTypingAsync>d__10.MoveNext()
at Microsoft.Bot.Builder.ShowTypingMiddleware.<OnTurnAsync>d__4.MoveNext()
at Microsoft.Bot.Builder.MiddlewareSet.<ReceiveActivityWithStatusAsync>d__3.MoveNext()
at Microsoft.Bot.Builder.BotAdapter.<RunPipelineAsync>d__23.MoveNext()" | string

To Reproduce

Just create the simplest bot possible and Use the ShowTypingMiddleware. Produce an exception to be capture and handled by the CloudAdapter with the OnTurnError handler. Then throw a second exception and you will get this error.

Expected behavior

This middleware should show a typing indicator in Microsoft Teams and also in a web chat without throwing exceptions.

Screenshots

N/A

Additional context

N/A

@rliberoff rliberoff added bug Indicates an unexpected problem or an unintended behavior. needs-triage The issue has just been created and it has not been reviewed by the team. labels Feb 21, 2024
@rliberoff rliberoff changed the title The ShowTypingMiddleware throws The ShowTypingMiddleware throws System.ObjectDisposedException when an exception occurs in the bot Feb 21, 2024
@rliberoff
Copy link
Author

Maybe the question is – if there is no bug – how to correctly handle exceptions in the CloudAdapter when using the ShowTypingMiddleware.

Please advice.

Thank you.

@rliberoff
Copy link
Author

rliberoff commented Feb 21, 2024

Menawhile we created a custom middleware. It is actually a copy of yours, but with a slightly different FinishTypingTaskAsync method, which is where we found the issue occurs.

    private async Task FinishTypingTaskAsync(ITurnContext turnContext)
    {
        if (string.IsNullOrEmpty(turnContext?.Activity?.Conversation?.Id) && !tasks.ContainsKey(turnContext.Activity.Conversation.Id))
        {
            return;
        }

        // Cancel the typing loop.
        tasks.TryGetValue(turnContext.Activity.Conversation.Id, out var item);
        var (typingTask, cts) = item;
        cts?.Cancel();
        cts?.Dispose();
        if (typingTask != null)
        {
            if (typingTask.Exception == null)
            {
                await typingTask.ConfigureAwait(false);
            }

            typingTask.Dispose();
        }

        tasks.TryRemove(turnContext.Activity.Conversation.Id, out _);
    }

Please notice the if (typingTask.Exception == null) which detects if there is an exception in the (previous) task.

Thank you.

@tracyboehrer tracyboehrer removed the needs-triage The issue has just been created and it has not been reviewed by the team. label Feb 26, 2024
@JhontSouth
Copy link
Collaborator

Hi @rliberoff,
We were able to reproduce the issue by forcing an error in the OnMessageActivityAsync method and another one in the OnTurnError.
We'll analyze how the correct behavior should be.

@rliberoff
Copy link
Author

Hi @JhontSouth!

Thank you. If there’s anything more I can assist you with, please don’t hesitate to reach out.

tracyboehrer pushed a commit that referenced this issue Apr 10, 2024
…eption when an exception occurs in the bot (#6772)

* avoid faulted task to dispose

* remove nested if

* remove blank spaces
@tracyboehrer
Copy link
Member

Merged in #6772. Available next release.

tracyboehrer pushed a commit that referenced this issue Apr 15, 2024
…eption when an exception occurs in the bot (#6772)

* avoid faulted task to dispose

* remove nested if

* remove blank spaces
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or an unintended behavior.
Projects
None yet
Development

No branches or pull requests

4 participants