Skip to content

Commit 07353d9

Browse files
szedergitster
authored andcommitted
git p4 test: clean up the p4d cleanup functions
Confusingly, the 'git p4' tests used two cleanup functions: - 'kill_p4d' was run in the last test before 'test_done', and it not only killed 'p4d', but it killed the watchdog process, and cleaned up after 'p4d' as well by removing all directories used by the P4 daemon and client. This cleanup is not necessary right before 'test_done', because the whole trash directory is about to get removed anyway, but it is necessary in 't9801-git-p4-branch.sh', which uses 'kill_p4d' to stop 'p4d' before re-starting it in the middle of the test script. - 'cleanup' was run in the trap on EXIT, and it killed 'p4d', but, it didn't kill the watchdog process, and, contrarily to its name, didn't perform any cleanup whatsoever. Make it clearer what's going on by renaming and simplifying the cleanup functions, so in the end we'll have: - 'stop_p4d_and_watchdog' replaces 'cleanup' as it will try to live up to its name and stop both the 'p4d' and the watchdog processes, and as the sole function registered with 'test_atexit' it will be responsible for no leaving any stray processes behind after 'git p4' tests were finished or interrupted. - 'stop_and_cleanup_p4d' replaces 'kill_p4d' as it will stop 'p4d' (and the watchdog) and remove all directories used by the P4 daemon and cliean, so it can be used mid-script to stop and then re-start 'p4d'. Note that while 'cleanup' sent a single SIGKILL to 'p4d', 'kill_p4d' was quite brutal, as it first sent SIGTERM to the daemon repeatedly, either until its pid disappeared or until a given timeout was up, and then it sent SIGKILL repeatedly, for good measure. This is overkill (pardon the pun): a single SIGKILL should be able to take down any process in a sensible state, and if a process were to somehow end up stuck in the dreaded uninterruptible sleep state then even repeated SIGKILLs won't bring immediate help. So ditch all the repeated SIGTERM/SIGKILL parts, and use a single SIGKILL to stop 'p4d', and make sure that there are no races between asynchron signal delivery and subsequent restart of 'p4d' by waiting for it to die. With this change the 'retry_until_fail' helper has no callers left, remove it. Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 99e37c2 commit 07353d9

File tree

2 files changed

+12
-28
lines changed

2 files changed

+12
-28
lines changed

t/lib-git-p4.sh

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,8 @@ cli="$TRASH_DIRECTORY/cli"
6767
git="$TRASH_DIRECTORY/git"
6868
pidfile="$TRASH_DIRECTORY/p4d.pid"
6969

70-
# Sometimes "prove" seems to hang on exit because p4d is still running
71-
cleanup () {
72-
if test -f "$pidfile"
73-
then
74-
kill -9 $(cat "$pidfile") 2>/dev/null && exit 255
75-
fi
70+
stop_p4d_and_watchdog () {
71+
kill -9 $p4d_pid $watchdog_pid
7672
}
7773

7874
# git p4 submit generates a temp file, which will
@@ -87,7 +83,7 @@ start_p4d () {
8783
# Don't register and then run the same atexit handlers several times.
8884
if test -z "$registered_stop_p4d_atexit_handler"
8985
then
90-
test_atexit 'kill_p4d; cleanup'
86+
test_atexit 'stop_p4d_and_watchdog'
9187
registered_stop_p4d_atexit_handler=AlreadyDone
9288
fi
9389

@@ -100,21 +96,21 @@ start_p4d () {
10096
echo $! >"$pidfile"
10197
}
10298
) &&
99+
p4d_pid=$(cat "$pidfile")
103100

104101
# This gives p4d a long time to start up, as it can be
105102
# quite slow depending on the machine. Set this environment
106103
# variable to something smaller to fail faster in, say,
107104
# an automated test setup. If the p4d process dies, that
108105
# will be caught with the "kill -0" check below.
109106
i=${P4D_START_PATIENCE:-300}
110-
pid=$(cat "$pidfile")
111107

112108
timeout=$(($(time_in_seconds) + $P4D_TIMEOUT))
113109
while true
114110
do
115111
if test $(time_in_seconds) -gt $timeout
116112
then
117-
kill -9 $pid
113+
kill -9 $p4d_pid
118114
exit 1
119115
fi
120116
sleep 1
@@ -131,7 +127,7 @@ start_p4d () {
131127
break
132128
fi
133129
# fail if p4d died
134-
kill -0 $pid 2>/dev/null || break
130+
kill -0 $p4d_pid 2>/dev/null || break
135131
echo waiting for p4d to start
136132
sleep 1
137133
i=$(( $i - 1 ))
@@ -178,22 +174,10 @@ retry_until_success () {
178174
done
179175
}
180176

181-
retry_until_fail () {
182-
timeout=$(($(time_in_seconds) + $RETRY_TIMEOUT))
183-
until ! "$@" 2>/dev/null || test $(time_in_seconds) -gt $timeout
184-
do
185-
sleep 1
186-
done
187-
}
188-
189-
kill_p4d () {
190-
pid=$(cat "$pidfile")
191-
retry_until_fail kill $pid
192-
retry_until_fail kill -9 $pid
193-
# complain if it would not die
194-
test_must_fail kill $pid >/dev/null 2>&1 &&
195-
rm -rf "$db" "$cli" "$pidfile" &&
196-
retry_until_fail kill -9 $watchdog_pid
177+
stop_and_cleanup_p4d () {
178+
kill -9 $p4d_pid $watchdog_pid
179+
wait $p4d_pid
180+
rm -rf "$db" "$cli" "$pidfile"
197181
}
198182

199183
cleanup_git () {

t/t9801-git-p4-branch.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ test_expect_success 'import depot, branch detection, branchList branch definitio
151151
'
152152

153153
test_expect_success 'restart p4d' '
154-
kill_p4d &&
154+
stop_and_cleanup_p4d &&
155155
start_p4d
156156
'
157157

@@ -505,7 +505,7 @@ test_expect_success 'use-client-spec detect-branches skips files in branches' '
505505
'
506506

507507
test_expect_success 'restart p4d' '
508-
kill_p4d &&
508+
stop_and_cleanup_p4d &&
509509
start_p4d
510510
'
511511

0 commit comments

Comments
 (0)