From 273613653fa9bdfd088cd11b19fcbb8ba1846f1e Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Wed, 2 Oct 2019 16:01:51 -0700 Subject: [PATCH] zygote: respond and wait for reply to --boot-completed. The zygote handles requests by polling on its sockets, and then handling one request for each active socket. However, it does so by reading from a socket via a BufferedReader, which means that if multiple requests are written into the socket before the zygote gets a chance to read them, the zygote reads multiple requests into its BufferedReader, it handles one request, and then never responds to the request that's buffered, leaving its client stuck waiting for a response that will never happen. For most requests, this can't happen, because the client will wait for a response to be sent from the zygote before sending another request, but this isn't true for --boot-completed until this patch. Bug: http://b/141767463 Test: forrest runs of apct/text/text_native_test-cloud-tf Change-Id: I8b7a80abfd9443d98f8cf5aedb7669b82c0cb84a --- core/java/android/os/ZygoteProcess.java | 1 + core/java/com/android/internal/os/ZygoteConnection.java | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/core/java/android/os/ZygoteProcess.java b/core/java/android/os/ZygoteProcess.java index e923336da4546..3a55aff146595 100644 --- a/core/java/android/os/ZygoteProcess.java +++ b/core/java/android/os/ZygoteProcess.java @@ -760,6 +760,7 @@ public class ZygoteProcess { ZygoteState state = openZygoteSocketIfNeeded(abi); state.mZygoteOutputWriter.write("1\n--boot-completed\n"); state.mZygoteOutputWriter.flush(); + state.mZygoteInputStream.readInt(); } } catch (Exception ex) { throw new RuntimeException("Failed to inform zygote of boot_completed", ex); diff --git a/core/java/com/android/internal/os/ZygoteConnection.java b/core/java/com/android/internal/os/ZygoteConnection.java index c24ffb0c329b2..b15e1efa46c8a 100644 --- a/core/java/com/android/internal/os/ZygoteConnection.java +++ b/core/java/com/android/internal/os/ZygoteConnection.java @@ -305,6 +305,12 @@ class ZygoteConnection { } private void handleBootCompleted() { + try { + mSocketOutStream.writeInt(0); + } catch (IOException ioe) { + throw new IllegalStateException("Error writing to command socket", ioe); + } + VMRuntime.bootCompleted(); }