botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

[LUIS] Latency in LUIS request causes Direct Line timeout

Open sayyednazeer opened this issue 9 years ago • 58 comments

My bot works fine on emulator and webchat channel. But with direct line api  it returns  500 internal server error messages.

 POST https://directline.botframework.com/api/conversations/BQf8iT1aU3ULgU4VRN2TSF/messages 500 (Internal Server Error)

i am calling it using jquery ajax,

sayyednazeer avatar Jan 20 '17 16:01 sayyednazeer

Hi @sayyednazeer, in Direct Line 1.1, we will return HTTP 500 when your bot generates an error. In this case, your bot's endpoint is returning an HTTP 500 to us, so we return it to your client.

(In Direct Line 3.0, we return a different error code so you can tell the difference between your bot's errors and our internal service errors.)

dandriscoll avatar Jan 20 '17 18:01 dandriscoll

Thanks @dandriscoll for your reply. But why i am getting this type of error. I have deployed my bot on my HTTPS server and not on azure. If it is working properly on emulator and webchat then why i am getting this error in Direct line. can you tell me if i am wrong some where and how to fix this. And also let you know some time it works fine and gives me the proper responce message.

And also let you know that, i thought may be there is some wrong with the code which i have return in my bot, so for testing i have change the code to just return a simple HELLO string from my bot to client. but then also got the same error.

sayyednazeer avatar Jan 21 '17 07:01 sayyednazeer

Hi @sayyednazeer, the failure is happening inside your bot. Have you tried debugging it with the Bot Framework Emulator?

dandriscoll avatar Jan 21 '17 15:01 dandriscoll

@dandriscoll
i have set debugger in my code you can test it.. just pass some greetings like(hi, hello etc or you can click the button available). but first open the developer tool you will notice it will fall inside the error function(to ignore error i again call the sendmessage function inside error function) in console it will show the error . BUT SOME TIME IT WORKS WITHOUT ANY ERROR.

I have debug with emulator it gives the same error 500 internal server error.

sayyednazeer avatar Jan 22 '17 07:01 sayyednazeer

error

@dandriscoll This is the screenshot while debugging with emulator Let me know if you need any more details.

sayyednazeer avatar Jan 22 '17 08:01 sayyednazeer

Hi @sayyednazeer, both the emulator and the API errors indicate that the failure is occurring inside the bot. Your C# client code seems fine although the failure does not appear to be coming from this part of the system.

Do you have any logs from inside your bot? When it generates a 500 error, do you have any traces that show where the failure is occurring?

dandriscoll avatar Jan 22 '17 17:01 dandriscoll

@dandriscoll no i have not return any code for trace and log can you tell me how to do that.. or is it possible to send you my bot code..??

sayyednazeer avatar Jan 22 '17 18:01 sayyednazeer

It depends on how your bot is written and hosted.

  1. Is your bot written in C# or Node.js?
  2. Are you hosting the bot in Azure or on a different hosting platform?

dandriscoll avatar Jan 22 '17 19:01 dandriscoll

written in C# and deployed on my HTTPS server not on azure

sayyednazeer avatar Jan 23 '17 05:01 sayyednazeer

Hi @sayyednazeer, if you're seeing HTTP 412 errors, they're probably coming from calls to the BotState client:

await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);

If so, here are instructions to fix this: https://docs.botframework.com/en-us/technical-faq/#how-can-i-fix-precondition-failed-412-or-conflict-409-http-errors

Place this code in your Global.asax.cs or similar startup code:

var builder = new ContainerBuilder();
builder
    .Register(c => new CachingBotDataStore(c.Resolve<ConnectorStore>(), CachingBotDataStoreConsistencyPolicy.LastWriteWins))
    .As<IBotDataStore<BotData>>()
    .AsSelf()
    .InstancePerLifetimeScope();
builder.Update(Conversation.Container);

dandriscoll avatar Jan 24 '17 04:01 dandriscoll

Done @dandriscoll But still my issue not fix.

