i8beef / HomeAutio.Mqtt.GoogleHome

MIT License
215 stars 29 forks source link

Crash when communication with google failes #99

Closed glsf91 closed 3 years ago

glsf91 commented 3 years ago

I saw the programs crashes when something is going wrong with communications to google:

17:35:16 INF] MQTT message received for topic google/home/switch2: off
Unhandled exception. [17:35:16 INF] Start processing HTTP request POST https://oauth2.googleapis.com/token
[17:35:16 INF] Sending HTTP request POST https://oauth2.googleapis.com/token
[17:35:16 INF] Received HTTP response after 56.7445ms - OK
[17:35:16 INF] End processing HTTP request after 57.5825ms - OK
[17:35:16 INF] Start processing HTTP request POST https://homegraph.googleapis.com/v1/devices:reportStateAndNotification
[17:35:16 INF] Sending HTTP request POST https://homegraph.googleapis.com/v1/devices:reportStateAndNotification
[17:35:16 INF] MQTT message received for topic google/home/switch2: off
[17:35:16 INF] Start processing HTTP request POST https://oauth2.googleapis.com/token
17:35:16 INF] Sending HTTP request POST https://oauth2.googleapis.com/token
System.Threading.Tasks.TaskCanceledException: The operation was canceled.
 ---> System.IO.IOException: Unable to read data from the transport connection: Operation canceled.
 ---> System.Net.Sockets.SocketException (125): Operation canceled
   --- End of inner exception stack trace ---
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
   at System.Net.Security.SslStream.<FillBufferAsync>g__InternalFillBufferAsync|215_0[TReadAdapter](TReadAdapter adap, ValueTask`1 task, Int32 min, Int32 initial)
   at System.Net.Security.SslStream.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at HomeAutio.Mqtt.GoogleHome.GoogleHomeGraphClient.GetAccessToken() in /home/john/HomeAutio.Mqtt.GoogleHome/src/HomeAutio.Mqtt.GoogleHome/GoogleHomeGraphClient.cs:line 204
   at HomeAutio.Mqtt.GoogleHome.GoogleHomeGraphClient.SendRequestAsync(HttpRequestMessage requestMessage) in /home/john/HomeAutio.Mqtt.GoogleHome/src/HomeAutio.Mqtt.GoogleHome/GoogleHomeGraphClient.cs:line 249
   at HomeAutio.Mqtt.GoogleHome.GoogleHomeGraphClient.SendUpdatesAsync(IList`1 devices, IDictionary`2 stateCache) in /home/john/HomeAutio.Mqtt.GoogleHome/src/HomeAutio.Mqtt.GoogleHome/GoogleHomeGraphClient.cs:line 142
   at HomeAutio.Mqtt.GoogleHome.GoogleHomeGraphService.HandleGoogleReportState(ReportStateEvent reportStateEvent) in /home/john/HomeAutio.Mqtt.GoogleHome/src/HomeAutio.Mqtt.GoogleHome/GoogleHomeGraphService.cs:line 86
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__139_1(Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

I think it should never crash but abort the current action and continue running.

i8beef commented 3 years ago

Correct. These services are all based around a "fail fast" model that will stop the entire process on failure. When running in Docker, this results in a container restart and allows the app to start from scratch, i.e., this is expected behavior because it is built around a container / service manager monitoring externally.

As for the error, your HomeGraph API file seems like it might be missing, bad, or the HomeGraph API Google setup hasn't been completed.

glsf91 commented 3 years ago

I'am not very familar with Docker. So this worked as designed. With systemd I expect I can do the same.

i8beef commented 3 years ago

Correct, as designed. I basically side stepped a lot of error / reconnect logic by saying "on any error just log and restart". Its not he most efficient, but it keeps things clean as this is ONE of like 7 or 8 little bridges like this I manage, and they all run the same way and have the same error handling approach with vastly different client implementations.

In Windows, you could run as a service to get the same restart functionality.

In Linux you'd have to setup some initd / whatever the current system process management framework is this week to do the same.... but thats why I highly recommend the Docker usage as its way easier.