From 1752c5f2490354cc6a6e682c1580a8dff07c5aa3 Mon Sep 17 00:00:00 2001 From: Akilesh Kailash Date: Mon, 18 Dec 2023 13:47:52 -0800 Subject: [PATCH] libsnapshot: Detach the daemon explicitly before stopping the service If the daemon is alive, detach it before explicitly terminating service. Bug: 316876960 Test: treehugger presubmit tests Change-Id: I94d9d1a0dab09a6b016f422c7497098abc86add8 Signed-off-by: Akilesh Kailash --- fs_mgr/libsnapshot/snapshot_test.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/fs_mgr/libsnapshot/snapshot_test.cpp b/fs_mgr/libsnapshot/snapshot_test.cpp index c0c3eafa3..e538d503d 100644 --- a/fs_mgr/libsnapshot/snapshot_test.cpp +++ b/fs_mgr/libsnapshot/snapshot_test.cpp @@ -2864,15 +2864,23 @@ void SnapshotTestEnvironment::TearDown() { } void KillSnapuserd() { - auto status = android::base::GetProperty("init.svc.snapuserd", "stopped"); - if (status == "stopped") { - return; + // Detach the daemon if it's alive + auto snapuserd_client = SnapuserdClient::TryConnect(kSnapuserdSocket, 5s); + if (snapuserd_client) { + snapuserd_client->DetachSnapuserd(); } - auto snapuserd_client = SnapuserdClient::Connect(kSnapuserdSocket, 5s); - if (!snapuserd_client) { - return; + + // Now stop the service - Init will send a SIGKILL to the daemon. However, + // process state will move from "running" to "stopping". Only after the + // process is reaped by init, the service state is moved to "stopped". + // + // Since the tests involve starting the daemon immediately, wait for the + // process to completely stop (aka. wait until init reaps the terminated + // process). + android::base::SetProperty("ctl.stop", "snapuserd"); + if (!android::base::WaitForProperty("init.svc.snapuserd", "stopped", 10s)) { + LOG(ERROR) << "Timed out waiting for snapuserd to stop."; } - snapuserd_client->DetachSnapuserd(); } } // namespace snapshot