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:
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user