Skip to contentSkip to conversation

test-runner: use WNOHANG option waiting for PID

PATCH Applied James Prestwood 2 participants 2 messages Jun 22, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 test-runner: use WNOHANG option waiting for PID

If an application has a bug and hangs on SIGTERM this causes test-runner to hang as well. This is obviously an issue with the application in question, but test-runner should have a way of continuing onto the next test rather than hanging.

Instead we can use WNOHANG and a sleep to allow applications some amount of time to exit, and if they haven't use SIGKILL instead as well as print an error. Similar to how wait_for_socket works. The timeout is hard coded to 2 seconds (100ms sleep + 20 iterations).

---
 tools/test-runner.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

v2:
- Removed extra check for i >= 20 since that is always going to be true if we get past the while loop
- Added another waitpid after SIGKILL

tools/test-runner.c +12 -2
index f8677be5..83d5194d 100644
@@ -626,14 +626,24 @@ exit:
 static void kill_process(pid_t pid)
 {
 	int status;
+	int i = 0;
 
 	l_debug("Terminate pid: %d", pid);
 
 	kill(pid, SIGTERM);
 
 	do {
-		waitpid(pid, &status, 0);
-	} while (!WIFEXITED(status) && !WIFSIGNALED(status));
+		if (waitpid(pid, &status, WNOHANG) == pid)
+			return;
+
+		usleep(100000);
+	} while (!WIFEXITED(status) && !WIFSIGNALED(status) && i++ < 20);
+
+	l_error("Failed to kill process %d gracefully", pid);
+	kill(pid, SIGKILL);
+
+	/* SIGKILL shouldn't need WNOHANG */
+	waitpid(pid, &status, 0);
 }
 
 static bool wait_for_socket(const char *socket, useconds_t wait_time)
Signature · 1 line
2.21.1
#1 lore raw

Hi James,

Quoted 10 lines · James Prestwood

If an application has a bug and hangs on SIGTERM this causes test-runner to hang as well. This is obviously an issue with the application in question, but test-runner should have a way of continuing onto the next test rather than hanging.

Instead we can use WNOHANG and a sleep to allow applications some amount of time to exit, and if they haven't use SIGKILL instead as well as print an error. Similar to how wait_for_socket works. The timeout is hard coded to 2 seconds (100ms sleep + 20 iterations).

Quoted 9 lines
---
  tools/test-runner.c | 14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)

v2:
- Removed extra check for i >= 20 since that is always going to be true if we get past the while loop
- Added another waitpid after SIGKILL

Applied, thanks.

Regards,
-Denis