From 9b03ff66e45d019631ed9fd3322c0911ce14a8a7 Mon Sep 17 00:00:00 2001
From: Andy Fan <zhihuifan1213@163.com>
Date: Fri, 10 Jan 2025 06:56:11 +0000
Subject: [PATCH 1/1] pgbench: Avoid misleading error for \[set]shell when
 timer_exceeded.

fgets in executeMetaCommand may return NULL if it receives a signal
during the shell command is executing. Before this commit, pgbench
client raises ERROR like below.

pgbench: error: client 3 aborted in command 3 (setshell) of script 0;
execution of meta-command failed

This behavior is misleading since people may think something is
wrong. In this commit, we just ignore fgets return NULL when
timer_exceeded.
---
 src/bin/pgbench/pgbench.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index c415e0f32c..7f61cd3aea 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -3851,7 +3851,9 @@ advanceConnectionState(TState *thread, CState *st, StatsData *agg)
 				Assert(st->state == CSTATE_WAIT_RESULT ||
 					   st->state == CSTATE_END_COMMAND ||
 					   st->state == CSTATE_SLEEP ||
-					   st->state == CSTATE_ABORTED);
+					   st->state == CSTATE_ABORTED ||
+					   st->state == CSTATE_FINISHED);
+
 				break;
 
 				/*
@@ -4414,6 +4416,8 @@ executeMetaCommand(CState *st, pg_time_usec_t *now)
 	{
 		if (!runShellCommand(&st->variables, argv[1], argv + 2, argc - 2))
 		{
+			if (timer_exceeded)
+				return CSTATE_FINISHED;
 			commandFailed(st, "setshell", "execution of meta-command failed");
 			return CSTATE_ABORTED;
 		}
@@ -4422,6 +4426,8 @@ executeMetaCommand(CState *st, pg_time_usec_t *now)
 	{
 		if (!runShellCommand(&st->variables, NULL, argv + 1, argc - 1))
 		{
+			if (timer_exceeded)
+				return CSTATE_FINISHED;
 			commandFailed(st, "shell", "execution of meta-command failed");
 			return CSTATE_ABORTED;
 		}
-- 
2.45.1

