Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 610f82fe authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Handle DeadObjectException in ContextHubEndpointBroker.

Updates the code to close the session with the right reason if a send message fails with DeadObjectException.

Also updates the logging code to avoid printing the entire stacktrace to avoid logspam.

Bug: 443346395
Flag: EXEMPT BUGFIX
Test: Presubmits
Change-Id: I5925e6e0a4d4bf65f6710d0ffb9db0f98a636869
parent 8776c91d
Loading
Loading
Loading
Loading
+9 −4
Original line number Original line Diff line number Diff line
@@ -35,6 +35,7 @@ import android.hardware.location.ContextHubTransaction;
import android.hardware.location.IContextHubTransactionCallback;
import android.hardware.location.IContextHubTransactionCallback;
import android.hardware.location.NanoAppState;
import android.hardware.location.NanoAppState;
import android.os.Binder;
import android.os.Binder;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.PowerManager.WakeLock;
@@ -321,7 +322,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
                                mHalEndpointInfo.id,
                                mHalEndpointInfo.id,
                                serviceDescriptor);
                                serviceDescriptor);
            } catch (RemoteException | IllegalArgumentException | UnsupportedOperationException e) {
            } catch (RemoteException | IllegalArgumentException | UnsupportedOperationException e) {
                Log.e(TAG, "Exception while calling HAL openEndpointSession", e);
                Log.e(TAG, "Exception while calling HAL openEndpointSession: " + e.getMessage());
                cleanupSessionResources(sessionId);
                cleanupSessionResources(sessionId);
                throw e;
                throw e;
            }
            }
@@ -413,13 +414,17 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
                try {
                try {
                    getHubInterface().sendMessageToEndpoint(sessionId, halMessage);
                    getHubInterface().sendMessageToEndpoint(sessionId, halMessage);
                } catch (RemoteException e) {
                } catch (RemoteException e) {
                    byte reason =
                            (e instanceof DeadObjectException)
                                    ? Reason.HUB_RESET
                                    : Reason.UNSPECIFIED;
                    Log.e(
                    Log.e(
                            TAG,
                            TAG,
                            "Exception while sending message on session "
                            "Exception while sending message on session "
                                    + sessionId
                                    + sessionId
                                    + ", closing session",
                                    + ", closing session: "
                            e);
                                    + e.getMessage());
                    notifySessionClosedToBoth(sessionId, Reason.UNSPECIFIED);
                    notifySessionClosedToBoth(sessionId, reason);
                }
                }
            } else {
            } else {
                IContextHubTransactionCallback wrappedCallback =
                IContextHubTransactionCallback wrappedCallback =
+2 −2
Original line number Original line Diff line number Diff line
@@ -179,7 +179,7 @@ class HubInfoRegistry implements ContextHubHalEndpointCallback.IEndpointLifecycl
        try {
        try {
            hubInfos = mContextHubWrapper.getHubs();
            hubInfos = mContextHubWrapper.getHubs();
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException while getting Hub info", e);
            Log.e(TAG, "RemoteException while getting Hub info: " + e.getMessage());
            hubInfos = Collections.emptyList();
            hubInfos = Collections.emptyList();
        }
        }


@@ -193,7 +193,7 @@ class HubInfoRegistry implements ContextHubHalEndpointCallback.IEndpointLifecycl
        try {
        try {
            endpointInfos = mContextHubWrapper.getEndpoints();
            endpointInfos = mContextHubWrapper.getEndpoints();
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException while getting Hub info", e);
            Log.e(TAG, "RemoteException while getting Hub info:" + e.getMessage());
            endpointInfos = Collections.emptyList();
            endpointInfos = Collections.emptyList();
        }
        }


+19 −0
Original line number Original line Diff line number Diff line
@@ -45,6 +45,7 @@ import android.hardware.contexthub.Reason;
import android.hardware.location.IContextHubTransactionCallback;
import android.hardware.location.IContextHubTransactionCallback;
import android.hardware.location.NanoAppState;
import android.hardware.location.NanoAppState;
import android.os.Binder;
import android.os.Binder;
import android.os.DeadObjectException;
import android.os.RemoteException;
import android.os.RemoteException;
import android.platform.test.annotations.Presubmit;
import android.platform.test.annotations.Presubmit;
import android.util.Log;
import android.util.Log;
@@ -560,6 +561,24 @@ public class ContextHubEndpointTest {
        unregisterExampleEndpoint(endpoint);
        unregisterExampleEndpoint(endpoint);
    }
    }


    @Test
    public void testSendMessageDeadObjectExceptionClosesSession() throws RemoteException {
        IContextHubEndpoint endpoint = registerExampleEndpoint();
        int sessionId = openTestSession(endpoint);

        doThrow(new DeadObjectException("Intended exception in test"))
                .when(mMockEndpointCommunications)
                .sendMessageToEndpoint(anyInt(), any(Message.class));
        endpoint.sendMessage(sessionId, SAMPLE_MESSAGE, null);
        restartHalAndVerifyHubRegistration();

        // Confirm that the service can close our session on our behalf when the HAL restarts.
        verify(mMockCallback).onSessionClosed(sessionId, HubEndpoint.REASON_ENDPOINT_STOPPED);
        assertThat(mEndpointManager.getNumAvailableSessions()).isEqualTo(SESSION_ID_RANGE);

        unregisterExampleEndpoint(endpoint);
    }

    /** A helper method to create a session and validates reliable message sending. */
    /** A helper method to create a session and validates reliable message sending. */
    private void testMessageTransactionInternal(
    private void testMessageTransactionInternal(
            IContextHubEndpoint endpoint, boolean deliverMessageStatus) throws RemoteException {
            IContextHubEndpoint endpoint, boolean deliverMessageStatus) throws RemoteException {