botframework-sdk: Bot stops responding after some time.

Bot Info

  • SDK Platform: .Net Framework 4.6
  • SDK Version: 3.14
  • Active Channels: Web Chat, Cortana, Skype
  • Deployment Environment: Azure App Service

Issue Description

After some time, my bot fails to respond. I cannot say definitively if it’s an hour, but is around that time. The only way to correct issue is to restart service. I did recently upgrade to the latest version, but not sure if this is the cause. I also added BotAuth, but have taken it out and issue still occurs. The error that I see in my event log is this.

Event time ‎3‎/‎15‎/‎2018‎ ‎9‎:‎36‎:‎02‎ ‎AM
Message POST to FosterCareAssistant failed: POST to the bot’s endpoint failed with HTTP status 500 POST to the bot’s endpoint failed with HTTP status 500
Exception type System.Exception
Failed method Microsoft.Bot.ChannelConnector.BotAPI+<PostActivityToBotAsync>d__29.MoveNext

{ “message”: “An error has occurred.”, “exceptionMessage”: “Operation returned an invalid status code ‘Unauthorized’”, “exceptionType”: “Microsoft.Bot.Connector.ErrorResponseException”, “stackTrace”: " at Microsoft.Bot.Connector.Conversations.<ReplyToActivityWithHttpMessagesAsync>d__8.MoveNext()\r\n— End of stack trace from previous location where exception was thrown —\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Connector.ConversationsExtensions.<ReplyToActivityAsync>d__7.MoveNext()\r\n— End of stack trace from previous location where exception was thrown —\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at HHS.ChildWelfareBot.Web.MessagesController.<Post>d__0.MoveNext() in C:\code\Code\FosterCareDemo\ChildWelfareBot\Controllers\MessagesController.cs:line 48\r\n— End of stack trace from previous location where exception was thrown —\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()\r\n— End of stack trace from previous location where exception was thrown —\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n— End of stack trace from previous location where exception was thrown —\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n— End of stack trace from previous location where exception was thrown —\r\n at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n— End of stack trace from previous location where exception was thrown —\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()\r\n— End of stack trace from previous location where exception was thrown —\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n— End of stack trace from previous location where exception was thrown —\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()" }

Code Example

Reproduction Steps

  1. Wait for an hour or so

Expected Behavior

Bot responds.

Actual Results

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 11
  • Comments: 23 (6 by maintainers)

Most upvoted comments

I am facing the same issue with v3.15.2.2. After every one hour whenever I try to interact for the first time it throws the same exception “Operation returned an invalid status code ‘Unauthorized’”. However, when I send the second message it starts working automatically. It’s reproducible after every hour. It happens with both webchat and facebook.

You will need to downgrade both Microsoft.Bot.Builder and Microsoft.Bot.Connector to 3.13.1 that is working fine with me till now.

wow, I was trying for hours to find the error, understand how to debug this… and then it turns out to be the framework itself?!

I found a solution maybe work for whom got unauthorized exception when bot service is idle. You need to refresh token and pass it to DelegatingHandler when creating BotConnector.

var account=new MicrosoftAppCredentials("{MicrosoftAppIdKey}", "{MicrosoftAppPasswordKey}");
var jwtToken=await account.GetTokenAsync();
var connector = new ConnectorClient(new Uri("{serviceUrl}"), handlers :new MyDelegatingHandler(jwtToken));

public class MyDelegatingHandler : DelegatingHandler
{
    private string _token;
    public MyDelegatingHandler(string token)
    {
        _token = token;
    }

    protected override Task<HttpResponseMessage> SendAsync(
         HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _token);
        return base.SendAsync(request, cancellationToken);
    }
}

For more detail, please refer to this post https://stackoverflow.com/questions/42749982/initiate-a-message-from-bot-to-user-on-botframework

After downgrading both Microsoft.Bot.Builder and Microsoft.Bot.Connector to 3.13.1 it seems to work fine again. Thanks ahattab & EricDahlvang!

The team is actively working a fix for this. Until we get a release out, please downgrade to v3.13.1.

@JakobMadsen717 I downgraded one of my bot code instances to 3.13.1 and it’s quite stable. Here’s my approach;

  1. Downgraded first
  2. Started the project locally
  3. Waited for over one hour
  4. Tried to interact with bot through the emulator and it responded.

You can give it a try 😃

I don’t know if it’s an hour or perhaps even more. Perhaps related to some internal token that expires after an hour if /api/messages is not called?

In any case I am experiencing the same thing, every morning for example. The service is not “dead”, it just doesn’t send back responses to questions (mine is a Q&A bot). I can tell from a log that the questions are coming in, I just don’t get a reply in e.g. “Test in Web Chat” utility on Azure or DirectLine channel for that matter.

Does anyone know if it helps to downgrade some nuget packages, and if so which ones to downgrade?