Random timeouts / TaskCanceledException within BotFramework












2














Problem description



We have deployed our chat bot to the Azure cloud as an App Service and within Application Insights we see several exceptions of type TaskCanceledException popping up randomly.



After taking a look at the logs we have identified three lines of code, which are throwing the exceptions. I have tagged these as TaskCanceledException source #. Here is simplified structure of our messaging controller for the reference:



public async Task<IHttpActionResult> Post([FromBody]BotActivity activity)
{
try
{
var result = await PostInternal(activity).ConfigureAwait(false);

return result;
}
catch (Exception e)
{
Logger.Error(e);
throw;
}
}

private async Task<IHttpActionResult> PostInternal(BotActivity activity)
{
// TaskCanceledException source #1.
await SendTypingIndicator(activity).ConfigureAwait(false);

var type = activity.GetActivityType();
if (type == ActivityTypes.Message)
{
// TaskCanceledException source #2.
var flag = await LoadFlagFromBotState(activity).ConfigureAwait(false);

// Some logic slightly altering flow according to value of 'flag'.

// TaskCanceledException source #3.
await Conversation.SendAsync(activity, () => new RootDialog()).ConfigureAwait(false);
}

return Ok();
}

private async Task SendTypingIndicator(BotActivity activity)
{
var reply = activity.CreateReply();
reply.Type = ActivityTypes.Typing;
reply.Text = null;
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

await connector.Conversations.ReplyToActivityAsync(reply).ConfigureAwait(false);
}

private async Task<bool> LoadFlagFromBotState(BotActivity activity)
{
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
var botDataStore = scope.Resolve<IBotDataStore<BotData>>();
var key = Address.FromActivity(activity);
var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None).ConfigureAwait(false);

var flag = userData.GetProperty<bool>("TheFlag");
if (!flag)
{
userData.SetProperty("TheFlag", true);
await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None).ConfigureAwait(false);
await botDataStore.FlushAsync(key, CancellationToken.None).ConfigureAwait(false);
}

return flag;
}
}


And here are the URLs of external dependencies which timed-out according to Application Insights:




  • For TaskCanceledException source #1:


    • https://directline.botframework.com/v3/conversations/<CONV_ID>/activities/<ACT_ID>



  • For TaskCanceledException source #2:


    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:user<USER_ID>



  • For TaskCanceledException source #3:


    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:private<CONV_ID>:<USER_ID>

    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:conversation<CONV_ID>

    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:user<USER_ID>




Additional information




  • Our bot is hosted as an App Service within Microsoft Azure cloud.

  • We plugged in custom BotState manager according to following documentation so that all the BotState is stored within Cosmos DB.

  • Our Cosmos DB instance is within same Azure Resource Group as the bot App Service.

  • We verified the availability and throughput of our Cosmos DB instance and everything seems to be perfectly in order.

  • We are using DirectLine as the communication channel.

  • The TaskCanceledException happens randomly, there does not seem to be any specific steps which needs to be performed to reproduce this.

  • We tried to adapt the async-await pattern the 'proper way', for example by making sure that we are not mixing any sync and async code and by using ConfigureAwait(false) everywhere where the await keyword is.


Conclusion



We have been trying to figure out the root cause for some time, however we have no clue what could be possibly the problem here. We ruled out the networking issues due to the fact that we utilize Azure's PaaS here.



I will appreciate any advice.










share|improve this question






















  • Have you tried increasing the timeout?
    – Paulo Morgado
    Nov 23 at 7:20










  • No, we have left the default timeout intervals in place, which are about 1 minute. Most of the time the duration of these external calls are just couple milliseconds according to App Insights.
    – Markkknk
    Nov 23 at 9:10












  • That would be a way of seeing if it takes more than one minute or never replies.
    – Paulo Morgado
    Nov 23 at 14:49










  • The ConfigureAwait(false) is causing the HttpContext to be lost, and it is probably sometimes disposed when it is expected to be available.
    – Eric Dahlvang
    Nov 26 at 17:12










  • More information about how .ConfigureAwait(false) affects httpcontext can be found in this answer: stackoverflow.com/a/28433417/86646
    – Eric Dahlvang
    Nov 26 at 19:52
















2














Problem description



We have deployed our chat bot to the Azure cloud as an App Service and within Application Insights we see several exceptions of type TaskCanceledException popping up randomly.



After taking a look at the logs we have identified three lines of code, which are throwing the exceptions. I have tagged these as TaskCanceledException source #. Here is simplified structure of our messaging controller for the reference:



