From 6499c5f5457bca168e6934679562548a94e4f7a8 Mon Sep 17 00:00:00 2001 From: Guillaume Bernos Date: Tue, 21 Jan 2025 14:37:06 +0100 Subject: [PATCH] fix(storage, android): fix an issue that could crash the app when concurrent calls to removeEventListeners were happening (#16996) --- .../storage/FlutterFirebaseStoragePlugin.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/firebase_storage/firebase_storage/android/src/main/java/io/flutter/plugins/firebase/storage/FlutterFirebaseStoragePlugin.java b/packages/firebase_storage/firebase_storage/android/src/main/java/io/flutter/plugins/firebase/storage/FlutterFirebaseStoragePlugin.java index 8593b95cd69d..c787ba5cbba2 100755 --- a/packages/firebase_storage/firebase_storage/android/src/main/java/io/flutter/plugins/firebase/storage/FlutterFirebaseStoragePlugin.java +++ b/packages/firebase_storage/firebase_storage/android/src/main/java/io/flutter/plugins/firebase/storage/FlutterFirebaseStoragePlugin.java @@ -156,16 +156,26 @@ private String registerEventChannel(String prefix, String identifier, StreamHand return identifier; } - private void removeEventListeners() { - for (String identifier : eventChannels.keySet()) { - eventChannels.get(identifier).setStreamHandler(null); + private synchronized void removeEventListeners() { + // Create a list to hold the keys to remove after iteration + List eventChannelKeys = new ArrayList<>(eventChannels.keySet()); + for (String identifier : eventChannelKeys) { + EventChannel eventChannel = eventChannels.get(identifier); + if (eventChannel != null) { + eventChannel.setStreamHandler(null); + } + eventChannels.remove(identifier); } - eventChannels.clear(); - for (String identifier : streamHandlers.keySet()) { - streamHandlers.get(identifier).onCancel(null); + // Create a list to hold the keys to remove after iteration + List streamHandlerKeys = new ArrayList<>(streamHandlers.keySet()); + for (String identifier : streamHandlerKeys) { + StreamHandler streamHandler = streamHandlers.get(identifier); + if (streamHandler != null) { + streamHandler.onCancel(null); + } + streamHandlers.remove(identifier); } - streamHandlers.clear(); } private FirebaseStorage getStorageFromPigeon(