Fix unbind crash in AbstractRemoteService.

Crashes if there's an attempt to unbind when the service isn't already
bound. It's unclear which service is triggering this code path though.

Fix: 230543089
Change-Id: Ia108c709ed3c82ca18e581b464c963d52e108a3e
This commit is contained in:
Ahaan Ugale
2022-05-31 18:44:28 -07:00
parent d88a6933e8
commit 67815e4fd8

View File

@@ -88,6 +88,7 @@ public abstract class AbstractRemoteService<S extends AbstractRemoteService<S, I
private final int mBindingFlags;
protected I mService;
private boolean mBound;
private boolean mConnecting;
private boolean mDestroyed;
private boolean mServiceDied;
@@ -255,8 +256,10 @@ public abstract class AbstractRemoteService<S extends AbstractRemoteService<S, I
.append(String.valueOf(mDestroyed)).println();
pw.append(prefix).append(tab).append("numUnfinishedRequests=")
.append(String.valueOf(mUnfinishedRequests.size())).println();
final boolean bound = handleIsBound();
pw.append(prefix).append(tab).append("bound=")
.append(String.valueOf(mBound));
final boolean bound = handleIsBound();
pw.append(prefix).append(tab).append("connected=")
.append(String.valueOf(bound));
final long idleTimeout = getTimeoutIdleBindMillis();
if (bound) {
@@ -430,6 +433,8 @@ public abstract class AbstractRemoteService<S extends AbstractRemoteService<S, I
*/
abstract void handleBindFailure();
// This is actually checking isConnected. TODO: rename this and other related methods (or just
// stop using this class..)
private boolean handleIsBound() {
return mService != null;
}
@@ -445,6 +450,7 @@ public abstract class AbstractRemoteService<S extends AbstractRemoteService<S, I
final boolean willBind = mContext.bindServiceAsUser(mIntent, mServiceConnection, flags,
mHandler, new UserHandle(mUserId));
mBound = true;
if (!willBind) {
Slog.w(mTag, "could not bind to " + mIntent + " using flags " + flags);
@@ -469,7 +475,10 @@ public abstract class AbstractRemoteService<S extends AbstractRemoteService<S, I
}
}
mNextUnbind = 0;
mContext.unbindService(mServiceConnection);
if (mBound) {
mContext.unbindService(mServiceConnection);
mBound = false;
}
}
private class RemoteServiceConnection implements ServiceConnection {