Merge "Avoid deadlock on soundtrigger HAL death" into rvc-dev am: 5c5a830c9a

Change-Id: Ic4591e2780fd69e54ead51ce04f2077fe3777f70
This commit is contained in:
Ytai Ben-tsvi
2020-05-05 17:37:23 +00:00
committed by Automerger Merge Worker
2 changed files with 33 additions and 20 deletions

View File

@@ -783,15 +783,17 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
@Override @Override
public void onModuleDied() { public void onModuleDied() {
synchronized (SoundTriggerMiddlewareValidation.this) { synchronized (SoundTriggerMiddlewareValidation.this) {
try { mState = ModuleStatus.DEAD;
mState = ModuleStatus.DEAD;
mCallback.onModuleDied();
} catch (RemoteException e) {
// Dead client will be handled by binderDied() - no need to handle here.
// In any case, client callbacks are considered best effort.
Log.e(TAG, "Client callback exception.", e);
}
} }
// Trigger the callback outside of the lock to avoid deadlocks.
try {
mCallback.onModuleDied();
} catch (RemoteException e) {
// Dead client will be handled by binderDied() - no need to handle here.
// In any case, client callbacks are considered best effort.
Log.e(TAG, "Client callback exception.", e);
}
} }
@Override @Override

View File

@@ -35,8 +35,10 @@ import android.os.RemoteException;
import android.os.ServiceSpecificException; import android.os.ServiceSpecificException;
import android.util.Log; import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@@ -166,12 +168,23 @@ class SoundTriggerModule implements IHwBinder.DeathRecipient {
} }
@Override @Override
public synchronized void serviceDied(long cookie) { public void serviceDied(long cookie) {
Log.w(TAG, String.format("Underlying HAL driver died.")); Log.w(TAG, String.format("Underlying HAL driver died."));
for (Session session : mActiveSessions) { List<ISoundTriggerCallback> callbacks = new ArrayList<>(mActiveSessions.size());
session.moduleDied(); synchronized (this) {
for (Session session : mActiveSessions) {
callbacks.add(session.moduleDied());
}
reset();
}
// Trigger the callbacks outside of the lock to avoid deadlocks.
for (ISoundTriggerCallback callback : callbacks) {
try {
callback.onModuleDied();
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
} }
reset();
} }
/** /**
@@ -379,15 +392,13 @@ class SoundTriggerModule implements IHwBinder.DeathRecipient {
/** /**
* The underlying module HAL is dead. * The underlying module HAL is dead.
* @return The client callback that needs to be invoked to notify the client.
*/ */
private void moduleDied() { private ISoundTriggerCallback moduleDied() {
try { ISoundTriggerCallback callback = mCallback;
mCallback.onModuleDied(); removeSession(this);
removeSession(this); mCallback = null;
mCallback = null; return callback;
} catch (RemoteException e) {
e.rethrowAsRuntimeException();
}
} }
private void checkValid() { private void checkValid() {