Skip to content

Commit a928d28

Browse files
committed
t/lib-httpd: document writing concurrency-safe CGI helpers
The apply-one-time-script.sh and http-429.sh fixes addressed the same underlying problem: a CGI helper assumed it had exclusive access to a file when Apache can run it for several requests at once. Document the atomic idioms that avoid this next to where lib-httpd.sh installs the CGI scripts, so the guidance is visible to anyone adding another one. The note covers the anti-pattern (a "test -f" check then a separate action) and the two safe operations these helpers use: "mkdir" to elect the first request (http-429.sh) and "rm" without "-f" to consume a one-shot marker (apply-one-time-script.sh). Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
1 parent 3c9ff51 commit a928d28

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

t/lib-httpd.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ prepare_httpd() {
159159
mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH"
160160
cp "$TEST_PATH"/passwd "$HTTPD_ROOT_PATH"
161161
cp "$TEST_PATH"/proxy-passwd "$HTTPD_ROOT_PATH"
162+
# Apache runs each of these CGI scripts once per request. Apache can run one
163+
# script for several requests at the same time. A helper that keeps state
164+
# between requests must update that state with one atomic operation. A check
165+
# and then a separate action is not safe: two requests can both pass the
166+
# check before either one acts. Test the exit status of one atomic operation
167+
# instead:
168+
# - "mkdir dir" fails if the directory exists, so only one request
169+
# succeeds. http-429.sh selects the first request this way.
170+
# - "rm marker" (without "-f") fails if the marker is gone, so only one
171+
# request consumes it. apply-one-time-script.sh claims its one-shot
172+
# marker this way.
173+
# A scratch file name includes the process ID ($$), so concurrent requests
174+
# do not overwrite each other's files.
162175
install_script incomplete-length-upload-pack-v2-http.sh
163176
install_script incomplete-body-upload-pack-v2-http.sh
164177
install_script error-no-report.sh

0 commit comments

Comments
 (0)