Skip to contentSkip to conversation

testsuite/smokey/gdb: Fix race-condition

PATCH Superseded Fabian Scheler 3 participants 7 messages Mar 12, 2024 – Mar 20, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 testsuite/smokey/gdb: Fix race-condition

When quitting gdb by sending the quit-command, gdb may actually finish before the father-process calls pause. In that case the father-process would wait endlessly.

The fix now uses waitpid instead of pause to explicitly wait for the child-process, in contrast to pause waitpid also handles child-processes that already have been terminated.

---
 testsuite/smokey/gdb/gdb.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
testsuite/smokey/gdb/gdb.c +15 -9
index b09b78b4e..5e9e9d101 100644
@@ -77,8 +77,7 @@ static void handle_sigchld(int signum)
 	wait(&status);
 	if (WEXITSTATUS(status) == ESRCH)
 		smokey_note("gdb: skipped (gdb not available)");
-	else
-		check("gdb execution", WEXITSTATUS(status), 0);
+
 	child_terminated = 1;
 	close(pipe_out[0]);
 }
@@ -202,6 +201,8 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 	pthread_attr_t attr;
 	char run_param[32];
 	cpu_set_t cpu_set;
+	pid_t childpid;
+	int status;
 	int err;
 
 	smokey_parse_args(t, argc, argv);
@@ -257,7 +258,8 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 		err = pipe(pipe_out);
 		check_no_error("pipe_out", err);
 
-		switch (do_fork()) {
+		childpid = do_fork();
+		switch (childpid) {
 		case -1:
 			error(1, errno, "fork/vfork");
 
@@ -283,8 +285,14 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 
 		default:
 			wait_for_pattern("(gdb) ");
+
+			/* the signal handler only is used to skip the test if
+			   gdb is missing */
 			if (child_terminated)
 				break;
+                        else
+				signal(SIGCHLD, SIG_DFL);
+
 			send_command("b breakpoint_target\n", 1);
 			send_command("r\n", 1);
 
@@ -301,13 +309,11 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 			eval_expression("thread_loops==expected_loops", "1");
 
 			send_command("q\n", 0);
-			pause();
 
-			if (!child_terminated) {
-				fprintf(stderr,
-					"FAILURE: gdb still running?!\n");
-				exit(1);
-			}
+			err = waitpid(childpid, &status, 0);
+			check("waitpid", err, childpid);
+			check("gdb execution", WEXITSTATUS(status), 0);
+			close(pipe_out[0]);                        
 		}
 
 		signal(SIGCHLD, SIG_DFL);
Signature · 1 line
2.44.0
#1 lore raw

Hi Fabian,

testsuite/smokey/gdb/gdb.c static int run_gdb(struct smokey_test *t, int argc, char *const argv[]) · line 293

Quoted 10 lines · Fabian Scheler
testsuite/smokey/gdb/gdb.c +4 -0
@@ -283,8 +285,14 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 
 		default:
 			wait_for_pattern("(gdb) ");
+
+			/* the signal handler only is used to skip the test if
+			   gdb is missing */
 			if (child_terminated)
 				break;
+                        else

Formatting: ^^^^^^^ Spaces vs tabs.

testsuite/smokey/gdb/gdb.c static int run_gdb(struct smokey_test *t, int argc, char *const argv[]) · line 297

Quoted 4 lines
testsuite/smokey/gdb/gdb.c +2 -0
+				signal(SIGCHLD, SIG_DFL);
+
 			send_command("b breakpoint_target\n", 1);
 			send_command("r\n", 1);

Best regards,
Florian

#2 lore raw

Hi Florian,

Am 12.03.2024 um 08:48 schrieb Florian Bezdeka:

Quoted 6 lines

Hi Fabian,

Quoted 2 lines · Fabian Scheler
(quoted diff) +0 -0
@@ -283,8 +285,14 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 
Quoted 10 lines

testsuite/smokey/gdb/gdb.c static int run_gdb(struct smokey_test *t, int argc, char *const argv[]) · line 293

Quoted 8 lines
testsuite/smokey/gdb/gdb.c +4 -0
  		default:
  			wait_for_pattern("(gdb) ");
+
+			/* the signal handler only is used to skip the test if
+			   gdb is missing */
  			if (child_terminated)
  				break;
+                        else

Formatting: ^^^^^^^ Spaces vs tabs.

thanks!

Ciao
Fabian

Quoted 8 lines

testsuite/smokey/gdb/gdb.c static int run_gdb(struct smokey_test *t, int argc, char *const argv[]) · line 297

Quoted 5 lines
testsuite/smokey/gdb/gdb.c +2 -0
+				signal(SIGCHLD, SIG_DFL);
+
  			send_command("b breakpoint_target\n", 1);
  			send_command("r\n", 1);

Best regards,
Florian

Signature · 13 lines
With best regards,
Dr. Fabian Scheler

Siemens AG
T CED EDC-DE
Hertha-Sponer-Weg 3
91058 Erlangen, Germany
Phone: +49 (1522) 1702973
Mobile: +49 (1522) 1702973
mailto:[email protected]
www.siemens.com

Siemens Aktiengesellschaft: Chairman of the Supervisory Board: Jim Hagemann Snabe; Managing Board: Roland Busch, Chairman, President and Chief Executive Officer; Cedrik Neike, Matthias Rebellius, Ralf P. Thomas, Judith Wiese; Registered offices: Berlin and Munich, Germany; Commercial registries: Berlin-Charlottenburg, HRB 12300, Munich, HRB 6684; WEEE-Reg.-No. DE 23691322
#3 lore raw
In reply to Fabian Scheler
Quoted 18 lines · Fabian Scheler

When quitting gdb by sending the quit-command, gdb may actually finish before the father-process calls pause. In that case the father-process would wait endlessly.

The fix now uses waitpid instead of pause to explicitly wait for the child-process, in contrast to pause waitpid also handles child-processes that already have been terminated.

---
 testsuite/smokey/gdb/gdb.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
testsuite/smokey/gdb/gdb.c +0 -0
index b09b78b4e..5e9e9d101 100644

testsuite/smokey/gdb/gdb.c static void handle_sigchld(int signum) · line 81

Quoted 5 lines
testsuite/smokey/gdb/gdb.c +0 -2
@@ -77,8 +77,7 @@ static void handle_sigchld(int signum)
 	wait(&status);
 	if (WEXITSTATUS(status) == ESRCH)
 		smokey_note("gdb: skipped (gdb not available)");
-	else
-		check("gdb execution", WEXITSTATUS(status), 0);

Why do we have to remove this?

Quoted 24 lines
testsuite/smokey/gdb/gdb.c +5 -1
+
 	child_terminated = 1;
 	close(pipe_out[0]);
 }
