From 503dae288339d14f27b7f38b8466c206054825ac Mon Sep 17 00:00:00 2001 From: Jarvis Huang Date: Wed, 28 Dec 2022 14:23:24 +0800 Subject: [PATCH] ipc/android: Early reject if server failed to start --- .../java/org/freedesktop/monado/ipc/MonadoImpl.java | 8 +++++--- src/xrt/ipc/server/ipc_server_process.c | 1 + src/xrt/targets/service-lib/service_target.cpp | 10 +++++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/xrt/ipc/android/src/main/java/org/freedesktop/monado/ipc/MonadoImpl.java b/src/xrt/ipc/android/src/main/java/org/freedesktop/monado/ipc/MonadoImpl.java index 7bb8310d0..cbb855ec9 100644 --- a/src/xrt/ipc/android/src/main/java/org/freedesktop/monado/ipc/MonadoImpl.java +++ b/src/xrt/ipc/android/src/main/java/org/freedesktop/monado/ipc/MonadoImpl.java @@ -11,6 +11,7 @@ package org.freedesktop.monado.ipc; import android.os.ParcelFileDescriptor; +import android.os.RemoteException; import android.util.Log; import android.view.Surface; import android.view.SurfaceHolder; @@ -18,8 +19,6 @@ import android.view.SurfaceHolder; import androidx.annotation.Keep; import androidx.annotation.NonNull; -import org.jetbrains.annotations.NotNull; - import java.io.IOException; /** @@ -64,7 +63,7 @@ public class MonadoImpl extends IMonado.Stub { } @Override - public void connect(@NotNull ParcelFileDescriptor parcelFileDescriptor) { + public void connect(@NonNull ParcelFileDescriptor parcelFileDescriptor) throws RemoteException { nativeStartServer(); int fd = parcelFileDescriptor.getFd(); Log.i(TAG, "connect: given fd " + fd); @@ -75,6 +74,9 @@ public class MonadoImpl extends IMonado.Stub { } catch (IOException e) { // do nothing, probably already closed. } + + // throw an exception so that client can gracefully fail + throw new IllegalStateException("server not available"); } else { Log.i(TAG, "connect: fd ownership transferred"); parcelFileDescriptor.detachFd(); diff --git a/src/xrt/ipc/server/ipc_server_process.c b/src/xrt/ipc/server/ipc_server_process.c index b69b67ea2..ce031ce91 100644 --- a/src/xrt/ipc/server/ipc_server_process.c +++ b/src/xrt/ipc/server/ipc_server_process.c @@ -832,6 +832,7 @@ ipc_server_main_android(struct ipc_server **ps, void (*startup_complete_callback int ret = init_all(s); if (ret < 0) { free(s); + startup_complete_callback(data); return ret; } diff --git a/src/xrt/targets/service-lib/service_target.cpp b/src/xrt/targets/service-lib/service_target.cpp index f525cf546..8cd41aec8 100644 --- a/src/xrt/targets/service-lib/service_target.cpp +++ b/src/xrt/targets/service-lib/service_target.cpp @@ -100,12 +100,16 @@ private: waitForStartupComplete() { std::unique_lock lock{server_mutex}; - bool completed = startup_cond.wait_for(lock, START_TIMEOUT_SECONDS, - [&]() { return server != NULL && startup_complete; }); + bool completed = startup_cond.wait_for(lock, START_TIMEOUT_SECONDS, [&]() { return startup_complete; }); + + if (!server) { + U_LOG_E("Failed to create ipc server"); + } + if (!completed) { U_LOG_E("Server startup timeout!"); } - return completed; + return server && completed; } //! Reference to the ipc_server, managed by ipc_server_process