question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MessageReceived problems after idle running in Azure

See original GitHub issue

Dotnet Core 2.0.3 Linux host on Azure

I have a bot that I’ve packaged as a Docker container running on Azure Container Instances. I have a very repeatable problem and am currently stuck. After the bot starts, it responds normally to commands I send to it. If I let it sit for about 3 or 4 minutes and send a command, I get no response and see in the log that the gateway is blocked on the MessageHandler. After another minute or so, there is an exception in the log for the task getting canceled and the bot reconnects, after which it is normal again… until I let it sit idle.

It seems like there is a websocket used to POST to Discord’s API that is becoming unusable after being idle for some time. Any idea why this is happening and the best way to resolve it quickly? Waiting for 2 minutes until it comes back to life isn’t going to work. “Debug” log level output is pasted below. I see events coming in the entire time right up to the “A MessageReceived handler…” message. It’s just outbound POSTs that seem to be affected.

03:22:55 Gateway     Received Dispatch (TYPING_START)
03:22:57 Gateway     Received Dispatch (CHANNEL_CREATE)
03:22:57 Gateway     Received Dispatch (MESSAGE_CREATE)
03:23:00 Gateway     A MessageReceived handler is blocking the gateway task.
03:23:27 Gateway     Sent Heartbeat
03:24:08 Gateway     System.Exception: Server missed last heartbeat
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Discord.ConnectionManager.<>c__DisplayClass28_0.<<StartAsync>b__0>d.MoveNext()
03:24:08 Gateway     Disconnecting
03:24:08 Gateway     Disconnecting ApiClient
03:24:36 Gateway     A MessageReceived handler has thrown an unhandled exception.:
System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Discord.Net.Rest.DefaultRestClient.<SendInternalAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Discord.Net.Rest.DefaultRestClient.<SendAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Discord.Net.Queue.JsonRestRequest.<SendAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Discord.Net.Queue.RequestBucket.<SendAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Discord.Net.Queue.RequestQueue.<SendAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Discord.API.DiscordRestApiClient.<SendInternalAsync>d__62.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Discord.API.DiscordRestApiClient.<SendJsonAsync>d__59`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Discord.API.DiscordRestApiClient.<CreateMessageAsync>d__77.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Discord.Rest.ChannelHelper.<SendMessageAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Discord.EventExtensions.<InvokeAsync>d__1`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Discord.WebSocket.DiscordSocketClient.<TimeoutWrap>d__122.MoveNext()
03:24:36 Gateway     Waiting for heartbeater
03:24:36 Gateway     Waiting for guild downloader
03:24:36 Gateway     Clearing large guild queue
03:24:36 Gateway     Raising virtual GuildUnavailables
03:24:36 Gateway     Disconnected from TESTGUILD1
03:24:36 Gateway     Disconnected from TESTGUILD2
03:24:36 Gateway     Disconnected
03:24:37 Gateway     Connecting
03:24:37 Gateway     Connecting ApiClient
03:24:37 Rest        GET gateway: 36.26 ms
03:24:37 Gateway     Received Hello
03:24:37 Gateway     Heartbeat Started
03:24:37 Gateway     Sent Heartbeat
03:24:37 Gateway     Resuming
03:24:37 Gateway     Sent Resume
03:24:37 Gateway     Received HeartbeatAck
03:24:37 Gateway     Latency = 97 ms
03:24:37 Gateway     Received Dispatch (TYPING_START)
03:24:37 Gateway     Received Dispatch (CHANNEL_CREATE)
03:24:37 Gateway     Received Dispatch (MESSAGE_CREATE)
03:24:38 Rest        POST channels/384397718916694016/messages: 108.53 ms
03:24:38 Gateway     Received Dispatch (RESUMED)
03:24:38 Gateway     Sending Status
03:24:38 Gateway     Sent StatusUpdate
03:24:38 Gateway     Connected
03:24:38 Gateway     Raising Event
03:24:38 Gateway     Connected to TESTGUILD1
03:24:38 Gateway     Connected to TESTGUILD2
03:24:38 Gateway     Resumed previous session
03:24:38 Gateway     Received Dispatch (CHANNEL_CREATE)
03:24:38 Gateway     Received Dispatch (MESSAGE_CREATE)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cowlikecommented, Dec 14, 2017

I have some good news. I deployed the bot on AWS and it’s running fine there. This seems to be something odd that is going on with Azure causing the bot to lose connection. I’ll look into that separately.

What do you guys think of letting the user specify a timeout value for their own tasks? The bot would still reconnect but it would be much faster. If there was also an event that the timeout happened, the bot could post a message about something that went wrong. Just some thoughts. I’m gong to bug MS about the Azure problem.

0reactions
luizcarlosfariacommented, May 5, 2023

For anyone stumbling across this, I recently deployed a bot to azure using Azure Kubernetes Service.

Try out AKS, ACS, Azure Container Instances, or a linux vm and see if that helps resolve the issue.

5 years later, same problem.

On Kubernetes

  • Lost connections
  • Sometimes I can’t even establish a connection.

On Docker

  • Working fine
Read more comments on GitHub >

github_iconTop Results From Across the Web

MessageReceived problems after idle running in Azure #904
For anyone stumbling across this, I recently deployed a bot to azure using Azure Kubernetes Service. Try out AKS, ACS, Azure Container Instances ......
Read more >
Azure function is not get trigger when message comes into ...
Try force resyncing your triggers by hitting the Refresh button. This is a known issue for Azure Functions that are idle for a ......
Read more >
Troubleshoot Azure point-to-site connection problems
Learn to troubleshoot and solve common point-to-site connection problems and other virtual private network errors and issues.
Read more >
Azure Function seems to be in idle
Hi,. We are running consumption based plans on Linux and we have some issues with service bus messages not being picked up properly....
Read more >
Azure Service Bus and its Complete Overview
Azure Service Bus is a messaging service on cloud used to connect any applications, devices, and services running in the cloud.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found