@@ -202,6 +201,8 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 	pthread_attr_t attr;
 	char run_param[32];
 	cpu_set_t cpu_set;
+	pid_t childpid;
+	int status;
 	int err;
 
 	smokey_parse_args(t, argc, argv);
@@ -257,7 +258,8 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 		err = pipe(pipe_out);
 		check_no_error("pipe_out", err);
 
-		switch (do_fork()) {
+		childpid = do_fork();
+		switch (childpid) {
 		case -1:
 			error(1, errno, "fork/vfork");
 

testsuite/smokey/gdb/gdb.c static int run_gdb(struct smokey_test *t, int argc, char *const argv[]) · line 293

Quoted 9 lines
testsuite/smokey/gdb/gdb.c +4 -0
@@ -283,8 +285,14 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 
 		default:
 			wait_for_pattern("(gdb) ");
+
+			/* the signal handler only is used to skip the test if
+			   gdb is missing */
 			if (child_terminated)
 				break;
+                        else

Indention via spaces here.

Quoted 13 lines
testsuite/smokey/gdb/gdb.c +2 -3
+				signal(SIGCHLD, SIG_DFL);
+
 			send_command("b breakpoint_target\n", 1);
 			send_command("r\n", 1);
 
@@ -301,13 +309,11 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 			eval_expression("thread_loops==expected_loops", "1");
 
 			send_command("q\n", 0);
-			pause();
 
-			if (!child_terminated) {
-				fprintf(stderr,

testsuite/smokey/gdb/gdb.c static int run_gdb(struct smokey_test *t, int argc, char *const argv[]) · line 319

Quoted 10 lines
testsuite/smokey/gdb/gdb.c +4 -3
-					"FAILURE: gdb still running?!\n");
-				exit(1);
-			}
+			err = waitpid(childpid, &status, 0);
+			check("waitpid", err, childpid);
+			check("gdb execution", WEXITSTATUS(status), 0);
+			close(pipe_out[0]);                        
 		}
 
 		signal(SIGCHLD, SIG_DFL);

Nice catch!

Jan

Signature · 2 lines
Siemens AG, Technology
Linux Expert Center
#4 lore raw

Hi Jan,

Am 12.03.2024 um 08:49 schrieb Jan Kiszka:

Quoted 14 lines · Fabian Scheler
Quoted 13 lines

When quitting gdb by sending the quit-command, gdb may actually finish before the father-process calls pause. In that case the father-process would wait endlessly.

The fix now uses waitpid instead of pause to explicitly wait for the child-process, in contrast to pause waitpid also handles child-processes that already have been terminated.

---
  testsuite/smokey/gdb/gdb.c | 24 +++++++++++++++---------
  1 file changed, 15 insertions(+), 9 deletions(-)
Quoted 12 lines

testsuite/smokey/gdb/gdb.c static void handle_sigchld(int signum) · line 81

Quoted 10 lines
testsuite/smokey/gdb/gdb.c +0 -2
index b09b78b4e..5e9e9d101 100644
@@ -77,8 +77,7 @@ static void handle_sigchld(int signum)
  	wait(&status);
  	if (WEXITSTATUS(status) == ESRCH)
  		smokey_note("gdb: skipped (gdb not available)");
-	else
-		check("gdb execution", WEXITSTATUS(status), 0);

Why do we have to remove this?

it is not necessary to remove this, but the else-branch should not be hit anymore. On the other hand, hitting the else-path indicates an erroneous situation in the testcase, so it might make sense to replace independently of the child's status

check("gdb execution", WEXITSTATUS(status), 0);

with

check("gdb terminated unexpectedly", 0, 1);

Quoted 25 lines
Quoted 25 lines
testsuite/smokey/gdb/gdb.c +5 -1
+
  	child_terminated = 1;
  	close(pipe_out[0]);
  }
