Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
[PATCH] history: fix lock file age check broken by 53257
- X-seq: zsh-workers 55194
- From: Mihai Cosma <mcosma@xxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: [PATCH] history: fix lock file age check broken by 53257
- Date: Sun, 4 Oct 2026 21:01:43 -0400
- Archived-at: <https://zsh.org/workers/55194>
- List-id: <zsh-workers.zsh.org>
53257 made checklocktime() take "now" from zmonotime(), but "then" is
the lock file's st_mtime, which is wall-clock time. Since the
monotonic clock usually counts from boot, every existing lock looked
more than 10 seconds in the future, so a shell neither waited for a
live lock nor removed a stale one; it failed at once with EEXIST. Go
back to time() for the age check.
The deadline passed to zsleep_random() was also a wall-clock time, while
zsleep_random() now compares it with the monotonic clock, so once the
age check works the backoff would not be capped at the lock's expiry.
Convert the time remaining until expiry into a monotonic deadline.
---
Against master (8cc5eade). 53257 is in 5.9.0.2-test and 5.9.0.3-test, but not in 5.9, 5.9.1 or 5.9.2.
Whenever a .LOCK file exists, every locking attempt that waits (loading history at startup, fc -p/-R/-W/-A, the save on exit) fails at once with "locking failed for ...: file exists". So of two shells exiting together, the second loses the history it would save on exit, and a stale lock blocks these paths until it's deleted by hand. INC_APPEND_HISTORY's and SHARE_HISTORY's non-waiting reads and saves skip silently while the lock exists, as in 5.9. HIST_FCNTL_LOCK isn't affected.
cd "$(mktemp -d)"
print 'echo old' > hist
: > hist.LOCK
touch -t 200001010000 hist.LOCK
zsh -fc 'fc -R hist'; [[ -e hist.LOCK || -L hist.LOCK ]] && echo lock still there
5.9 prints nothing; master prints the error and "lock still there". With a fresh lock (ln -s x hist.LOCK), master fails at once, while 5.9 and the patched build wait about 10 seconds and then take it.
The new W01history.ztst test fails on master and passes with the patch. It covers the age check; a test for the backoff cap would add about 10 seconds to make check and depend on the C library's rand(), but I can add one if you want it. make check passes on Fedora 44 x86_64 with gcc 16.
53257 broke the mail check the same way (unread mail is announced at every check). I'll send that separately.
Src/hist.c | 7 ++++---
Test/W01history.ztst | 11 +++++++++++
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/Src/hist.c b/Src/hist.c
index c353601bf..61e5a71ad 100644
--- a/Src/hist.c
+++ b/Src/hist.c
@@ -3147,7 +3147,8 @@ static int lockhistct;
static int
checklocktime(char *lockfile, long *sleep_usp, time_t then)
{
- time_t now = zmonotime(NULL);
+ /* "then" is the lock file's mtime, so compare with wall-clock time. */
+ time_t now = time(NULL);
if (now + 10 < then) {
/* File is more than 10 seconds in the future? */
@@ -3163,9 +3164,9 @@ checklocktime(char *lockfile, long *sleep_usp, time_t then)
DPUTS(now < then, "time flowing backwards through history");
/*
* Randomise to minimise clashes with shells exiting at the same
- * time.
+ * time. zsleep_random() takes a monotonic deadline.
*/
- (void)zsleep_random(*sleep_usp, then + 10);
+ (void)zsleep_random(*sleep_usp, zmonotime(NULL) + (then + 10 - now));
*sleep_usp <<= 1;
} else
unlink(lockfile);
diff --git a/Test/W01history.ztst b/Test/W01history.ztst
index 665a37f2c..a8a2cc422 100644
--- a/Test/W01history.ztst
+++ b/Test/W01history.ztst
@@ -114,3 +114,14 @@ F:Check that a history bug introduced by workers/34160 is working again.
> 5 five\\\\\
> 6 while false\ndo\ntrue\\n && break\ndone
> 7 echo one\\ntwo
+
+ mkdir histlock.tmp
+ print 'echo old' >histlock.tmp/hist
+ : >histlock.tmp/hist.LOCK
+ touch -t 200001010000 histlock.tmp/hist.LOCK
+ (unsetopt histfcntllock; fc -R histlock.tmp/hist)
+ [[ -e histlock.tmp/hist.LOCK || -L histlock.tmp/hist.LOCK ]] || print stale lock removed
+ rm -rf histlock.tmp
+0:fc -R removes a stale history lock file
+>stale lock removed
+F:Regression introduced by workers/53257 (monotonic clock vs. lock mtime).
--
2.55.0
Messages sorted by:
Reverse Date,
Date,
Thread,
Author