public async Task<IHttpActionResult> Post([FromBody]BotActivity activity)
{
try
{
var result = await PostInternal(activity).ConfigureAwait(false);

return result;
}
catch (Exception e)
{
Logger.Error(e);
throw;
}
}

private async Task<IHttpActionResult> PostInternal(BotActivity activity)
{
// TaskCanceledException source #1.
await SendTypingIndicator(activity).ConfigureAwait(false);

var type = activity.GetActivityType();
if (type == ActivityTypes.Message)
{
// TaskCanceledException source #2.
var flag = await LoadFlagFromBotState(activity).ConfigureAwait(false);

// Some logic slightly altering flow according to value of 'flag'.

// TaskCanceledException source #3.
await Conversation.SendAsync(activity, () => new RootDialog()).ConfigureAwait(false);
}

return Ok();
}

private async Task SendTypingIndicator(BotActivity activity)
{
var reply = activity.CreateReply();
reply.Type = ActivityTypes.Typing;
reply.Text = null;
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

await connector.Conversations.ReplyToActivityAsync(reply).ConfigureAwait(false);
}

private async Task<bool> LoadFlagFromBotState(BotActivity activity)
{
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
var botDataStore = scope.Resolve<IBotDataStore<BotData>>();
var key = Address.FromActivity(activity);
var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None).ConfigureAwait(false);

var flag = userData.GetProperty<bool>("TheFlag");
if (!flag)
{
userData.SetProperty("TheFlag", true);
await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None).ConfigureAwait(false);
await botDataStore.FlushAsync(key, CancellationToken.None).ConfigureAwait(false);
}

return flag;
}
}


And here are the URLs of external dependencies which timed-out according to Application Insights:




  • For TaskCanceledException source #1:


    • https://directline.botframework.com/v3/conversations/<CONV_ID>/activities/<ACT_ID>



  • For TaskCanceledException source #2:


    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:user<USER_ID>



  • For TaskCanceledException source #3:


    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:private<CONV_ID>:<USER_ID>

    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:conversation<CONV_ID>

    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:user<USER_ID>




Additional information




  • Our bot is hosted as an App Service within Microsoft Azure cloud.

  • We plugged in custom BotState manager according to following documentation so that all the BotState is stored within Cosmos DB.

  • Our Cosmos DB instance is within same Azure Resource Group as the bot App Service.

  • We verified the availability and throughput of our Cosmos DB instance and everything seems to be perfectly in order.

  • We are using DirectLine as the communication channel.

  • The TaskCanceledException happens randomly, there does not seem to be any specific steps which needs to be performed to reproduce this.

  • We tried to adapt the async-await pattern the 'proper way', for example by making sure that we are not mixing any sync and async code and by using ConfigureAwait(false) everywhere where the await keyword is.


Conclusion



We have been trying to figure out the root cause for some time, however we have no clue what could be possibly the problem here. We ruled out the networking issues due to the fact that we utilize Azure's PaaS here.



I will appreciate any advice.










share|improve this question






















  • Have you tried increasing the timeout?
    – Paulo Morgado
    Nov 23 at 7:20










  • No, we have left the default timeout intervals in place, which are about 1 minute. Most of the time the duration of these external calls are just couple milliseconds according to App Insights.
    – Markkknk
    Nov 23 at 9:10












  • That would be a way of seeing if it takes more than one minute or never replies.
    – Paulo Morgado
    Nov 23 at 14:49










  • The ConfigureAwait(false) is causing the HttpContext to be lost, and it is probably sometimes disposed when it is expected to be available.
    – Eric Dahlvang
    Nov 26 at 17:12










  • More information about how .ConfigureAwait(false) affects httpcontext can be found in this answer: stackoverflow.com/a/28433417/86646
    – Eric Dahlvang
    Nov 26 at 19:52














2












2








2







Problem description



We have deployed our chat bot to the Azure cloud as an App Service and within Application Insights we see several exceptions of type TaskCanceledException popping up randomly.



After taking a look at the logs we have identified three lines of code, which are throwing the exceptions. I have tagged these as TaskCanceledException source #. Here is simplified structure of our messaging controller for the reference:



public async Task<IHttpActionResult> Post([FromBody]BotActivity activity)
{
try
{
var result = await PostInternal(activity).ConfigureAwait(false);

return result;
}
catch (Exception e)
{
Logger.Error(e);
throw;
}
}

private async Task<IHttpActionResult> PostInternal(BotActivity activity)
{
// TaskCanceledException source #1.
await SendTypingIndicator(activity).ConfigureAwait(false);

var type = activity.GetActivityType();
if (type == ActivityTypes.Message)
{
// TaskCanceledException source #2.
var flag = await LoadFlagFromBotState(activity).ConfigureAwait(false);

// Some logic slightly altering flow according to value of 'flag'.

// TaskCanceledException source #3.
await Conversation.SendAsync(activity, () => new RootDialog()).ConfigureAwait(false);
}

return Ok();
}

