Skip to content

plugin: keep a held session lock fresh, so the daily prune leaves it - #211

Merged
Saksham Uboweja (SakshamUboweja) merged 1 commit into
microsoft:mainfrom
1AbdullahButt:fix/210-hookqueue-lock-prune
Nov 11, 2026
Merged

Saksham Uboweja (SakshamUboweja) merged 1 commit into
microsoft:mainfrom
1AbdullahButt:fix/210-hookqueue-lock-prune

Conversation

@1AbdullahButt

Copy link
Copy Markdown
Contributor

Fixes #210

A session's hook lock file (locks/<host>/.<session>.lock) is created at the session's first hook. Taking the lock after that only opens and locks the file and writes nothing to it, so its mtime stays at that first hook. _prune removes lock files older than IDLE_S (2 days), and it runs while a hook holds its own lock. So once a session had been open for more than 2 days, the prune could delete the lock file while it was held. On POSIX the session's next hook then created a new file at the same path, locked it at once and ran beside the holder. That brings back the parallel-hooks race #156 closed. On Windows the delete fails while the file is open.

This PR makes run touch the lock file right after it takes the lock, before _prune runs. The file's age is then the time since the session's last hook, which is what IDLE_S is meant to measure. A session that has run no hook for 2 days still loses its file, as before.

The test is in tests/test_hook_queue.py, next to the existing prune test. It ages a session's lock file past IDLE_S and runs a hook through hookqueue.run. Inside the handler, a second hook tries to take the same session's lock. The test checks that the second hook does not get it, and that the lock file's mtime is fresh afterwards.

How it was produced: Claude Code, an AI coding agent, found the bug by reading hookqueue.py and reproduced it in a Linux container. It also wrote the fix and the test. I directed the work.

What was checked, on main at 81e32e5:

  • Without the fix, the new test fails on both platforms I could run. On Linux (Docker python:3.12), the second hook gets the lock: [True] == [False]. On Windows 11 the delete fails while the file is open, so the test fails on the mtime check instead.
  • With the fix, tests/test_hook_queue.py passes on Linux and on Windows.
  • Full tests run on Windows 11, Python 3.10: python -m pytest tests reports 888 passed, 24 skipped and 0 failed. The skips need tools this machine does not have. I did not run experiments/tests.

🤖 Generated with Claude Code

Taking a session's hook lock writes nothing to its file, so the file's
mtime stayed at the session's first hook. Once the session was open for
more than IDLE_S, `_prune` removed the lock file while a hook held it,
and on POSIX the session's next hook locked a new file and ran beside
the holder. The hook that takes the lock now touches its file first.

Fixes microsoft#210

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@SakshamUboweja
Saksham Uboweja (SakshamUboweja) merged commit 26ff86b into microsoft:main Nov 11, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

plugin: the daily lock prune deletes the lock of a session open for more than two days, and its hooks run in parallel again

2 participants