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

Commit 5084305d authored by Wilson Wu's avatar Wilson Wu
Browse files

Prevent UnsupportedOperationException in Completable

The CountDownLatch#await() will be unblocked if there is an
InterruptedException happened.

Then it will cause the UnsupportedOperationException when
calling Completable.ValueBase#enforceGetValueLocked(),

Add a retry mechanism if InterruptedException happened.

Bug: 176441559
Test: atest CtsInputMethodTestCases
Change-Id: I6b93e76f78b4b8c94fc4423c33a707207961816d
parent 92b38ba2
Loading
Loading
Loading
Loading
+20 −8
Original line number Diff line number Diff line
@@ -117,7 +117,7 @@ public final class Completable {
        }

        /**
         * @return {@link true} if {@link #onComplete()} gets called and {@link #mState} is
         * @return {@code true} if {@link #onComplete()} gets called and {@link #mState} is
         *         {@link CompletionState#COMPLETED_WITH_VALUE}.
         */
        @AnyThread
@@ -232,13 +232,25 @@ public final class Completable {
        }

        /**
         * Blocks the calling thread until this object becomes ready to return the value.
         * Blocks the calling thread until this object becomes ready to return the value, even if
         * {@link InterruptedException} is thrown.
         */
        @AnyThread
        public void await() {
            boolean interrupted = false;
            while (true) {
                try {
                    mLatch.await();
            } catch (InterruptedException ignored) { }
                    break;
                } catch (InterruptedException ignored) {
                    interrupted = true;
                }
            }

            if (interrupted) {
                // Try to preserve the interrupt bit on this thread.
                Thread.currentThread().interrupt();
            }
        }
    }

@@ -444,7 +456,7 @@ public final class Completable {
    /**
     * Await the result by the {@link Completable.Values}.
     *
     * @return the result once {@link ValueBase#onComplete()}
     * @return the result once {@link ValueBase#onComplete()}.
     */
    @AnyThread
    @Nullable
@@ -456,7 +468,7 @@ public final class Completable {
    /**
     * Await the int result by the {@link Completable.Int}.
     *
     * @return the result once {@link ValueBase#onComplete()}
     * @return the result once {@link ValueBase#onComplete()}.
     */
    @AnyThread
    public static int getIntResult(@NonNull Completable.Int value) {