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

Commit 98ef00fe authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Defer endpoint session cleanup on openSession HAL errors

This change defers the cleanup of endpoint session resources to an
executor when an exception occurs during the HAL `openEndpointSession`
call. Instead of synchronously cleaning up and re-throwing, the session
is closed asynchronously with an appropriate reason. The executor is
also renamed to reflect its broader use.

Bug: 443346395
Flag: EXEMPT BUGFIX
Test: Presubmit
Change-Id: I43c532b1c4970335f71acca217f6309f01de2e5a
parent 610f82fe
Loading
Loading
Loading
Loading
+19 −8
Original line number Diff line number Diff line
@@ -91,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")
@@ -268,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;
@@ -278,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();
@@ -322,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.getMessage());
                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;
@@ -706,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);