Merge "binder_rpc_fuzzer: avoid SIGPIPE"
diff --git a/libs/binder/RpcServer.cpp b/libs/binder/RpcServer.cpp
index d8ba2c6..77cae83 100644
--- a/libs/binder/RpcServer.cpp
+++ b/libs/binder/RpcServer.cpp
@@ -329,7 +329,7 @@
     return true;
 }
 
-void RpcServer::onSessionTerminating(const sp<RpcSession>& session) {
+void RpcServer::onSessionLockedAllServerThreadsEnded(const sp<RpcSession>& session) {
     auto id = session->mId;
     LOG_ALWAYS_FATAL_IF(id == std::nullopt, "Server sessions must be initialized with ID");
     LOG_RPC_DETAIL("Dropping session %d", *id);
@@ -341,7 +341,7 @@
     (void)mSessions.erase(it);
 }
 
-void RpcServer::onSessionThreadEnding(const sp<RpcSession>& session) {
+void RpcServer::onSessionServerThreadEnded(const sp<RpcSession>& session) {
     (void)session;
     mShutdownCv.notify_all();
 }
diff --git a/libs/binder/RpcSession.cpp b/libs/binder/RpcSession.cpp
index 156a834..ccf7f89 100644
--- a/libs/binder/RpcSession.cpp
+++ b/libs/binder/RpcSession.cpp
@@ -219,23 +219,7 @@
     }
 
     if (server != nullptr) {
-        server->onSessionThreadEnding(sp<RpcSession>::fromExisting(this));
-    }
-}
-
-void RpcSession::terminateLocked() {
-    // TODO(b/185167543):
-    // - kindly notify other side of the connection of termination (can't be
-    // locked)
-    // - prevent new client/servers from being added
-    // - stop all threads which are currently reading/writing
-    // - terminate RpcState?
-
-    if (mTerminated) return;
-
-    sp<RpcServer> server = mForServer.promote();
-    if (server) {
-        server->onSessionTerminating(sp<RpcSession>::fromExisting(this));
+        server->onSessionServerThreadEnded(sp<RpcSession>::fromExisting(this));
     }
 }
 
@@ -359,7 +343,10 @@
         it != mServerConnections.end()) {
         mServerConnections.erase(it);
         if (mServerConnections.size() == 0) {
-            terminateLocked();
+            sp<RpcServer> server = mForServer.promote();
+            if (server) {
+                server->onSessionLockedAllServerThreadsEnded(sp<RpcSession>::fromExisting(this));
+            }
         }
         return true;
     }
diff --git a/libs/binder/include/binder/RpcServer.h b/libs/binder/include/binder/RpcServer.h
index a08c401..8ad5821 100644
--- a/libs/binder/include/binder/RpcServer.h
+++ b/libs/binder/include/binder/RpcServer.h
@@ -153,8 +153,8 @@
 
     // internal use only
 
-    void onSessionTerminating(const sp<RpcSession>& session);
-    void onSessionThreadEnding(const sp<RpcSession>& session);
+    void onSessionLockedAllServerThreadsEnded(const sp<RpcSession>& session);
+    void onSessionServerThreadEnded(const sp<RpcSession>& session);
 
 private:
     friend sp<RpcServer>;
diff --git a/libs/binder/include/binder/RpcSession.h b/libs/binder/include/binder/RpcSession.h
index 4401aaf..eadf0f8 100644
--- a/libs/binder/include/binder/RpcSession.h
+++ b/libs/binder/include/binder/RpcSession.h
@@ -144,7 +144,6 @@
     void preJoin(std::thread thread);
     // join on thread passed to preJoin
     void join(base::unique_fd client);
-    void terminateLocked();
 
     struct RpcConnection : public RefBase {
         base::unique_fd fd;
@@ -227,7 +226,6 @@
     // TODO(b/185167543): allow sharing between different sessions in a
     // process? (or combine with mServerConnections)
     std::map<std::thread::id, std::thread> mThreads;
-    bool mTerminated = false;
 };
 
 } // namespace android