Compare
8a631963a95b0357fdb2a4c61e5df898028c4641..c2a48fdcb56fed3a79bf43b7eac0a4b5ffb32317
t/lib-httpd: document writing concurrency-safe CGI helpers
Update t/lib-httpd.sh to document the fixes applied to apply-one-time-script.sh and http-429.sh for future developers working on helper scripts. Add concrete examples of patterns and anti-patterns that should be considered when handling state management. 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
6 files changed,
+144
−27
- Documentation/lint-gitlink.perl
- t/lib-httpd.sh
- t/lib-httpd/apply-one-time-script.sh
- t/lib-httpd/http-429.sh
- t/meson.build
- t/t5567-one-time-script.sh
modified Documentation/lint-gitlink.perl
| @@ -42,7 +42,7 @@ die "BUG: No list of valid linkgit:* files given" unless @ARGV; | |||
| 42 | 42 | while (<>) { | |
| 43 | 43 | my $line = $_; | |
| 44 | 44 | next if $line =~ /^\s*(ifn?def|endif)::/; | |
| 45 | - | while ($line =~ m/(.{0,8})((git[-a-z]+|scalar)\[(\d)*\])/g) { | |
| 45 | + | while ($line =~ m/(.{,8})((git[-a-z]+|scalar)\[(\d)*\])/g) { | |
| 46 | 46 | my $pos = pos $line; | |
| 47 | 47 | my ($macro, $target, $page, $section) = ($1, $2, $3, $4); | |
| 48 | 48 | if ( $macro ne "linkgit:" ) { | |
modified t/lib-httpd.sh
| @@ -159,6 +159,18 @@ prepare_httpd() { | |||
| 159 | 159 | mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH" | |
| 160 | 160 | cp "$TEST_PATH"/passwd "$HTTPD_ROOT_PATH" | |
| 161 | 161 | cp "$TEST_PATH"/proxy-passwd "$HTTPD_ROOT_PATH" | |
| 162 | + | # Apache can run the following scripts concurrently per request. Make | |
| 163 | + | # sure any state management logic is resilient to race conditions. | |
| 164 | + | # | |
| 165 | + | # For example: | |
| 166 | + | # - use "mkdir dir" to ensure only one request "succeeds" under some | |
| 167 | + | # condition (see http-429.sh). | |
| 168 | + | # - chain (&&) atomic operations like "rm marker" (no -f) with the | |
| 169 | + | # logic that is guarded by the marker instead of relying on a | |
| 170 | + | # separate "test -f" and "rm marker" check | |
| 171 | + | # (see apply-one-time-script.sh). | |
| 172 | + | # - use scratch file names that include the process ID ($$), so | |
| 173 | + | # concurrent requests do not overwrite each other's state. | |
| 162 | 174 | install_script incomplete-length-upload-pack-v2-http.sh | |
| 163 | 175 | install_script incomplete-body-upload-pack-v2-http.sh | |
| 164 | 176 | install_script error-no-report.sh | |
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/lib-httpd/http-429.sh
| @@ -3,7 +3,7 @@ | |||
| 3 | 3 | # Script to return HTTP 429 Too Many Requests responses for testing retry logic. | |
| 4 | 4 | # Usage: /http_429/<test-context>/<retry-after-value>/<repo-path> | |
| 5 | 5 | # | |
| 6 | - | # The test-context is a unique identifier for each test to isolate state files. | |
| 6 | + | # The test-context is a unique identifier for each test to isolate state directories. | |
| 7 | 7 | # The retry-after-value can be: | |
| 8 | 8 | # - A number (e.g., "1", "2", "100") - sets Retry-After header to that many seconds | |
| 9 | 9 | # - "none" - no Retry-After header | |
| @@ -26,14 +26,16 @@ repo_path="${remaining#*/}" # Get rest (repo path) | |||
| 26 | 26 | # The repo name is the first component before any "/" | |
| 27 | 27 | repo_name="${repo_path%%/*}" | |
| 28 | 28 | ||
| 29 | - | # Use current directory (HTTPD_ROOT_PATH) for state file | |
| 30 | - | # Create a safe filename from test_context, retry_after and repo_name | |
| 31 | - | # This ensures all requests for the same test context share the same state file | |
| 29 | + | # Use current directory (HTTPD_ROOT_PATH) to hold state directory | |
| 30 | + | # Create a safe directory name from test_context, retry_after and repo_name | |
| 31 | + | # This ensures all requests for the same test context share the same state directory | |
| 32 | 32 | safe_name=$(echo "${test_context}-${retry_after}-${repo_name}" | tr '/' '_' | tr -cd 'a-zA-Z0-9_-') | |
| 33 | - | state_file="http-429-state-${safe_name}" | |
| 33 | + | state="http-429-state-${safe_name}" | |
| 34 | 34 | ||
| 35 | - | # Check if this is the first call (no state file exists) | |
| 36 | - | if test -f "$state_file" | |
| 35 | + | # Check if this is the first call (no state directory exists), or if | |
| 36 | + | # the retry-after-value is "permanent", which indicates a 429 must be | |
| 37 | + | # returned for every request (even if the state directory exists). | |
| 38 | + | if test "$retry_after" != permanent && ! mkdir "$state" 2>/dev/null | |
| 37 | 39 | then | |
| 38 | 40 | # Already returned 429 once, forward to git-http-backend | |
| 39 | 41 | # Set PATH_INFO to just the repo path (without retry-after value) | |
| @@ -52,9 +54,6 @@ then | |||
| 52 | 54 | exec "$GIT_EXEC_PATH/git-http-backend" | |
| 53 | 55 | fi | |
| 54 | 56 | ||
| 55 | - | # Mark that we've returned 429 | |
| 56 | - | touch "$state_file" | |
| 57 | - | ||
| 58 | 57 | # Output HTTP 429 response | |
| 59 | 58 | printf "Status: 429 Too Many Requests\r\n" | |
| 60 | 59 | ||
| @@ -67,8 +66,7 @@ case "$retry_after" in | |||
| 67 | 66 | printf "Retry-After: invalid-format-123abc\r\n" | |
| 68 | 67 | ;; | |
| 69 | 68 | permanent) | |
| 70 | - | # Always return 429, don't set state file for success | |
| 71 | - | rm -f "$state_file" | |
| 69 | + | # Always return 429 | |
| 72 | 70 | printf "Retry-After: 1\r\n" | |
| 73 | 71 | printf "Content-Type: text/plain\r\n" | |
| 74 | 72 | printf "\r\n" | |
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 | |