Skip to content
This repository has been archived by the owner on May 13, 2023. It is now read-only.

Stream Subscriptions die when auth token expires #35

Closed
KirioXX opened this issue Jan 5, 2022 · 14 comments
Closed

Stream Subscriptions die when auth token expires #35

KirioXX opened this issue Jan 5, 2022 · 14 comments

Comments

@KirioXX
Copy link

KirioXX commented Jan 5, 2022

We noticed will testing that after some time that our streams were unexpectedly closed.
I tested it with a 60 second expiry time on the JWT and received this exception after 60s :

[sentry.runZonedGuarded] [error] Uncaught zone error
[sentry.runZonedGuarded] _CastError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String?' in type cast)
[sentry.runZonedGuarded] #0      RealtimeSubscription.onError.<anonymous closure> 		package:realtime_client/src/realtime_subscription.dart:97
#1      RealtimeSubscription.trigger													package:realtime_client/src/realtime_subscription.dart:221
#2      RealtimeClient.onConnMessage.<anonymous closure>.<anonymous closure>			package:realtime_client/src/realtime_client.dart:265
#3      Iterable.forEach (dart:core/iterable.dart:279:35)
#4      RealtimeClient.onConnMessage.<anonymous closure>								package:realtime_client/src/realtime_client.dart:264
#5      new RealtimeClient.<anonymous closure>											package:realtime_client/src/realtime_client.dart:86
#6      RealtimeClient.onConnMessage													package:realtime_client/src/realtime_client.dart:249
#7      RealtimeClient.connect.<anonymous closure>										package:realtime_client/src/realtime_client.dart:108
#8      _rootRunUnary (dart:async/zone.dart:1436:47)
#9      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
#10     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
#11     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#12     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#13     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#14     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#15     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#16     _rootRunUnary (dart:async/zone.dart:1436:47)
#17     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
#18     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
#19     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#20     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#21     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:733:19)
#22     _StreamController._add (dart:async/stream_controller.dart:607:7)
#23     _rootRunUnary (dart:async/zone.dart:1436:47)
#24     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
#25     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
#26     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#27     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#28     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:733:19)
#29     _StreamController._add (dart:async/stream_controller.dart:607:7)
#30     _StreamController.add (dart:async/stream_controller.dart:554:5)
#31     new _WebSocketImpl._fromSocket.<anonymous closure> (dart:_http/websocket_impl.dart:1144:21)
#32     _rootRunUnary (dart:async/zone.dart:1436:47)
#33     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
#34     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
#35     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#36     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#37     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#38     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#39     _WebSocketProtocolTransformer._messageFrameEnd (dart:_http/websocket_impl.dart:332:23)
#40     _WebSocketProtocolTransformer.add (dart:_http/websocket_impl.dart:226:46)
#41     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#42     _rootRunUnary (dart:async/zone.dart:1436:47)
#43     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
#44     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
#45     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#46     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#47     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:733:19)
#48     _StreamController._add (dart:async/stream_controller.dart:607:7)
#49     _StreamController.add (dart:async/stream_controller.dart:554:5)
#50     _Socket._onData (dart:io-patch/socket_patch.dart:2302:41)
#51     _rootRunUnary (dart:async/zone.dart:1436:47)
#52     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
#53     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
#54     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#55     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#56     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:733:19)
#57     _StreamController._add (dart:async/stream_controller.dart:607:7)
#58     _StreamController.add (dart:async/stream_controller.dart:554:5)
#59     _RawSecureSocket._sendReadEvent (dart:io/secure_socket.dart:1000:19)
#60     _rootRun (dart:async/zone.dart:1420:47)
#61     _CustomZone.run (dart:async/zone.dart:1328:19)
#62     _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
#63     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
#64     _rootRun (dart:async/zone.dart:1428:13)
#65     _CustomZone.run (dart:async/zone.dart:1328:19)
#66     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1260:23)
#67     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
#68     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
#69     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
#70     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

It might be related to this issue #28

