From 81aeb5cf8620f7bf45773566dd2e77bcbd53e934 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Fri, 27 Nov 2026 18:04:06 +0100 Subject: [PATCH] mod_dav: Use one clock for lock timeouts. f33eb040d9 (#920) took "now" for the lock expiry in dav_get_timeout_string() from apr_time_now(), while the expiry check and the remaining time in the LOCK response still use time(NULL). On Linux time(NULL) can lag gettimeofday() by up to a clock tick, so a LOCK handled right after a second boundary got an expiry one second later than asked: a "Second-0" lock reported "Second-1" or was still held (423). Go back to time(NULL), as on 2.4.x, keeping the overflow capping. Let test_dav_004_01/02 accept one second less, since a second may tick between the LOCK and its response, and add a test locking repeatedly across second boundaries. --- modules/dav/main/util.c | 3 +- .../dav/test_004_lock_timeout_overflow.py | 46 +++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/modules/dav/main/util.c b/modules/dav/main/util.c index 3eae91a9a1b..37704c6b47c 100644 --- a/modules/dav/main/util.c +++ b/modules/dav/main/util.c @@ -611,7 +611,8 @@ DAV_DECLARE(time_t) dav_get_timeout_string(request_rec *r, sec = 0; } - now = apr_time_sec(apr_time_now()); + /* same clock as the expiry checks and the Timeout output */ + now = time(NULL); /* The RFC 4918 maximum of 2^32 - 1 does not necessarily * fit a time_t on platforms where time_t is 32 bits. diff --git a/test/modules/dav/test_004_lock_timeout_overflow.py b/test/modules/dav/test_004_lock_timeout_overflow.py index d48acd85f31..362d430acc0 100644 --- a/test/modules/dav/test_004_lock_timeout_overflow.py +++ b/test/modules/dav/test_004_lock_timeout_overflow.py @@ -1,6 +1,8 @@ import glob +import http.client import os import re +import time import pytest @@ -30,7 +32,7 @@ class TestLockTimeoutOverflow: def _class_scope(cls, env): dav_dir = os.path.join(env.gen_dir, 'dav-timeout-overflow') os.makedirs(dav_dir, exist_ok=True) - for name in ('file1.txt', 'file2.txt', 'file3.txt'): + for name in ('file1.txt', 'file2.txt', 'file3.txt', 'file4.txt'): with open(os.path.join(dav_dir, name), 'w') as fd: fd.write('hello\n') @@ -70,7 +72,8 @@ def test_dav_004_01(self, env): assert r.response["status"] == 200, r.response m = re.search(r'([^<]*)', r.stdout) assert m, f"no in response: {r.stdout}" - assert m.group(1) == "Second-4294967295", \ + # a second may tick between the LOCK and its response + assert m.group(1) in ("Second-4294967295", "Second-4294967294"), \ f"timeout: {m.group(1)}" # an ordinary, in-range timeout request is unaffected @@ -79,7 +82,8 @@ def test_dav_004_02(self, env): assert r.response["status"] == 200, r.response m = re.search(r'([^<]*)', r.stdout) assert m, f"no in response: {r.stdout}" - assert m.group(1) == "Second-600", f"timeout: {m.group(1)}" + assert m.group(1) in ("Second-600", "Second-599"), \ + f"timeout: {m.group(1)}" # a Second- value with no digits at all parses the same way atol() # already did (0, not an error), same as before this fix: an @@ -91,3 +95,39 @@ def test_dav_004_03(self, env): m = re.search(r'([^<]*)', r.stdout) assert m, f"no in response: {r.stdout}" assert m.group(1) == "Second-0", f"timeout: {m.group(1)}" + + # the expiry, the expiry check and the reported remaining time + # come from the same clock: a lock expiring "now" is never still + # held and never reports a second left, even when the LOCK lands + # right after a second boundary + def test_dav_004_04(self, env): + conn = http.client.HTTPConnection(env.http_addr, env.http_port, + timeout=10) + headers = { + 'Host': f"test1.{env.http_tld}", + 'Timeout': 'Second-0', + 'Content-Type': 'text/xml', + } + bad = [] + count = 0 + # span several second boundaries + end = time.time() + 3.5 + try: + while time.time() < end: + conn.request('LOCK', '/dav-timeout-overflow/file4.txt', + body=LOCK_BODY, headers=headers) + resp = conn.getresponse() + body = resp.read().decode() + count += 1 + if resp.status != 200: + # a lock expiring "now" is still held: 423 + bad.append(resp.status) + continue + m = re.search(r'([^<]*)', body) + assert m, f"no in response: {body}" + if m.group(1) != "Second-0": + bad.append(m.group(1)) + finally: + conn.close() + assert not bad, \ + f"{len(bad)} of {count} locks: {sorted(set(map(str, bad)))}"