Fix race condition while WallpaperMS rebinding service.

Connection object could not receive onServiceConnected after
new process of the service was active.

How issue happen:
1. The process of wallpaper service died, AM schedule to restart,
WallpaperMS schedule to rebind.
2. WallpaperMS bind service with new connection object.
=> ActiveService bring up new process. Add this ServiceRecord to
   mPendingServices.
3. WallpaperMS unbind previous connection object.
=> ActiveService remove previous connection, also remove this
   ServiceRecord object from mPendingServices list.
4. Process ready, attach application but cannot find the service
   from pendingServices.

Solution: Do not remove the ServiceRecord object from mPendingServices
if there is any connection left.

Also use runnable object instead of method reference.

Bug: 147964645
Bug: 149625847
Test: atest WallpaperManagerTest WallpaperManagerServiceTests
Change-Id: I3adec66de8ac0c7efc9ad3ffbf604b01eacb5720
This commit is contained in:
wilsonshih
2020-02-24 11:51:43 +08:00
parent a67e14a6e1
commit d155133382
2 changed files with 11 additions and 4 deletions

View File

@@ -3286,8 +3286,11 @@ public final class ActiveServices {
}
}
// If unbound while waiting to start, remove the pending service
mPendingServices.remove(s);
// If unbound while waiting to start and there is no connection left in this service,
// remove the pending service
if (s.getConnections().isEmpty()) {
mPendingServices.remove(s);
}
if ((c.flags&Context.BIND_AUTO_CREATE) != 0) {
boolean hasAutoCreate = s.hasAutoCreateConnections();

View File

@@ -1173,6 +1173,10 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
}
};
private Runnable mTryToRebindRunnable = () -> {
tryToRebind();
};
WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper, int clientUid) {
mInfo = info;
mWallpaper = wallpaper;
@@ -1279,7 +1283,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
saveSettingsLocked(mWallpaper.userId);
}
FgThread.getHandler().removeCallbacks(mResetRunnable);
mContext.getMainThreadHandler().removeCallbacks(this::tryToRebind);
mContext.getMainThreadHandler().removeCallbacks(mTryToRebindRunnable);
}
}
}
@@ -1337,7 +1341,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
< WALLPAPER_RECONNECT_TIMEOUT_MS) {
// Bind fail without timeout, schedule rebind
Slog.w(TAG, "Rebind fail! Try again later");
mContext.getMainThreadHandler().postDelayed(this::tryToRebind, 1000);
mContext.getMainThreadHandler().postDelayed(mTryToRebindRunnable, 1000);
} else {
// Timeout
Slog.w(TAG, "Reverting to built-in wallpaper!");