I have notice my controller code goes in loop some times it calls the ConversationUpdate method 2-3 times and some time ActivityTypes.Message calls 2-3 times don't know why i have seen this kind of entries in my database log.

i am also assigning this property when making a call

data: JSON.stringify({ conversationId: convId, "text": msg, // "from": user // }),

sayyednazeer avatar Jan 24 '17 08:01 sayyednazeer

The repeated messages (2-3 times) are expected.

ConversationUpdate activities are sent multiple times in Direct Line: The first time, it's sent when your bot is added to the conversation. Then, it's sent for each user that joins the conversation.

Message activities are sometimes retried by clients when the bot generates an error. Until we solve the source of the errors, seeing retries is expected.

Can you try adding a try/catch block around your code and log the exception details? Put the try/catch at the very outer limits of each call.

try
{
    // your existing code here
}
catch (Exception e)
{
    Log(e.ToString());
    throw;
}

Be sure to call e.ToString() so you capture the call stack.

dandriscoll avatar Jan 24 '17 14:01 dandriscoll

@dandriscoll my code is already in a try catch block.

Above i have paste my MessagesController.cs code please check.

Sometimes it works properly and sometimes on every message it says 500 Internal Server error.

Why sometimes it works and sometimes not...??

sayyednazeer avatar Jan 24 '17 19:01 sayyednazeer

I have observe one more thing...

When i greet my bot it gives 500 Internal Server error.. so i tried for 5-6 times but as i said sometimes it works , so let say on 7th time when it success it gives me the response of my all the 7 request back to back.

sayyednazeer avatar Jan 24 '17 20:01 sayyednazeer

Hi @sayyednazeer, in the example you provided, does your bot respond reliably after the 7th response?

Your hosting platform may be putting your bot to sleep after an idle period. Many platforms do this. When a message comes it, the bot takes a few seconds to start, and it may exceed my server's 15 second timeout. Because your bot received all of the messages via its request pipeline, its replies come back to the channel.

dandriscoll avatar Jan 24 '17 23:01 dandriscoll

@dandriscoll yes the respond was reliable and also in a sequence.

How can i make sure that my server is putting my bot to sleep and if it is then how to fix it.

Let me show you some error screenshot also which i got yesterday on my Bot portale and App

botportal_directline directlineapp

sayyednazeer avatar Jan 25 '17 04:01 sayyednazeer

Hello @sayyednazeer, there are two approaches:

  1. Sometimes your platform has an option to keep your application running at all times.
  2. You can set up an HTTP pinger to make a request to your service (say, every 60 seconds) to keep the service running.

This is a common problem but it depends entirely on your hosting platform. We have some instructions in our FAQ for how to fix this on Azure: https://docs.botframework.com/en-us/technical-faq/#my-bot-is-slow-to-respond-to-the-first-message-it-receives-how-can-i-make-it-faster

dandriscoll avatar Jan 25 '17 06:01 dandriscoll

@dandriscoll that means the problem is with my server right ?

I have Windows Server 2008 R2 Standard

Can you guide me how to fix it in my server.

sayyednazeer avatar Jan 25 '17 09:01 sayyednazeer

Hi @sayyednazeer, I found this blog post that describes an IIS plugin that could work: http://www.diaryofaninja.com/blog/2010/05/06/keep-your-aspnet-websites-warm-and-fast-247

Here is a link to the IIS plugin: https://www.iis.net/downloads/microsoft/application-initialization

dandriscoll avatar Jan 25 '17 14:01 dandriscoll

hi @dandriscoll the above link does not so helpfull.. i have follow the blog and it was configuring the .aspx pages and not the web services.

I saw in my log when my code come to the line when it makes a call to LUIS model which takes aprox 5-6 seconds and after that it return the responce but when it calls the LUIS model which takes 5-6 sec.. at that time my DIRECT LINE app throw Internal Sever Error.

Today in my try catch block i got the following exception (I Got This Error Only At Once)

**System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 40.83.182.206:443 at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar) --- End of inner exception stack trace --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) **

sayyednazeer avatar Jan 30 '17 05:01 sayyednazeer