Fixes collection modified while iterating by cloning the collection.

During a user switch, in MediaRouterService, a collection of users was
modified while iterating, which ends up skipping some elements of the
collection unintentionally. This CL creates a clone of the collection
before the for loop to prevent the issue.

Bug: 260410380
Test: n/a
Change-Id: Iec4d460ba0a0ca1325506e729f58715bb2d68898
This commit is contained in:
Yasin Kilicdere
2022-11-25 18:55:44 +00:00
parent 2ff1284e94
commit 8beafe3ab0
2 changed files with 10 additions and 6 deletions

View File

@@ -645,9 +645,11 @@ class MediaRouter2ServiceImpl {
"userId: %d", newActiveUserId));
mCurrentActiveUserId = newActiveUserId;
for (int i = 0; i < mUserRecords.size(); i++) {
int userId = mUserRecords.keyAt(i);
UserRecord userRecord = mUserRecords.valueAt(i);
// disposeUserIfNeededLocked might modify the collection, hence clone
final var userRecords = mUserRecords.clone();
for (int i = 0; i < userRecords.size(); i++) {
int userId = userRecords.keyAt(i);
UserRecord userRecord = userRecords.valueAt(i);
if (isUserActiveLocked(userId)) {
// userId corresponds to the active user, or one of its profiles. We
// ensure the associated structures are initialized.

View File

@@ -665,9 +665,11 @@ public final class MediaRouterService extends IMediaRouterService.Stub
synchronized (mLock) {
if (mCurrentActiveUserId != newActiveUserId) {
mCurrentActiveUserId = newActiveUserId;
for (int i = 0; i < mUserRecords.size(); i++) {
int userId = mUserRecords.keyAt(i);
UserRecord userRecord = mUserRecords.valueAt(i);
// disposeUserIfNeededLocked might modify the collection, hence clone
final var userRecords = mUserRecords.clone();
for (int i = 0; i < userRecords.size(); i++) {
int userId = userRecords.keyAt(i);
UserRecord userRecord = userRecords.valueAt(i);
if (isUserActiveLocked(userId)) {
// userId corresponds to the active user, or one of its profiles. We
// ensure the associated structures are initialized.