Commit

59454646906cc0d191170e69c5f3f080ad838995

t/lib-httpd: fix apply-one-time-script race under concurrent requests

apply-one-time-script.sh is a test helper that executes a
"one-time-script" responsible for modifying the response normally
returned by git-http-backend. apply-one-time-script.sh should run
"one-time-script" once and return a modified response once. However,
sometimes a race between multiple concurrent requests causes
apply-one-time-script.sh to misbehave and return multiple modified
responses or an empty response that results in:

  fatal: ... The requested URL returned error: 500
  fatal: could not fetch <oid> from promisor remote

This can be seen in the flaky failure of t5616.47 on the macOS CI
runners.

Fix the logic that checks if "one-time-script" has returned its modified
response by chaining "rm one-time-script" with its execution. This
ensures a racing script does not also have the opportunity to execute
"one-time-script".

Add t/t5567-one-time-script.sh to verify the race is fixed. Implement a
stub "git-http-backend" that intentionally invokes a concurrent request,
and check that only one modified response is returned without error.

Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Montalbo <mmontalbo@gmail.com> · 2026-09-01 15:53 UTC · committed by Junio C Hamano

Browse files
3 files changed, +121 −14
modified t/lib-httpd/apply-one-time-script.sh
@@ -6,21 +6,31 @@
6 6 #
7 7 # This can be used to simulate the effects of the repository changing in
8 8 # between HTTP request-response pairs.
9 - if test -f one-time-script
10 - then
11 - LC_ALL=C
12 - export LC_ALL
9 + test -f one-time-script || exec "$GIT_EXEC_PATH/git-http-backend"
10 +
11 + LC_ALL=C
12 + export LC_ALL
13 13
14 - "$GIT_EXEC_PATH/git-http-backend" >out
15 - ./one-time-script out >out_modified
14 + out=out.$$
15 + modified=out-modified.$$
16 + "$GIT_EXEC_PATH/git-http-backend" >"$out"
16 17
17 - if cmp -s out out_modified
18 - then
19 - cat out
20 - else
21 - cat out_modified
22 - rm one-time-script
23 - fi
18 + # Since Apache can execute this script for multiple requests
19 + # concurrently, we chain "rm one-time-script" with the logic
20 + # for generating a modified response. If the "rm" ran separately,
21 + # a concurrent request could pass the "test -f" above and
22 + # erroneously result in multiple modified responses or an empty
23 + # body depending on the race state.
24 + #
25 + # We discard stderr for ./one-time-script since it is possible
26 + # ./one-time-script has been removed already, which is expected
27 + # sometimes. In this case, the unmodified response will be returned.
28 + if ./one-time-script "$out" 2>/dev/null >"$modified" &&
29 + ! cmp -s "$out" "$modified" &&
30 + rm one-time-script 2>/dev/null
31 + then
32 + cat "$modified"
24 33 else
25 - "$GIT_EXEC_PATH/git-http-backend"
34 + cat "$out"
26 35 fi
36 + rm -f "$out" "$modified"
modified t/meson.build
@@ -707,6 +707,7 @@ integration_tests = [
707 707 't5564-http-proxy.sh',
708 708 't5565-push-multiple.sh',
709 709 't5566-push-group.sh',
710 + 't5567-one-time-script.sh',
710 711 't5570-git-daemon.sh',
711 712 't5571-pre-push-hook.sh',
712 713 't5572-pull-submodule.sh',
added t/t5567-one-time-script.sh mode 100755
@@ -0,0 +1,96 @@
1 + #!/bin/sh
2 +
3 + test_description='apply-one-time-script CGI helper is safe under concurrent requests'
4 +
5 + . ./test-lib.sh
6 +
7 + HELPER="$TEST_DIRECTORY/lib-httpd/apply-one-time-script.sh"
8 +
9 + test_expect_success PIPE 'helper only serves one rewritten response for concurrent requests' '
10 + mkdir workdir fakebin &&
11 + ENTERED="$PWD/entered" &&
12 + GATE="$PWD/gate" &&
13 + export ENTERED GATE &&
14 + mkfifo "$ENTERED" "$GATE" &&
15 +
16 + # A stub git-http-backend that returns a response based on
17 + # $ROLE. For $ROLE = modify, return the response string
18 + # "packfile", which ends up being modified by the example
19 + # one-time-script below.
20 + #
21 + # Otherwise, run the branch returning a response that
22 + # should be passed through, and block until released
23 + # by "read -r $GATE".
24 + write_script fakebin/git-http-backend <<-\EOF &&
25 + printf "Status: 200 OK\r\n"
26 + printf "Content-Type: application/x-git-result\r\n"
27 + printf "\r\n"
28 + if test "$ROLE" = modify
29 + then
30 + printf "packfile\n"
31 + else
32 + echo entered >"$ENTERED"
33 + read -r released <"$GATE"
34 + printf "refs\n"
35 + fi
36 + EOF
37 +
38 + # An example one-time-script for apply-one-time-script
39 + # to execute. Checks for "packfile" in the response
40 + # that will be returned, and replaces it with a
41 + # modified response. Passes through responses without
42 + # "packfile" in them.
43 + write_script workdir/one-time-script <<-\EOF &&
44 + if grep packfile "$1" >/dev/null
45 + then
46 + sed "/packfile/q" "$1" &&
47 + printf "REPLACED\n"
48 + else
49 + cat "$1"
50 + fi
51 + EOF
52 +
53 + GIT_EXEC_PATH="$PWD/fakebin" &&
54 + export GIT_EXEC_PATH &&
55 +
56 + # Ensure $GATE has a reader so the test does not block indefinitely if
57 + # the helper is buggy and "echo released >&9" below does not unblock
58 + # the unmodified response gate.
59 + exec 9<>"$GATE" &&
60 +
61 + # Launch the passthrough request in the background. Record its pid
62 + # so it can be killed when the test finishes if, for some reason, the
63 + # request stays blocked and would stall a test runner.
64 + { (
65 + cd workdir &&
66 + ROLE=passthrough sh "$HELPER" >../passthrough.out 2>../passthrough.err
67 + ) & } &&
68 + passthrough_pid=$! &&
69 + test_when_finished "kill $passthrough_pid 2>/dev/null || :" &&
70 +
71 + # Wait until the passthrough request is "in-flight" and paused
72 + # mid-response.
73 + read -r entered <"$ENTERED" &&
74 +
75 + # Launch the request for a modified response while the passthrough
76 + # request is concurrently "in-flight" and paused.
77 + (
78 + cd workdir &&
79 + ROLE=modify sh "$HELPER" >../modify.out 2>../modify.err
80 + ) &&
81 +
82 + # Unblock the passthrough request, allowing git-http-backend to
83 + # complete its response.
84 + echo released >&9 &&
85 + { wait "$passthrough_pid" || :; } &&
86 +
87 + test_must_be_empty passthrough.err &&
88 + test_must_be_empty modify.err &&
89 + test_grep "Status: 200 OK" passthrough.out &&
90 + test_grep "Status: 200 OK" modify.out &&
91 + test_grep REPLACED modify.out &&
92 + test_grep ! REPLACED passthrough.out &&
93 + test_grep refs passthrough.out
94 + '
95 +
96 + test_done