Skip to contentSkip to conversation

lib: shell: Fix timeout process races

RFC PATCH Superseded Cyril Hrubis 4 participants 7 messages Sep 17, 2021 – Sep 21, 2021

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 lib: shell: Fix timeout process races

There were actually several races in the shell library timeout handling.

This commit fixes hopefully all of them by:

* Reimplementing the backgroud timer in C

* Making sure that the timer has started before we leave test setup

The rewrite of the backround timer to C allows us to run all the timeout logic in a single process, which simplifies the whole problem greatly since previously we had chain of processes that estabilished signal handlers to kill it's descendants, which in the end had a few races in it.

The race that caused the problems is, as far as I can tell, in the way how shell spawns it's children. I haven't checked the shell code, but I guess that when shell runs a process in bacground it does vfork() + exec() and because signals are ignored during the operation. If the SIGTERM arrives at that point it gets lost.

That means that we created a race window in the shell library each time we started a new process. The rewrite to C simplifies the code but we still end up with a single place where this can happen and that is when we execute the tst_timeout_kill binary. This is now fixed in the shell library by waiting until the background process gets to a sleep state, which means that the proces has been executed and waiting for the timeout.

After these fixes I haven't been able to reproduce the hang with:

cat > debug.sh <<EOF
#!/bin/sh

TST_SETUP="setup"
TST_TESTFUNC="do_test"
. tst_test.sh

setup()
{
tst_brk TCONF "quit now!"
}

do_test()
{
tst_res TPASS "pass :)"
}

tst_run
EOF

# while true; do ./debug.sh; done
---
 testcases/lib/.gitignore         |  1 +
 testcases/lib/Makefile           |  2 +-
 testcases/lib/tst_test.sh        | 47 +++++-----------
 testcases/lib/tst_timeout_kill.c | 93 ++++++++++++++++++++++++++++++++
 4 files changed, 108 insertions(+), 35 deletions(-)
 create mode 100644 testcases/lib/tst_timeout_kill.c
testcases/lib/.gitignore +1 -0
index 5a0e8cba2..f266a3e6b 100644
@@ -14,3 +14,4 @@
 /tst_sleep
 /tst_supported_fs
 /tst_hexdump
+tst_timeout_kill
testcases/lib/Makefile +1 -1
index 179b47479..d6b4c7a91 100644
@@ -11,6 +11,6 @@ INSTALL_TARGETS		:= *.sh
 MAKE_TARGETS		:= tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\
 			   tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars\
 			   tst_getconf tst_supported_fs tst_check_drivers tst_get_unused_port\
-			   tst_get_median tst_hexdump tst_get_free_pids
+			   tst_get_median tst_hexdump tst_get_free_pids tst_timeout_kill
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/lib/tst_test.sh +13 -34

67 lines not on this page · raw message

testcases/lib/tst_timeout_kill.c new file +93 -0

93 lines not on this page · raw message

Signature · 5 lines
2.32.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi

Sorry for my late reply on this topic, I was on vacation.

Quoted 5 lines · Cyril Hrubis

There were actually several races in the shell library timeout handling.

This commit fixes hopefully all of them by:

* Reimplementing the backgroud timer in C

I did that once, but at that point it was kinda rejected ;) See https://lists.linux.it/pipermail/ltp/2021-May/022445.html
and https://lists.linux.it/pipermail/ltp/2021-May/022453.html

Quoted 132 lines

* Making sure that the timer has started before we leave test setup

The rewrite of the backround timer to C allows us to run all the timeout logic in a single process, which simplifies the whole problem greatly since previously we had chain of processes that estabilished signal handlers to kill it's descendants, which in the end had a few races in it.

The race that caused the problems is, as far as I can tell, in the way how shell spawns it's children. I haven't checked the shell code, but I guess that when shell runs a process in bacground it does vfork() + exec() and because signals are ignored during the operation. If the SIGTERM arrives at that point it gets lost.