@KirioXX KirioXX changed the title Stream Subscriptions dye when auth token expires Stream Subscriptions die when auth token expires Jan 5, 2022
@phamhieu
Copy link
Member

@KirioXX we deployed supabase-dart v0.2.11 with the latest realtime client.

Can you give it a try and let us know if the problem persists. Thank you.

@KirioXX
Copy link
Author

KirioXX commented Jan 11, 2022

@phamhieu that's amazing just gave it a try and it's working like a charm. Thank you very much 🙌🌟

@KirioXX KirioXX closed this as completed Jan 11, 2022
@KirioXX
Copy link
Author

KirioXX commented Jan 11, 2022

@phamhieu not related to this issue.
But we see now with the new version this error messages coming through when we try to sign a user in:

VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: LateInitializationError: Field 'changedAccessToken' has not been initialized.
#0      SupabaseClient.changedAccessToken (package:supabase/src/supabase.dart)
package:supabase/src/supabase.dart:1
#1      SupabaseClient._handleTokenChanged
package:supabase/src/supabase.dart:171
#2      SupabaseClient._listenForAuthEvents.<anonymous closure>
package:supabase/src/supabase.dart:165
#3      GoTrueClient._notifyAllSubscribers.<anonymous closure>
package:gotrue/src/gotrue_client.dart:442
#4      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:539:8)
#5      GoTrueClient._notifyAllSubscribers
package:gotrue/src/gotrue_client.dart:442
#6      GoTrueClient._handleEmailSignIn
package:gotrue/src/gotrue_client.dart:353
<asynchronous suspension>
#7      _LoginPageState._signInEmailPW
package:tusks/…/login/login_page.dart:102
<asynchronous suspension>

can it be that superbase_flutter also needs an update?

@KirioXX
Copy link
Author

KirioXX commented Jan 14, 2022

Sorry, @phamhieu I think this needs to be reopened we still see the error now it's even more often.
This is the exception that we see now every 60s probably on the heardbeat:

[sentry.runZonedGuarded] [error] Uncaught zone error
[sentry.runZonedGuarded] _CastError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String?' in type cast)
[sentry.runZonedGuarded] #0      RealtimeSubscription.onError.<anonymous closure>
package:realtime_client/src/realtime_subscription.dart:97
#1      RealtimeSubscription.trigger
package:realtime_client/src/realtime_subscription.dart:225
#2      RealtimeClient.onConnMessage.<anonymous closure>.<anonymous closure>
package:realtime_client/src/realtime_client.dart:267
#3      Iterable.forEach (dart:core/iterable.dart:279:35)
#4      RealtimeClient.onConnMessage.<anonymous closure>
package:realtime_client/src/realtime_client.dart:266
#5      new RealtimeClient.<anonymous closure>
package:realtime_client/src/realtime_client.dart:88
#6      RealtimeClient.onConnMessage
package:realtime_client/src/realtime_client.dart:251
#7      RealtimeClient.connect.<anonymous closure>
package:realtime_client/src/realtime_client.dart:110
#8      _rootRunUnary (dart:async/zone.dart:1436:47)
#9      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
#10     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
#11     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#12     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#13     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#14     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#15     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#16     _rootRunUnary (dart:async/zone.dart:1436:47)
#17     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
#18     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
#19     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#20     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#21     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:733:19)
#22     _StreamController._add (dart:async/stream_controller.dart:607:7)
#23     _rootRunUnary (dart:async/zone.dart:1436:47)
#24     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
#25     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
#26     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#27     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#28     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:733:19)
#29     _StreamController._add (dart:async/stream_controller.dart:607:7)
#30     _StreamController.add (dart:async/stream_controller.dart:554:5)
#31     new _WebSocketImpl._fromSocket.<anonymous closure> (dart:_http/websocket_impl.dart:1144:21)
#32     _rootRunUnary (dart:async/zone.dart:1436:47)
#33     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
#34     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
#35     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#36     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#37     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#38     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#39     _WebSocketProtocolTransformer._messageFrameEnd (dart:_http/websocket_impl.dart:332:23)
#40     _WebSocketProtocolTransformer.add (dart:_http/websocket_impl.dart:226:46)
#41     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#42     _rootRunUnary (dart:async/zone.dart:1436:47)
#43     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
#44     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
#45     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#46     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#47     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:733:19)
#48     _StreamController._add (dart:async/stream_controller.dart:607:7)
#49     _StreamController.add (dart:async/stream_controller.dart:554:5)
#50     _Socket._onData (dart:io-patch/socket_patch.dart:2302:41)
#51     _rootRunUnary (dart:async/zone.dart:1444:13)
#52     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
#53     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
#54     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#55     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#56     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:733:19)
#57     _StreamController._add (dart:async/stream_controller.dart:607:7)
#58     _StreamController.add (dart:async/stream_controller.dart:554:5)
#59     new _RawSocket.<anonymous closure> (dart:io-patch/socket_patch.dart:1830:33)
#60     _NativeSocket.issueReadEvent.issue (dart:io-patch/socket_patch.dart:1314:14)
#61     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#62     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