private async Task SendTypingIndicator(BotActivity activity)
{
var reply = activity.CreateReply();
reply.Type = ActivityTypes.Typing;
reply.Text = null;
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

await connector.Conversations.ReplyToActivityAsync(reply).ConfigureAwait(false);
}

private async Task<bool> LoadFlagFromBotState(BotActivity activity)
{
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
var botDataStore = scope.Resolve<IBotDataStore<BotData>>();
var key = Address.FromActivity(activity);
var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None).ConfigureAwait(false);

var flag = userData.GetProperty<bool>("TheFlag");
if (!flag)
{
userData.SetProperty("TheFlag", true);
await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None).ConfigureAwait(false);
await botDataStore.FlushAsync(key, CancellationToken.None).ConfigureAwait(false);
}

return flag;
}
}


And here are the URLs of external dependencies which timed-out according to Application Insights:




  • For TaskCanceledException source #1:


    • https://directline.botframework.com/v3/conversations/<CONV_ID>/activities/<ACT_ID>



  • For TaskCanceledException source #2:


    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:user<USER_ID>



  • For TaskCanceledException source #3:


    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:private<CONV_ID>:<USER_ID>

    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:conversation<CONV_ID>

    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:user<USER_ID>




Additional information




  • Our bot is hosted as an App Service within Microsoft Azure cloud.

  • We plugged in custom BotState manager according to following documentation so that all the BotState is stored within Cosmos DB.

  • Our Cosmos DB instance is within same Azure Resource Group as the bot App Service.

  • We verified the availability and throughput of our Cosmos DB instance and everything seems to be perfectly in order.

  • We are using DirectLine as the communication channel.

  • The TaskCanceledException happens randomly, there does not seem to be any specific steps which needs to be performed to reproduce this.

  • We tried to adapt the async-await pattern the 'proper way', for example by making sure that we are not mixing any sync and async code and by using ConfigureAwait(false) everywhere where the await keyword is.


Conclusion



We have been trying to figure out the root cause for some time, however we have no clue what could be possibly the problem here. We ruled out the networking issues due to the fact that we utilize Azure's PaaS here.



I will appreciate any advice.










share|improve this question













Problem description



We have deployed our chat bot to the Azure cloud as an App Service and within Application Insights we see several exceptions of type TaskCanceledException popping up randomly.



After taking a look at the logs we have identified three lines of code, which are throwing the exceptions. I have tagged these as TaskCanceledException source #. Here is simplified structure of our messaging controller for the reference:



public async Task<IHttpActionResult> Post([FromBody]BotActivity activity)
{
try
{
var result = await PostInternal(activity).ConfigureAwait(false);

return result;
}
catch (Exception e)
{
Logger.Error(e);
throw;
}
}

private async Task<IHttpActionResult> PostInternal(BotActivity activity)
{
// TaskCanceledException source #1.
await SendTypingIndicator(activity).ConfigureAwait(false);

var type = activity.GetActivityType();
if (type == ActivityTypes.Message)
{
// TaskCanceledException source #2.
var flag = await LoadFlagFromBotState(activity).ConfigureAwait(false);

// Some logic slightly altering flow according to value of 'flag'.

// TaskCanceledException source #3.
await Conversation.SendAsync(activity, () => new RootDialog()).ConfigureAwait(false);
}

return Ok();
}

private async Task SendTypingIndicator(BotActivity activity)
{
var reply = activity.CreateReply();
reply.Type = ActivityTypes.Typing;
reply.Text = null;
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

await connector.Conversations.ReplyToActivityAsync(reply).ConfigureAwait(false);
}

private async Task<bool> LoadFlagFromBotState(BotActivity activity)
{
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
var botDataStore = scope.Resolve<IBotDataStore<BotData>>();
var key = Address.FromActivity(activity);
var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None).ConfigureAwait(false);

var flag = userData.GetProperty<bool>("TheFlag");
if (!flag)
{
userData.SetProperty("TheFlag", true);
await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None).ConfigureAwait(false);
await botDataStore.FlushAsync(key, CancellationToken.None).ConfigureAwait(false);
}

return flag;
}
}


