supabase-community / gotrue-csharp

C# implementation of Supabase's GoTrue
https://supabase-community.github.io/gotrue-csharp/api/Supabase.Gotrue.Client.html
MIT License
39 stars 27 forks source link

Connection Failure closes application #44

Closed Kuffs2205 closed 1 year ago

Kuffs2205 commented 1 year ago

Sorry if this isn't strictly speaking, a bug but I was unsure how to communicate it.

I've had some internet issues recently and they have brought to light an issue where my application closes itself. Normally, any unhandled exceptions are caught by the .Net runtime event handler where I restart the application.

  Private Sub MyApplication_Shutdown(sender As Object, e As EventArgs) Handles Me.Shutdown
        If Restart Then Windows.Forms.Application.Restart()
    End Sub

    Private Sub MyApplication_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
        Core.Logger.WriteException(e.Exception, "UNHANDLED")
        Restart = True
    End Sub

However, it seems that when the Supabase client encounters an error, it bypasses this and just force closes the application. The only way I know what caused it is from the WIndows event log which shows.

Application: App.exe
CoreCLR Version: 7.0.323.6910
.NET Version: 7.0.3
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Net.Http.HttpRequestException: An error occurred while sending the request.
 ---> System.IO.IOException: The response ended prematurely.
 at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at Supabase.Gotrue.Helpers.MakeRequest(HttpMethod method, String url, Object data, Dictionary`2 headers)
   at Supabase.Gotrue.Helpers.MakeRequest[T](HttpMethod method, String url, Object data, Dictionary`2 headers)
   at Supabase.Gotrue.Client.RefreshToken(String refreshToken)
   at Supabase.Gotrue.Client.<InitRefreshTimer>b__70_0(Object obj)
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.QueueUserWorkItemCallback.Execute()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

The above makes me think that the connection fails when refreshing the auth token. Is there a way to handle such errors cleanly? I can find no way to wrap the call in a Try/Catch as the code seems to be in the library internals.

Thank you.

acupofjose commented 1 year ago

Actually - I would call this a bug! I'll get to work on a fix for it. Thanks for the report!

Kuffs2205 commented 1 year ago

Excellent. I'm only learning the library so I'm kind of fumbling my way though right now.