That means that we created a race window in the shell library each time we started a new process. The rewrite to C simplifies the code but we still end up with a single place where this can happen and that is when we execute the tst_timeout_kill binary. This is now fixed in the shell library by waiting until the background process gets to a sleep state, which means that the proces has been executed and waiting for the timeout.

After these fixes I haven't been able to reproduce the hang with:

cat > debug.sh <<EOF
#!/bin/sh

TST_SETUP="setup"
TST_TESTFUNC="do_test"
. tst_test.sh

setup()
{
tst_brk TCONF "quit now!"
}

do_test()
{
tst_res TPASS "pass :)"
}

tst_run
EOF

# while true; do ./debug.sh; done
---
  testcases/lib/.gitignore         |  1 +
  testcases/lib/Makefile           |  2 +-
  testcases/lib/tst_test.sh        | 47 +++++-----------
  testcases/lib/tst_timeout_kill.c | 93 ++++++++++++++++++++++++++++++++
  4 files changed, 108 insertions(+), 35 deletions(-)

create mode 100644 testcases/lib/tst_timeout_kill.c

testcases/lib/.gitignore +1 -0
index 5a0e8cba2..f266a3e6b 100644
@@ -14,3 +14,4 @@
  /tst_sleep
  /tst_supported_fs
  /tst_hexdump
+tst_timeout_kill
testcases/lib/Makefile +1 -1
index 179b47479..d6b4c7a91 100644
@@ -11,6 +11,6 @@ INSTALL_TARGETS		:= *.sh
  MAKE_TARGETS		:= tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\
  			   tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars\
  			   tst_getconf tst_supported_fs tst_check_drivers tst_get_unused_port\
-			   tst_get_median tst_hexdump tst_get_free_pids
+			   tst_get_median tst_hexdump tst_get_free_pids tst_timeout_kill
 
  include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/lib/tst_test.sh +0 -33
index 8f69b0551..2556b28f5 100644
@@ -474,27 +474,6 @@ _tst_multiply_timeout()
  	return 0
  }
 
-_tst_kill_test()
-{
-	local i=10
-
-	trap '' TERM
-	tst_res TBROK "Test timed out, sending SIGTERM! If you are running on slow machine, try exporting LTP_TIMEOUT_MUL > 1"
-	kill -TERM -$pid
-	tst_sleep 100ms
-
-	while kill -0 $pid >/dev/null 2>&1 && [ $i -gt 0 ]; do
-		tst_res TINFO "Test is still running, waiting ${i}s"
-		sleep 1
-		i=$((i-1))
-	done
-
-	if kill -0 $pid >/dev/null 2>&1; then
-		tst_res TBROK "Test still running, sending SIGKILL"
-		kill -KILL -$pid
-	fi
-}
-
  _tst_cleanup_timer()
  {
  	if [ -n "$_tst_setup_timer_pid" ]; then
@@ -503,18 +482,6 @@ _tst_cleanup_timer()
  	fi
  }
 
-_tst_timeout_process()
-{
-	local sleep_pid
-
-	sleep $sec &
-	sleep_pid=$!
-	trap "kill $sleep_pid; exit" TERM
-	wait $sleep_pid
-	trap - TERM
-	_tst_kill_test
-}
-
  _tst_setup_timer()
  {
  	TST_TIMEOUT=${TST_TIMEOUT:-300}
@@ -539,9 +506,21 @@ _tst_setup_timer()
 
  	_tst_cleanup_timer

testcases/lib/tst_test.sh _tst_setup_timer() · line 516

Quoted 10 lines
testcases/lib/tst_test.sh +6 -1
 
-	_tst_timeout_process &
+	tst_timeout_kill $sec $pid &
 
  	_tst_setup_timer_pid=$!
+
+	while true; do
+		local state
+
+		state=$(cut -d' ' -f3 "/proc/$_tst_setup_timer_pid/stat")

Hmm maybe we could use the checkpoint api here instead. Wouldn't this be more portable?

Quoted 43 lines
testcases/lib/tst_test.sh +7 -0
+
+		if [ "$state" = "S" ]; then
+			break;
+		fi
+
+		tst_sleep 1ms
+	done
  }
 
  tst_require_root()