As a short there solution, I tried to catch the error and restart the stream, but I can't pinpoint where the catch needs to happen.

@KirioXX KirioXX reopened this Jan 14, 2022
@bit-burger
Copy link

This is also occurring to me, but i fixed it by going back to an older version of supabase_flutter (with an older version of realtime_client

@bit-burger
Copy link

Im on version 0.1.12+1, however on a fork (that was recently merged) (https://github.com/Vinzent03/realtime-dart)

@bit-burger
Copy link

On the newest version 0.1.13 it is not working for me

@phamhieu
Copy link
Member

Hi @KirioXX @bit-burger I can't replicate the error on my side.

Can you provide a step by step guide? It will help a lot. Thank you

@clarky2233
Copy link

To reproduce this error on my end I do the following:

  1. Create a table and setup the real time stream (enable replication on that table)
  2. In my flutter front end use the supabase stream() method with a filter e.g. 'countries:id=eq.120' in the select
  3. Use a streambuilder widget to display the stream data
  4. Trigger changes to the rows I am listening to by updating columns in the database
  5. After update to database is made the error then occurs

@bit-burger
Copy link

bit-burger commented Jan 22, 2022

Thanks @clarky2233

@KirioXX
Copy link
Author

KirioXX commented Jan 22, 2022

@phamhieu Sorry for the delayed response.

I build a test project to reproduce the issue: https://github.com/KirioXX/supabase_todo .
I'm not sure if it's related to the rls or the number of items,
but when I started to press the todo items in the shared shopping list I get the error message in the picture.
After the message appears the stream connection is gone.
Screenshot 2022-01-22 at 18 10 11

@osaxma
Copy link

osaxma commented Jan 29, 2022

I found out that the error only occurs when I recover auth session. In other words, subscribing after login directly doesn't have an issue, but subscribing after a session recovery has the issue.

Workaround

After a session recovery, update the accessToken manually for the realtime client :

final res = await supabaseClient.auth.recoverSession(sessionString);
supabaseClient.realtime.setAuth(res.data?.accessToken); // <~~~ this solves it

Issue:

The issue is that onAuthStateChange does not trigger an event after a session recovery, which is used to update the accessToken for the realtime client in here:

https://github.com/supabase-community/supabase-dart/blob/2d5be1c6968da5944f8b3b039e351b38a953d815/lib/src/supabase.dart#L169-L180

If gotrue-dart emits a signedIn or tokenRefreshed event after session recovery, this issue would be fixed.

@phamhieu
Copy link
Member

I released a new supabase-dart version:

  • fix the casting error
  • fix missing gotrue event on session recover. Thanks @osaxma

Can you give it a try. Thank you

@bdlukaa
Copy link
Contributor

bdlukaa commented Apr 17, 2022

Closing as resolved!

@bdlukaa bdlukaa closed this as completed Apr 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants