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

Commit cc16eef6 authored by rockyfang's avatar rockyfang
Browse files

Synchronized setClientProxy and callbackFinished to prevent NPE

During the gap between createClient and setClientProxy that sets up
mClientProxy, another thread callbackFinished on mClientProxy that will
causes NPE, this CL wait until mClientProxy is created then allow
callbackFinished to be called

Test: Added Thread.sleep(7000) to expand the gap between
mService.createClient and client.setClientProxy to expand the chance of creating race condition NPE then run PtsChreTestCases and passed (ab/I55000010077218265)
Fixes: 240691175
Change-Id: I8a729abf9f4368eab51c886a20035ccf879d9eff
parent 78c33bb4
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -81,7 +81,7 @@ public class ContextHubClient implements Closeable {
     *
     * @param clientProxy the proxy of the client at the service
     */
    /* package */ void setClientProxy(IContextHubClient clientProxy) {
    /* package */ synchronized void setClientProxy(IContextHubClient clientProxy) {
        Objects.requireNonNull(clientProxy, "IContextHubClient cannot be null");
        if (mClientProxy != null) {
            throw new IllegalStateException("Cannot change client proxy multiple times");
@@ -93,6 +93,7 @@ public class ContextHubClient implements Closeable {
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
        this.notifyAll();
    }

    /**
@@ -221,8 +222,15 @@ public class ContextHubClient implements Closeable {
    }

    /** @hide */
    public void callbackFinished() {
    public synchronized void callbackFinished() {
        try {
            while (mClientProxy == null) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
            mClientProxy.callbackFinished();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();