-
Notifications
You must be signed in to change notification settings - Fork 52.6k
Align Windows _jobs_lock timeout with POSIX polling pattern #62763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rahulrao85
wants to merge
2
commits into
NousResearch:main
Choose a base branch
from
rahulrao85:fix/windows-jobs-lock-timeout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """Regression tests for the Windows (msvcrt) _jobs_lock path. | ||
|
|
||
| The POSIX bounded-lock fix (#60703, #60855) added a polling loop with | ||
| 30-second timeout and graceful degradation for fcntl/flock, but the | ||
| msvcrt/Windows path was not updated — it used a blocking LK_LOCK call | ||
| with ~10s internal retry and a generic WARNING on failure. | ||
|
|
||
| These tests verify that the msvcrt path now uses LK_NBLCK with the same | ||
| 30-second bounded polling, ERROR-level timeout logging, and degraded | ||
| critical-section entry as the POSIX path. They run on any platform by | ||
| faking an msvcrt module (since real msvcrt is only available on Windows). | ||
| """ | ||
|
|
||
| import time | ||
|
|
||
| import cron.jobs as jobs_mod | ||
|
|
||
|
|
||
| def _fake_msvcrt(*, fail_count: int = 0): | ||
| """Build a fake ``msvcrt`` module for ``_jobs_lock``. | ||
|
|
||
| *fail_count*: how many ``locking()`` calls raise ``OSError`` before | ||
| succeeding. ``-1`` means always fail. | ||
| """ | ||
| attempts = [0] # mutable closure | ||
|
|
||
| class FakeMsvcrt: | ||
| LK_NBLCK = 0x0004 | ||
| LK_UNLCK = 0x0000 | ||
|
|
||
| @staticmethod | ||
| def locking(fd, mode, nbytes): | ||
| if fail_count == -1: | ||
| raise OSError(33, "lock held by another process") | ||
| attempts[0] += 1 | ||
| if attempts[0] <= fail_count: | ||
| raise OSError(33, "lock held by another process") | ||
|
|
||
| return FakeMsvcrt | ||
|
|
||
|
|
||
| class TestWindowsBoundedJobsLock: | ||
| def test_lock_times_out_and_degrades(self, monkeypatch, caplog): | ||
| """LK_NBLCK contention must not block _jobs_lock forever on Windows.""" | ||
| monkeypatch.setattr(jobs_mod, "fcntl", None) | ||
| monkeypatch.setattr(jobs_mod, "msvcrt", _fake_msvcrt(fail_count=-1)) | ||
| monkeypatch.setattr(jobs_mod, "_JOBS_LOCK_TIMEOUT_SECONDS", 1.0) | ||
| jobs_mod.ensure_dirs() | ||
| _ = jobs_mod._jobs_lock_file().touch() | ||
|
|
||
| start = time.monotonic() | ||
| entered = False | ||
| with caplog.at_level("ERROR", logger="cron.jobs"): | ||
| with jobs_mod._jobs_lock(): | ||
| entered = True | ||
| elapsed = time.monotonic() - start | ||
|
|
||
| assert entered, "critical section must still run in degraded mode" | ||
| assert elapsed < 10, f"lock wait was not bounded (took {elapsed:.1f}s)" | ||
| assert any("Timed out" in r.message for r in caplog.records), ( | ||
| "degraded-mode fallback must be logged at ERROR" | ||
| ) | ||
|
|
||
| def test_uncontended_lock_is_fast_and_silent(self, monkeypatch, caplog): | ||
| """Uncontested LK_NBLCK acquisition should succeed immediately.""" | ||
| monkeypatch.setattr(jobs_mod, "fcntl", None) | ||
| monkeypatch.setattr(jobs_mod, "msvcrt", _fake_msvcrt(fail_count=0)) | ||
| jobs_mod.ensure_dirs() | ||
|
|
||
| start = time.monotonic() | ||
| with caplog.at_level("ERROR", logger="cron.jobs"): | ||
| with jobs_mod._jobs_lock(): | ||
| pass | ||
| assert time.monotonic() - start < 5 | ||
| assert not [r for r in caplog.records if "Timed out" in r.message] | ||
|
|
||
| def test_lock_recovers_after_transient_contention(self, monkeypatch, caplog): | ||
| """A brief LK_NBLCK conflict (1 failure, then success) must not degrade.""" | ||
| monkeypatch.setattr(jobs_mod, "fcntl", None) | ||
| monkeypatch.setattr(jobs_mod, "msvcrt", _fake_msvcrt(fail_count=1)) | ||
| monkeypatch.setattr(jobs_mod, "_JOBS_LOCK_TIMEOUT_SECONDS", 3.0) | ||
| jobs_mod.ensure_dirs() | ||
|
|
||
| with caplog.at_level("ERROR", logger="cron.jobs"): | ||
| with jobs_mod._jobs_lock(): | ||
| pass | ||
| assert not [r for r in caplog.records if "Timed out" in r.message] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add regression coverage for this
LK_NBLCKpath. The existing bounded-lock tests are module-skipped withoutfcntl(tests/cron/test_ticker_stall_60703.py:45), so they do not cover Windows. A fakemsvcrtcan verify retry-to-deadline, ERROR logging, degraded critical-section entry, and an uncontended acquisition without requiring Windows CI.