testcases/lib/tst_timeout_kill.c new file +27 -0
new file mode 100644
index 000000000..6e97514f1
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 Cyril Hrubis <[email protected]>
+ */
+
+#include <stdio.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <tst_res_flags.h>
+#include <tst_ansi_color.h>
+
+static void print_help(const char *name)
+{
+	fprintf(stderr, "usage: %s timeout pid\n", name);
+}
+
+static void print_msg(int type, const char *msg)
+{
+	const char *strtype = "";
+
+	switch (type) {
+	case TBROK:
+		strtype = "TBROK";
+	break;
+	case TINFO:
+		strtype = "TINFO";

testcases/lib/tst_timeout_kill.c line 37

Quoted 10 lines
testcases/lib/tst_timeout_kill.c new file +10 -0
+	break;
+	}
+
+	if (tst_color_enabled(STDERR_FILENO)) {
+		fprintf(stderr, "%s%s: %s%s\n", tst_ttype2color(type),
+			strtype, ANSI_COLOR_RESET, msg);
+	} else {
+		fprintf(stderr, "%s: %s\n", strtype, msg);
+	}
+}

Shouldn't this be reused from the library instead of being reimplemented?

Quoted 31 lines
testcases/lib/tst_timeout_kill.c new file +31 -0
+
+int main(int argc, char *argv[])
+{
+	int timeout, pid, ret, i;
+
+	if (argc != 3) {
+		print_help(argv[0]);
+		return 1;
+	}
+
+	timeout = atoi(argv[1]);
+	pid = atoi(argv[2]);
+
+	if (timeout < 0) {
+		fprintf(stderr, "Invalid timeout '%s'", argv[1]);
+		print_help(argv[0]);
+		return 1;
+	}
+
+	if (pid <= 1) {
+		fprintf(stderr, "Invalid pid '%s'", argv[2]);
+		print_help(argv[0]);
+		return 1;
+	}
+
+	if (timeout)
+		sleep(timeout);
+
+	print_msg(TBROK, "Test timed out, sending SIGTERM! If you are running on slow machine, try exporting LTP_TIMEOUT_MUL > 1");
+
+	ret = kill(-pid, SIGTERM);

testcases/lib/tst_timeout_kill.c line 78

Quoted 10 lines
testcases/lib/tst_timeout_kill.c new file +10 -0
+	if (ret) {
+		print_msg(TBROK, "kill() failed");
+		return 1;
+	}
+
+	usleep(100000);
+
+	i = 10;
+
+	while (!kill(-pid, 0) && i-- > 0) {

This was kill(pid, 0) in the original shell code. I am not sure if it is important

testcases/lib/tst_timeout_kill.c line 83

Quoted 5 lines
testcases/lib/tst_timeout_kill.c new file +5 -0
+		print_msg(TINFO, "Test is still running...");
+		sleep(1);
+	}
+
+	if (!kill(-pid, 0)) {

Here as well

testcases/lib/tst_timeout_kill.c line 93

Quoted 10 lines
testcases/lib/tst_timeout_kill.c new file +10 -0
+		print_msg(TBROK, "Test is still running, sending SIGKILL");
+		ret = kill(-pid, SIGKILL);
+		if (ret) {
+			print_msg(TBROK, "kill() failed");
+			return 1;
+		}
+	}
+
+	return 0;
+}

Joerg

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Hi!

Quoted 8 lines
Quoted 5 lines

There were actually several races in the shell library timeout handling.

This commit fixes hopefully all of them by:

* Reimplementing the backgroud timer in C

I did that once, but at that point it was kinda rejected ;) See https://lists.linux.it/pipermail/ltp/2021-May/022445.html
and https://lists.linux.it/pipermail/ltp/2021-May/022453.html

I guess we found out the hard way that it's impossible to write raceless timeouts in shell.

Quoted 10 lines

testcases/lib/tst_test.sh _tst_setup_timer() · line 516