And here are the URLs of external dependencies which timed-out according to Application Insights:




  • For TaskCanceledException source #1:


    • https://directline.botframework.com/v3/conversations/<CONV_ID>/activities/<ACT_ID>



  • For TaskCanceledException source #2:


    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:user<USER_ID>



  • For TaskCanceledException source #3:


    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:private<CONV_ID>:<USER_ID>

    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:conversation<CONV_ID>

    • https://docdb-instance-url.documents.azure.com/dbs/<DB_NAME>/colls/BotState/docs/directline:user<USER_ID>




Additional information




  • Our bot is hosted as an App Service within Microsoft Azure cloud.

  • We plugged in custom BotState manager according to following documentation so that all the BotState is stored within Cosmos DB.

  • Our Cosmos DB instance is within same Azure Resource Group as the bot App Service.

  • We verified the availability and throughput of our Cosmos DB instance and everything seems to be perfectly in order.

  • We are using DirectLine as the communication channel.

  • The TaskCanceledException happens randomly, there does not seem to be any specific steps which needs to be performed to reproduce this.

  • We tried to adapt the async-await pattern the 'proper way', for example by making sure that we are not mixing any sync and async code and by using ConfigureAwait(false) everywhere where the await keyword is.


Conclusion



We have been trying to figure out the root cause for some time, however we have no clue what could be possibly the problem here. We ruled out the networking issues due to the fact that we utilize Azure's PaaS here.



I will appreciate any advice.







c# .net azure async-await botframework






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 19:50









Markkknk

16918




16918












  • Have you tried increasing the timeout?
    – Paulo Morgado
    Nov 23 at 7:20










  • No, we have left the default timeout intervals in place, which are about 1 minute. Most of the time the duration of these external calls are just couple milliseconds according to App Insights.
    – Markkknk
    Nov 23 at 9:10












  • That would be a way of seeing if it takes more than one minute or never replies.
    – Paulo Morgado
    Nov 23 at 14:49










  • The ConfigureAwait(false) is causing the HttpContext to be lost, and it is probably sometimes disposed when it is expected to be available.
    – Eric Dahlvang
    Nov 26 at 17:12










  • More information about how .ConfigureAwait(false) affects httpcontext can be found in this answer: stackoverflow.com/a/28433417/86646
    – Eric Dahlvang
    Nov 26 at 19:52


















  • Have you tried increasing the timeout?
    – Paulo Morgado
    Nov 23 at 7:20










  • No, we have left the default timeout intervals in place, which are about 1 minute. Most of the time the duration of these external calls are just couple milliseconds according to App Insights.
    – Markkknk
    Nov 23 at 9:10












  • That would be a way of seeing if it takes more than one minute or never replies.
    – Paulo Morgado
    Nov 23 at 14:49










  • The ConfigureAwait(false) is causing the HttpContext to be lost, and it is probably sometimes disposed when it is expected to be available.
    – Eric Dahlvang
    Nov 26 at 17:12










  • More information about how .ConfigureAwait(false) affects httpcontext can be found in this answer: stackoverflow.com/a/28433417/86646
    – Eric Dahlvang
    Nov 26 at 19:52
















Have you tried increasing the timeout?
– Paulo Morgado
Nov 23 at 7:20




Have you tried increasing the timeout?
– Paulo Morgado
Nov 23 at 7:20












No, we have left the default timeout intervals in place, which are about 1 minute. Most of the time the duration of these external calls are just couple milliseconds according to App Insights.
– Markkknk
Nov 23 at 9:10






No, we have left the default timeout intervals in place, which are about 1 minute. Most of the time the duration of these external calls are just couple milliseconds according to App Insights.
– Markkknk
Nov 23 at 9:10














That would be a way of seeing if it takes more than one minute or never replies.
– Paulo Morgado
Nov 23 at 14:49




That would be a way of seeing if it takes more than one minute or never replies.
– Paulo Morgado
Nov 23 at 14:49












The ConfigureAwait(false) is causing the HttpContext to be lost, and it is probably sometimes disposed when it is expected to be available.
– Eric Dahlvang
Nov 26 at 17:12




The ConfigureAwait(false) is causing the HttpContext to be lost, and it is probably sometimes disposed when it is expected to be available.
– Eric Dahlvang
Nov 26 at 17:12












More information about how .ConfigureAwait(false) affects httpcontext can be found in this answer: stackoverflow.com/a/28433417/86646
– Eric Dahlvang
Nov 26 at 19:52




More information about how .ConfigureAwait(false) affects httpcontext can be found in this answer: stackoverflow.com/a/28433417/86646
– Eric Dahlvang
Nov 26 at 19:52

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437305%2frandom-timeouts-taskcanceledexception-within-botframework%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437305%2frandom-timeouts-taskcanceledexception-within-botframework%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu