Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrent modification during iteration bug fix #514

Merged
merged 1 commit into from
Feb 17, 2025

Conversation

aNOOBisTheGod
Copy link
Contributor

Hey there!

Sometimes I am getting a bug like this:

Concurrent modification during iteration: _Map len:1.
StackTrace: #0      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:635)
#1      UA.stop (package:sip_ua/src/ua.dart:325)

I was wondering what causes the bug and found that there is a case when call is stopping but package is trying to destroy session in sessions list at the same time. I saw that you have _stopping flag already in your stop function and use it to prevent list modifications if they can cause an error. So I taught that I can use that flag to fix my problem.

Seems like I am not getting this error any more :)

@aNOOBisTheGod
Copy link
Contributor Author

@mikaelwills take a look please :)

@mikaelwills
Copy link
Collaborator

@aNOOBisTheGod the issue is that the _sessions map is being modified during interation. ua.dart:325

 _sessions.forEach((String? key, _) {
      if (_sessions.containsKey(key)) {
        logger.d('closing session $key');
        try {
          RTCSession rtcSession = _sessions[key]!;
          if (!rtcSession.isEnded()) {
            rtcSession.terminate();
          }
        } catch (e, s) {
          logger.e(e.toString(), error: e, stackTrace: s);
        }
      }
    });

Can you test what might be two better solutions:

1: Replace the _sessions.forEach with a for loop

for (String? key in _sessions.keys) {}

2: Create a copy of the map to iterate over

_sessions.keys.toList().forEach((String? key) {}

@mikaelwills mikaelwills force-pushed the main branch 2 times, most recently from 1db2473 to 4e8e03f Compare February 4, 2025 13:42
@aNOOBisTheGod
Copy link
Contributor Author

@aNOOBisTheGod the issue is that the _sessions map is being modified during interation. ua.dart:325

 _sessions.forEach((String? key, _) {
      if (_sessions.containsKey(key)) {
        logger.d('closing session $key');
        try {
          RTCSession rtcSession = _sessions[key]!;
          if (!rtcSession.isEnded()) {
            rtcSession.terminate();
          }
        } catch (e, s) {
          logger.e(e.toString(), error: e, stackTrace: s);
        }
      }
    });

Can you test what might be two better solutions:

1: Replace the _sessions.forEach with a for loop

for (String? key in _sessions.keys) {}

2: Create a copy of the map to iterate over

_sessions.keys.toList().forEach((String? key) {}

The second solution had fixed the bug. PR updated

@mikaelwills mikaelwills merged commit 9117018 into flutter-webrtc:main Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants