supabase-community / realtime-csharp

A C# client library for supabase/realtime.
https://supabase-community.github.io/realtime-csharp/api/Supabase.Realtime.Client.html
MIT License
70 stars 12 forks source link

Task Cancelled Exceptions for long-running channel connections #19

Open bdcodestuff opened 1 year ago

bdcodestuff commented 1 year ago

Hello,

I'm using this excellent library in a mobile app that subscribes to changes on various supabase tables as follows:

Client.Instance.From<Condition>().On(Supabase.Client.ChannelEventType.Insert,action)
Client.Instance.From<Condition>().On(Supabase.Client.ChannelEventType.Update,action)
others...

where Condition is a database table model, and action is a callback to fire when the event occurs

This works great initially but after long periods of time I eventually get crashes due to unhandled "Task Cancelled" exceptions.

Here's a possibly useful stack trace from MS App center sent from the app when it crashes. I also noticed the same behavior when setting up long-running channel subscriptions within an ASP Netcore server app:

Websocket.Client.WebsocketClient.StartClient(Uri uri, CancellationToken token, ReconnectionType type, Boolean failFast)
Websocket.Client.WebsocketClient.StartInternal(Boolean failFast)
Supabase.Realtime.Socket.Connect()
System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object )
System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
System.Threading.ThreadPoolWorkQueue.Dispatch()
System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
System.Threading.Thread.StartCallback()

Any thoughts why this might be happening and what I could do to fix it? It causes my app to crash every few hours throughout the day...

acupofjose commented 1 year ago

@bdcodestuff thanks for the issue! Glad you’re enjoying the library.

Three questions:

bdcodestuff commented 1 year ago

Thanks as always for the quick response.

1.) It's tough to say exactly, generally it occurs no sooner than 45 mins after initializing the app and the channel subscriptions and not when I'm actively using the app (i.e. I believe it tends to occur when the app is in the background and the channels are idle) 2.) It seems inconsistent, sometimes it's 45 minutes, sometimes it's a few hours before it occurs 3.) this is using version 3.x.x

Do you think the new Dependency Injection restructure in 4.x.x would make a difference?

acupofjose commented 1 year ago

Hm. Let me get back to you. I'm going to set up a testing project and see if I can replicate the issue. My gut says that you may be backgrounding the socket connection and the Task.Cancelled is eventually thrown from a connection timeout (say, losing connection for a long period of time?). If that's the case, a simple try/catch and reconnection would fix it.

Idk. Need some more data, so I'll test a bit!

acupofjose commented 1 year ago

@bdcodestuff alright. I'm struggling to duplicate your issue - could you give some more information please?

bdcodestuff commented 1 year ago

@acupofjose -- thanks so much for taking this on.

1.) This is a hosted instance 2.) Supabase-csharp v0.5.3 3.) it's on Maui (formerly Xamarin) 4.) RLS is turned on in Supabase but I've opened it up so that all users can select, update, insert (at least for the time being) 5.) Tokens and Auth are generally working fine

I did try to to wrap the subscription command in a try/catch with a reconnection on Task cancelled exception but it didn't seem to make a difference. I'm also trying out supabase 0.6.1 in the hopes that the DI design will maybe eliminate the issue. I can get back to you on that.