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

Commit 1f139252 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes I43c532b1,I5925e6e0 into main

* changes:
  Defer endpoint session cleanup on openSession HAL errors
  Handle DeadObjectException in ContextHubEndpointBroker.
parents 19abea72 98ef00fe
Loading
Loading
Loading
Loading
+27 −11
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.hardware.location.ContextHubTransaction;
import android.hardware.location.IContextHubTransactionCallback;
import android.hardware.location.NanoAppState;
import android.os.Binder;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
@@ -90,8 +91,8 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
    /** The context of the service. */
    private final Context mContext;

    /** The shared executor service for handling session operation timeout. */
    private final ScheduledExecutorService mSessionTimeoutExecutor;
    /** The shared executor service to defer operations. */
    private final ScheduledExecutorService mExecutor;

    /** The proxy to talk to the Context Hub HAL for endpoint communication. */
    @GuardedBy("mRegistrationLock")
@@ -267,7 +268,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
            String packageName,
            String attributionTag,
            ContextHubTransactionManager transactionManager,
            ScheduledExecutorService sessionTimeoutExecutor) {
            ScheduledExecutorService executor) {
        mContext = context;
        mHubInterface = hubInterface;
        mEndpointManager = endpointManager;
@@ -277,7 +278,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
        mPackageName = packageName;
        mAttributionTag = attributionTag;
        mTransactionManager = transactionManager;
        mSessionTimeoutExecutor = sessionTimeoutExecutor;
        mExecutor = executor;

        mPid = Binder.getCallingPid();
        mUid = Binder.getCallingUid();
@@ -321,9 +322,20 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
                                mHalEndpointInfo.id,
                                serviceDescriptor);
            } catch (RemoteException | IllegalArgumentException | UnsupportedOperationException e) {
                Log.e(TAG, "Exception while calling HAL openEndpointSession", e);
                cleanupSessionResources(sessionId);
                throw e;
                Log.e(
                        TAG,
                        "Exception on HAL openEndpointSession (id="
                                + sessionId
                                + "), closing session: "
                                + e.getMessage());
                mExecutor.execute(
                        () -> {
                            byte reason =
                                    (e instanceof DeadObjectException)
                                            ? Reason.HUB_RESET
                                            : Reason.UNSPECIFIED;
                            onCloseEndpointSession(sessionId, reason);
                        });
            }

            return sessionId;
@@ -413,13 +425,17 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
                try {
                    getHubInterface().sendMessageToEndpoint(sessionId, halMessage);
                } catch (RemoteException e) {
                    byte reason =
                            (e instanceof DeadObjectException)
                                    ? Reason.HUB_RESET
                                    : Reason.UNSPECIFIED;
                    Log.e(
                            TAG,
                            "Exception while sending message on session "
                                    + sessionId
                                    + ", closing session",
                            e);
                    notifySessionClosedToBoth(sessionId, Reason.UNSPECIFIED);
                                    + ", closing session: "
                                    + e.getMessage());
                    notifySessionClosedToBoth(sessionId, reason);
                }
            } else {
                IContextHubTransactionCallback wrappedCallback =
@@ -701,7 +717,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
                existingSessionActive = false;
                Session pendingSession = new Session(initiator, true);
                pendingSession.setSessionOpenTimeoutFuture(
                        mSessionTimeoutExecutor.schedule(
                        mExecutor.schedule(
                                () -> onEndpointSessionOpenRequestTimeout(sessionId),
                                OPEN_SESSION_REQUEST_TIMEOUT_SECONDS,
                                TimeUnit.SECONDS));
+5 −5
Original line number Diff line number Diff line
@@ -117,8 +117,8 @@ import java.util.function.Consumer;
    /** The interface for endpoint communication (retrieved from HAL in init()) */
    private IEndpointCommunication mHubInterface = null;

    /** Thread pool executor for handling timeout */
    private final ScheduledExecutorService mSessionTimeoutExecutor;
    /** Thread pool executor to be shared with all endpoints */
    private final ScheduledExecutorService mExecutor;

    /*
     * The list of previous registration records.
@@ -168,12 +168,12 @@ import java.util.function.Consumer;
            IContextHubWrapper contextHubProxy,
            HubInfoRegistry hubInfoRegistry,
            ContextHubTransactionManager transactionManager,
            ScheduledExecutorService scheduledExecutorService) {
            ScheduledExecutorService executor) {
        mContext = context;
        mContextHubProxy = contextHubProxy;
        mHubInfoRegistry = hubInfoRegistry;
        mTransactionManager = transactionManager;
        mSessionTimeoutExecutor = scheduledExecutorService;
        mExecutor = executor;
    }

    /* package */ ContextHubEndpointManager(
@@ -300,7 +300,7 @@ import java.util.function.Consumer;
                            packageName,
                            attributionTag,
                            mTransactionManager,
                            mSessionTimeoutExecutor);
                            mExecutor);
            broker.register();
            mEndpointMap.put(endpointId, broker);

+2 −2
Original line number Diff line number Diff line
@@ -179,7 +179,7 @@ class HubInfoRegistry implements ContextHubHalEndpointCallback.IEndpointLifecycl
        try {
            hubInfos = mContextHubWrapper.getHubs();
        } 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();
        }

@@ -193,7 +193,7 @@ class HubInfoRegistry implements ContextHubHalEndpointCallback.IEndpointLifecycl
        try {
            endpointInfos = mContextHubWrapper.getEndpoints();
        } 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();
        }

+19 −0
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ import android.hardware.contexthub.Reason;
import android.hardware.location.IContextHubTransactionCallback;
import android.hardware.location.NanoAppState;
import android.os.Binder;
import android.os.DeadObjectException;
import android.os.RemoteException;
import android.platform.test.annotations.Presubmit;
import android.util.Log;
@@ -560,6 +561,24 @@ public class ContextHubEndpointTest {
        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. */
    private void testMessageTransactionInternal(
            IContextHubEndpoint endpoint, boolean deliverMessageStatus) throws RemoteException {