@@ -202,6 +201,8 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
  	pthread_attr_t attr;
  	char run_param[32];
  	cpu_set_t cpu_set;
+	pid_t childpid;
+	int status;
  	int err;
 
  	smokey_parse_args(t, argc, argv);
@@ -257,7 +258,8 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
  		err = pipe(pipe_out);
  		check_no_error("pipe_out", err);
 
-		switch (do_fork()) {
+		childpid = do_fork();
+		switch (childpid) {
  		case -1:
  			error(1, errno, "fork/vfork");
 
@@ -283,8 +285,14 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 
Quoted 10 lines

testsuite/smokey/gdb/gdb.c static int run_gdb(struct smokey_test *t, int argc, char *const argv[]) · line 293

Quoted 8 lines
testsuite/smokey/gdb/gdb.c +4 -0
  		default:
  			wait_for_pattern("(gdb) ");
+
+			/* the signal handler only is used to skip the test if
+			   gdb is missing */
  			if (child_terminated)
  				break;
+                        else

Indention via spaces here.

Thanks!

Ciao
Fabian

Quoted 29 lines
Quoted 14 lines
testsuite/smokey/gdb/gdb.c +2 -3
+				signal(SIGCHLD, SIG_DFL);
+
  			send_command("b breakpoint_target\n", 1);
  			send_command("r\n", 1);
 
@@ -301,13 +309,11 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
  			eval_expression("thread_loops==expected_loops", "1");
 
  			send_command("q\n", 0);
-			pause();
 
-			if (!child_terminated) {
-				fprintf(stderr,

testsuite/smokey/gdb/gdb.c static int run_gdb(struct smokey_test *t, int argc, char *const argv[]) · line 319

Quoted 10 lines
testsuite/smokey/gdb/gdb.c +4 -3
-					"FAILURE: gdb still running?!\n");
-				exit(1);
-			}
+			err = waitpid(childpid, &status, 0);
+			check("waitpid", err, childpid);
+			check("gdb execution", WEXITSTATUS(status), 0);
+			close(pipe_out[0]);
  		}
 
  		signal(SIGCHLD, SIG_DFL);

Nice catch!

Jan

Signature · 13 lines
With best regards,
Dr. Fabian Scheler

Siemens AG
T CED EDC-DE
Hertha-Sponer-Weg 3
91058 Erlangen, Germany
Phone: +49 (1522) 1702973
Mobile: +49 (1522) 1702973
mailto:[email protected]
www.siemens.com

Siemens Aktiengesellschaft: Chairman of the Supervisory Board: Jim Hagemann Snabe; Managing Board: Roland Busch, Chairman, President and Chief Executive Officer; Cedrik Neike, Matthias Rebellius, Ralf P. Thomas, Judith Wiese; Registered offices: Berlin and Munich, Germany; Commercial registries: Berlin-Charlottenburg, HRB 12300, Munich, HRB 6684; WEEE-Reg.-No. DE 23691322
#5 lore raw

PATCH v2 1/1 testsuite/smokey/gdb: Fix race-condition

When quitting gdb by sending the quit-command, gdb may actually finish before the father-process calls pause. In that case the father-process would wait endlessly.

The fix now uses waitpid instead of pause to explicitly wait for the child-process, in contrast to pause waitpid also handles child-processes that already have been terminated.

---
 testsuite/smokey/gdb/gdb.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
testsuite/smokey/gdb/gdb.c +15 -8
index b09b78b4e..1daa7ee4c 100644
@@ -78,7 +78,7 @@ static void handle_sigchld(int signum)
 	if (WEXITSTATUS(status) == ESRCH)
 		smokey_note("gdb: skipped (gdb not available)");
 	else
-		check("gdb execution", WEXITSTATUS(status), 0);
+		check("gdb terminated unexpectedly", 0, 1);
 	child_terminated = 1;
 	close(pipe_out[0]);
 }