Quoted 8 lines
testcases/lib/tst_test.sh +6 -0
+	tst_timeout_kill $sec $pid &
 
  	_tst_setup_timer_pid=$!
+
+	while true; do
+		local state
+
+		state=$(cut -d' ' -f3 "/proc/$_tst_setup_timer_pid/stat")

Hmm maybe we could use the checkpoint api here instead. Wouldn't this be more portable?

Well there are other tests in LTP that depend on the /proc/$PID/stat already and I do not remmeber any bugreports about it being broken or not available. Linux generally does not work without /proc/ being mounted anyways.

I wanted to avoid checkpoints because that requires shared memory and it's more complex to setup and if we add it here it would be set up by every shell process, but maybe that's not that bad. Maybe we should set up the piece of shared memory as we do in C and use it for the result counter as well. But anyways that is orthogonal from the fixes here and could be done later on.

Quoted 46 lines
Quoted 46 lines
testcases/lib/tst_test.sh +7 -0
+
+		if [ "$state" = "S" ]; then
+			break;
+		fi
+
+		tst_sleep 1ms
+	done
  }
 
  tst_require_root()
testcases/lib/tst_timeout_kill.c new file +30 -0
new file mode 100644
index 000000000..6e97514f1
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 Cyril Hrubis <[email protected]>
+ */
+
+#include <stdio.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <tst_res_flags.h>
+#include <tst_ansi_color.h>
+
+static void print_help(const char *name)
+{
+	fprintf(stderr, "usage: %s timeout pid\n", name);
+}
+
+static void print_msg(int type, const char *msg)
+{
+	const char *strtype = "";
+
+	switch (type) {
+	case TBROK:
+		strtype = "TBROK";
+	break;
+	case TINFO:
+		strtype = "TINFO";
+	break;
+	}
+
Quoted 8 lines

testcases/lib/tst_timeout_kill.c line 37

Quoted 7 lines
testcases/lib/tst_timeout_kill.c new file +7 -0
+	if (tst_color_enabled(STDERR_FILENO)) {
+		fprintf(stderr, "%s%s: %s%s\n", tst_ttype2color(type),
+			strtype, ANSI_COLOR_RESET, msg);
+	} else {
+		fprintf(stderr, "%s: %s\n", strtype, msg);
+	}
+}

Shouldn't this be reused from the library instead of being reimplemented?

I wanted to avoid calling the tst_res() functions to keep things simple, but I guess that there is no reason not to use it.

Quoted 35 lines
Quoted 35 lines
testcases/lib/tst_timeout_kill.c new file +35 -0
+
+int main(int argc, char *argv[])
+{
+	int timeout, pid, ret, i;
+
+	if (argc != 3) {
+		print_help(argv[0]);
+		return 1;
+	}
+
+	timeout = atoi(argv[1]);
+	pid = atoi(argv[2]);
+
+	if (timeout < 0) {
+		fprintf(stderr, "Invalid timeout '%s'", argv[1]);
+		print_help(argv[0]);
+		return 1;
+	}
+
+	if (pid <= 1) {
+		fprintf(stderr, "Invalid pid '%s'", argv[2]);
+		print_help(argv[0]);
+		return 1;
+	}
+
+	if (timeout)
+		sleep(timeout);
+
+	print_msg(TBROK, "Test timed out, sending SIGTERM! If you are running on slow machine, try exporting LTP_TIMEOUT_MUL > 1");
+
+	ret = kill(-pid, SIGTERM);
+	if (ret) {
+		print_msg(TBROK, "kill() failed");
+		return 1;
+	}
Quoted 8 lines

testcases/lib/tst_timeout_kill.c line 78

Quoted 6 lines
testcases/lib/tst_timeout_kill.c new file +6 -0
+
+	usleep(100000);
+
+	i = 10;
+
+	while (!kill(-pid, 0) && i-- > 0) {

This was kill(pid, 0) in the original shell code. I am not sure if it is important

I think that this is better this way since this should loop until there is at least one process alive in the process group.

Quoted 18 lines

testcases/lib/tst_timeout_kill.c line 83

Quoted 5 lines
testcases/lib/tst_timeout_kill.c new file +5 -0
+		print_msg(TINFO, "Test is still running...");
+		sleep(1);
+	}
+
+	if (!kill(-pid, 0)) {

Here as well

testcases/lib/tst_timeout_kill.c line 93

Quoted 10 lines
testcases/lib/tst_timeout_kill.c new file +10 -0
+		print_msg(TBROK, "Test is still running, sending SIGKILL");
+		ret = kill(-pid, SIGKILL);
+		if (ret) {
+			print_msg(TBROK, "kill() failed");
+			return 1;
+		}
+	}
+
+	return 0;
+}

