appwrite / sdk-for-flutter

[READ-ONLY] Official Appwrite Flutter SDK ๐Ÿ’™
https://appwrite.io
BSD 3-Clause "New" or "Revised" License
369 stars 110 forks source link

๐Ÿ› Bug Report: Concurrent Modification Error in RealtimeMixin Causing Crashes #203

Closed monzim closed 6 months ago

monzim commented 6 months ago

๐Ÿ‘Ÿ Reproduction steps

Create a Flutter project and integrate the Appwrite SDK. Set up real-time subscriptions using RealtimeMixin.subscribeTo. The error occurs intermittently, particularly when subscriptions are being closed or created while the _createSocket method is running.

E/flutter (22154): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Concurrent modification during iteration: _Map len:0.
E/flutter (22154): #0      _CompactIterator.moveNext (dart:collection-patch/compact_hash.dart:714:7)
E/flutter (22154): #1      RealtimeMixin._createSocket.<anonymous closure> (package:appwrite/src/realtime_mixin.dart:91:49)
E/flutter (22154): #2      _RootZone.runGuarded (dart:async/zone.dart:1582:10)
E/flutter (22154): #3      _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:418:13)
E/flutter (22154): #4      _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:428:7)
E/flutter (22154): #5      _BufferingStreamSubscription._close (dart:async/stream_impl.dart:317:7)
E/flutter (22154): #6      _ForwardingStream._handleDone (dart:async/stream_pipe.dart:99:10)

๐Ÿ‘ Expected behavior

I'm encountering a "Concurrent modification during iteration: _Map len:0" error when using the Appwrite Flutter SDK for real-time subscriptions. This error seems to be originating within the RealtimeMixin._createSocket method (line 91).

The error appears to be related to concurrent access to the _subscriptions map within the RealtimeMixin. This issue causes the app to crash unexpectedly, disrupting the real-time functionality.

๐Ÿ‘Ž Actual Behavior

The app crashes: The "Unhandled Exception: Concurrent modification during iteration" is a critical error. In most cases, this would lead to an immediate crash of the Flutter application.

Disrupted real-time updates: The error originates from the RealtimeMixin, which is responsible for managing real-time data updates. So, even before the potential crash, the functionality related to receiving and handling real-time events would likely be disrupted.

Difficulty pinpointing the issue: The stack trace points to the Appwrite SDK's internal code. This makes it challenging for developers to directly diagnose and fix the issue, as it might require modifications to the SDK itself.

๐ŸŽฒ Appwrite version

Version 1.0.x

๐Ÿ’ป Operating system

MacOS

๐Ÿงฑ Your Environment

๐Ÿ‘€ Have you spent some time to check if this issue has been raised before?

๐Ÿข Have you read the Code of Conduct?

stnguyen90 commented 6 months ago

@monzim, thanks for raising ๐Ÿ™๐Ÿผ . It looks like this is a duplicate of https://github.com/appwrite/sdk-for-flutter/issues/200 so I'm going to close it as so.

monzim commented 5 months ago

@monzim, thanks for raising ๐Ÿ™๐Ÿผ . It looks like this is a duplicate of #200 so I'm going to close it as so.

After making some changes to the _createSocket and the _cleanup methods in the /realtime_mixin.dart file, the error has been resolved. This worked for me. Can I make a pull request to incorporate these changes? I guess this repo is readonly. If so, how can I proceed?