@@ -202,6 +202,8 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 	pthread_attr_t attr;
 	char run_param[32];
 	cpu_set_t cpu_set;
+	pid_t childpid;
+	int status;
 	int err;
 
 	smokey_parse_args(t, argc, argv);
@@ -257,7 +259,8 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 		err = pipe(pipe_out);
 		check_no_error("pipe_out", err);
 
-		switch (do_fork()) {
+		childpid = do_fork();
+		switch (childpid) {
 		case -1:
 			error(1, errno, "fork/vfork");
 
@@ -283,8 +286,14 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 
 		default:
 			wait_for_pattern("(gdb) ");
+
+			/* the signal handler only is used to skip the test if
+			   gdb is missing */
 			if (child_terminated)
 				break;
+			else
+				signal(SIGCHLD, SIG_DFL);
+
 			send_command("b breakpoint_target\n", 1);
 			send_command("r\n", 1);
 
@@ -301,13 +310,11 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 			eval_expression("thread_loops==expected_loops", "1");
 
 			send_command("q\n", 0);
-			pause();
 
-			if (!child_terminated) {
-				fprintf(stderr,
-					"FAILURE: gdb still running?!\n");
-				exit(1);
-			}
+			err = waitpid(childpid, &status, 0);
+			check("waitpid", err, childpid);
+			check("gdb execution", WEXITSTATUS(status), 0);
+			close(pipe_out[0]);
 		}
 
 		signal(SIGCHLD, SIG_DFL);
Signature · 1 line
2.44.0
Applied
#6 lore raw
Quoted 68 lines · Fabian Scheler

When quitting gdb by sending the quit-command, gdb may actually finish before the father-process calls pause. In that case the father-process would wait endlessly.

The fix now uses waitpid instead of pause to explicitly wait for the child-process, in contrast to pause waitpid also handles child-processes that already have been terminated.

---
 testsuite/smokey/gdb/gdb.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
testsuite/smokey/gdb/gdb.c +11 -5
index b09b78b4e..1daa7ee4c 100644
@@ -78,7 +78,7 @@ static void handle_sigchld(int signum)
 	if (WEXITSTATUS(status) == ESRCH)
 		smokey_note("gdb: skipped (gdb not available)");
 	else
-		check("gdb execution", WEXITSTATUS(status), 0);
+		check("gdb terminated unexpectedly", 0, 1);
 	child_terminated = 1;
 	close(pipe_out[0]);
 }
@@ -202,6 +202,8 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 	pthread_attr_t attr;
 	char run_param[32];
 	cpu_set_t cpu_set;
+	pid_t childpid;
+	int status;
 	int err;
 
 	smokey_parse_args(t, argc, argv);
@@ -257,7 +259,8 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 		err = pipe(pipe_out);
 		check_no_error("pipe_out", err);
 
-		switch (do_fork()) {
+		childpid = do_fork();
+		switch (childpid) {
 		case -1:
 			error(1, errno, "fork/vfork");
 
@@ -283,8 +286,14 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 
 		default:
 			wait_for_pattern("(gdb) ");
+
+			/* the signal handler only is used to skip the test if
+			   gdb is missing */
 			if (child_terminated)
 				break;
+			else
+				signal(SIGCHLD, SIG_DFL);
+
 			send_command("b breakpoint_target\n", 1);
 			send_command("r\n", 1);
 
@@ -301,13 +310,11 @@ static int run_gdb(struct smokey_test *t, int argc, char *const argv[])
 			eval_expression("thread_loops==expected_loops", "1");
 
 			send_command("q\n", 0);
-			pause();
 
-			if (!child_terminated) {
-				fprintf(stderr,

testsuite/smokey/gdb/gdb.c static int run_gdb(struct smokey_test *t, int argc, char *const argv[]) · line 320

Quoted 10 lines
testsuite/smokey/gdb/gdb.c +4 -3
-					"FAILURE: gdb still running?!\n");
-				exit(1);
-			}
+			err = waitpid(childpid, &status, 0);
+			check("waitpid", err, childpid);
+			check("gdb execution", WEXITSTATUS(status), 0);
+			close(pipe_out[0]);
 		}
 
 		signal(SIGCHLD, SIG_DFL);

Thanks, applied.

Jan

Signature · 2 lines
Siemens AG, Technology
Linux Expert Center