Joerg

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Hi!

Quoted 4 lines
Quoted 1 line

Shouldn't this be reused from the library instead of being reimplemented?

I wanted to avoid calling the tst_res() functions to keep things simple, but I guess that there is no reason not to use it.

And there actuall was a reason, since we cannot do tst_res(TBROK, "..."); in the C library.

And looking at the C library, we avoid using the tst_res() on timeouts as well, actually we just write to stderr because we do that from a signal handler context. So I guess that we can just print to stderr here instead.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
In reply to Cyril Hrubis

Hi Joerg,

Quoted 9 lines · Cyril Hrubis

Hi!

Quoted 1 line
Quoted 1 line

There were actually several races in the shell library timeout

handling.

Quoted 5 lines
Quoted 4 lines

This commit fixes hopefully all of them by:

* Reimplementing the backgroud timer in C

I did that once, but at that point it was kinda rejected ;)

Sorry, I was suggesting to solve the problem only in Shell ATM, and it proved that it's not easy to cover all.

Quoted 5 lines
Quoted 2 lines

See https://lists.linux.it/pipermail/ltp/2021-May/022445.html
and https://lists.linux.it/pipermail/ltp/2021-May/022453.html

I guess we found out the hard way that it's impossible to write raceless timeouts in shell.

+1 Indeed.

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by Tested-by on v1 1/1
#5 lore raw
In reply to Cyril Hrubis

Hi Cyril,

Quoted 5 lines

There were actually several races in the shell library timeout handling.

This commit fixes hopefully all of them by:

* Reimplementing the backgroud timer in C

+1

Quoted 1 line

* Making sure that the timer has started before we leave test setup

+1

Quoted 39 lines

The rewrite of the backround timer to C allows us to run all the timeout logic in a single process, which simplifies the whole problem greatly since previously we had chain of processes that estabilished signal handlers to kill it's descendants, which in the end had a few races in it.

The race that caused the problems is, as far as I can tell, in the way how shell spawns it's children. I haven't checked the shell code, but I guess that when shell runs a process in bacground it does vfork() + exec() and because signals are ignored during the operation. If the SIGTERM arrives at that point it gets lost.

That means that we created a race window in the shell library each time we started a new process. The rewrite to C simplifies the code but we still end up with a single place where this can happen and that is when we execute the tst_timeout_kill binary. This is now fixed in the shell library by waiting until the background process gets to a sleep state, which means that the proces has been executed and waiting for the timeout.

After these fixes I haven't been able to reproduce the hang with:

cat > debug.sh <<EOF
#!/bin/sh

TST_SETUP="setup"
TST_TESTFUNC="do_test"
. tst_test.sh

setup()
{
tst_brk TCONF "quit now!"
}

do_test()
{
tst_res TPASS "pass :)"
}

Quoted 4 lines

tst_run
EOF

# while true; do ./debug.sh; done

I verified it's ok on both VM which were previously affected.

After release I might write a test for tst_timeout_kill.c. Thanks for fixing it!

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw
In reply to Cyril Hrubis

Hi Cyril,

...

testcases/lib/.gitignore line 17

Quoted 6 lines

+++ b/testcases/lib/.gitignore

testcases/lib/.gitignore +1 -0
@@ -14,3 +14,4 @@
 /tst_sleep
 /tst_supported_fs
 /tst_hexdump
+tst_timeout_kill

nit: /tst_timeout_kill

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp