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

Commit 4d153619 authored by Pablo Gamito's avatar Pablo Gamito
Browse files

Make ProtoLog client registration atomic

This change ensures that registering a client and its associated log groups is an atomic operation, preventing potential race conditions.

Bug: 420006066
Flag: EXEMPT minor cleanup
Change-Id: Iba969fd1a70369d3ec9fe7225ab7916414e6f45c
parent 0ed8f65f
Loading
Loading
Loading
Loading
+22 −20
Original line number Diff line number Diff line
@@ -199,9 +199,9 @@ public class ProtoLogConfigurationServiceImpl extends IProtoLogConfigurationServ
                mConfigFileCounts.put(viewerConfigFile,
                        mConfigFileCounts.getOrDefault(viewerConfigFile, 0) + 1);
            }
        }

        registerGroups(client, args.groups, args.groupsDefaultLogcatStatus);
            registerGroupsLocked(client, args.groups, args.groupsDefaultLogcatStatus);
        }

        clientBinder.linkToDeath(this, /* flags= */ 0);
    }
@@ -209,7 +209,9 @@ public class ProtoLogConfigurationServiceImpl extends IProtoLogConfigurationServ
    @Override
    public void registerGroups(@NonNull IProtoLogClient client, @NonNull RegisterGroupsArgs args)
            throws RemoteException {
        registerGroups(client, args.groups, args.groupsDefaultLogcatStatus);
        synchronized (mConfigLock) {
            registerGroupsLocked(client, args.groups, args.groupsDefaultLogcatStatus);
        }
    }

    /**
@@ -327,7 +329,8 @@ public class ProtoLogConfigurationServiceImpl extends IProtoLogConfigurationServ
        unregisterClient(IProtoLogClient.Stub.asInterface(clientBinder));
    }

    private void registerGroups(@NonNull IProtoLogClient client, @NonNull String[] groups,
    @GuardedBy("mConfigLock")
    private void registerGroupsLocked(@NonNull IProtoLogClient client, @NonNull String[] groups,
            @NonNull boolean[] logcatStatuses) throws RemoteException {
        if (groups.length != logcatStatuses.length) {
            throw new RuntimeException(
@@ -336,10 +339,10 @@ public class ProtoLogConfigurationServiceImpl extends IProtoLogConfigurationServ
                        + " and logcatStatuses has length " + logcatStatuses.length);
        }

        synchronized (mConfigLock) {
        final var clientRecord = mClientRecords.get(client.asBinder());
        if (clientRecord == null) {
                throw new RuntimeException("Client " + client + " is not registered");
            Log.wtf(LOG_TAG, "Trying to add groups to unregistered client: " + client);
            return;
        }

        for (int i = 0; i < groups.length; i++) {
@@ -358,7 +361,6 @@ public class ProtoLogConfigurationServiceImpl extends IProtoLogConfigurationServ
            }
        }
    }
    }

    private void toggleProtoLogToLogcat(
            @NonNull PrintWriter pw, boolean enabled, @NonNull String[] groups