Skip to content

Commit 68fe99f

Browse files
swissspidyclaude
andcommitted
Harden the WP_VERSION cache against typos, hangs and concurrent runs
Three points from review, all on the caching added to the Behat runner: * A non-numeric WP_CLI_TEST_WP_VERSION_CACHE_TTL made `[ "${ttl}" -ge 0 ]` fail rather than evaluate, which skipped the age check entirely and left the cached copy valid forever. A typo therefore turned the cache into one that never refreshes. Validated up front now, with a warning and the default TTL. * The fetch was unbounded, so an unreachable or unresponsive host held up the whole run instead of falling back to the cached copy. Bounded with --connect-timeout and --max-time, after which the existing stale-cache path takes over. * The cache was written by truncating the target in place, so a concurrent runner could read it between the truncation and the end of the write and hand partial JSON to jq. It now goes to a temporary file in the same directory and is moved into place, which is atomic. The move keeps the file readable to others, as the shared cache directory needs and the old redirect did. Refs wp-cli/wp-cli#6161 Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W7iMRSfmFda1bzp87UcYoM
1 parent e844d35 commit 68fe99f

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

‎bin/run-behat-tests‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ WP_VERSIONS_URL="https://raw.githubusercontent.com/wp-cli/wp-cli-tests/artifacts
108108
WP_VERSIONS_CACHE_FILE="${TMPDIR:-/tmp}/wp-cli-test-wp-version-cache/wp-versions.json"
109109
WP_VERSIONS_CACHE_TTL="${WP_CLI_TEST_WP_VERSION_CACHE_TTL:-86400}"
110110

111+
# A typo must not turn into a cache that never expires: the age comparison below
112+
# errors out on a non-numeric TTL, which would then accept the cached copy at
113+
# any age.
114+
if ! is_numeric "${WP_VERSIONS_CACHE_TTL}"; then
115+
echo "Warning: WP_CLI_TEST_WP_VERSION_CACHE_TTL is not a number of seconds, falling back to 86400."
116+
WP_VERSIONS_CACHE_TTL=86400
117+
fi
118+
111119
# Print the cached versions file if it is younger than the given number of
112120
# seconds. A negative TTL accepts it at any age.
113121
read_versions_cache() {
@@ -129,6 +137,32 @@ read_versions_cache() {
129137
cat "${WP_VERSIONS_CACHE_FILE}"
130138
}
131139

140+
# Store the versions data for the next run. The write goes through a temporary
141+
# file in the same directory, so that a concurrent runner reading the cache sees
142+
# either the previous copy or the new one, never a partial write. Caching is
143+
# best effort throughout: a temp directory that cannot be written to is not a
144+
# reason to fail the run.
145+
write_versions_cache() {
146+
local dir
147+
local tmp
148+
149+
dir=$( dirname "${WP_VERSIONS_CACHE_FILE}" )
150+
mkdir -p "${dir}" 2>/dev/null || return 0
151+
152+
tmp=$( mktemp "${dir}/wp-versions.XXXXXX" 2>/dev/null ) || return 0
153+
154+
if printf '%s' "$1" > "${tmp}" 2>/dev/null; then
155+
# mktemp creates the file private to its owner; the cache directory is
156+
# shared, and the data in it is public.
157+
chmod 644 "${tmp}" 2>/dev/null
158+
mv -f "${tmp}" "${WP_VERSIONS_CACHE_FILE}" 2>/dev/null || rm -f "${tmp}"
159+
else
160+
rm -f "${tmp}"
161+
fi
162+
163+
return 0
164+
}
165+
132166
# Print the WordPress versions data, from the cache where possible. Warnings go
133167
# to STDERR so that they cannot end up inside the returned JSON.
134168
get_wp_versions() {
@@ -140,12 +174,13 @@ get_wp_versions() {
140174
return 0
141175
fi
142176

143-
json=$( curl -s "${WP_VERSIONS_URL}" )
177+
# Bounded, so that an unreachable or unresponsive host falls back to the
178+
# cached copy instead of holding up the run indefinitely.
179+
json=$( curl -s --connect-timeout 10 --max-time 30 "${WP_VERSIONS_URL}" )
144180

145181
# Only cache a well-formed response; an error page is not one.
146182
if echo "${json}" | jq -e 'type == "object" and length > 0' > /dev/null 2>&1; then
147-
mkdir -p "$( dirname "${WP_VERSIONS_CACHE_FILE}" )" 2>/dev/null \
148-
&& printf '%s' "${json}" > "${WP_VERSIONS_CACHE_FILE}" 2>/dev/null || true
183+
write_versions_cache "${json}"
149184
printf '%s' "${json}"
150185
return 0
151186
fi

0 commit comments

Comments
 (0)