MCPcopy Create free account
hub / github.com/tox-dev/filelock / _hammer

Function _hammer

tasks/verify_filesystem.py:109–136  ·  view source on GitHub ↗
(name: str, lock_path: str)

Source from the content-addressed store, hash-verified

107
108
109def _hammer(name: str, lock_path: str) -> tuple[list[tuple[float, float]], str | None]:
110 lock = _build(name, lock_path)
111 intervals: list[tuple[float, float]] = []
112 acquire_timeout: float = 60.0
113 hold_seconds: float = 0.004
114 gap_max_seconds: float = 0.008
115 for _ in range(_HOLDS):
116 if (reason := _resiliently(lambda: lock.acquire(timeout=acquire_timeout))) is not None:
117 if isinstance(lock, StrictSoftFileLock):
118 try:
119 reason = f"{reason}; claims={[(claim.state, claim.pid, claim.name) for claim in lock.claims]}"
120 except SoftFileLockProtocolError as error:
121 reason = f"{reason}; claims unavailable: {error}"
122 return intervals, reason
123 enter = time.monotonic()
124 # Hold briefly so a broken lock lets a second holder in during an observable window; a correct lock serializes
125 # the holds regardless. enter is stamped after acquire and leave before release, so a correct hand-off can never
126 # look like an overlap even though release and the next acquire race.
127 time.sleep(hold_seconds)
128 leave = time.monotonic()
129 if (reason := _resiliently(lock.release)) is not None:
130 intervals.append((enter, leave))
131 return intervals, reason
132 intervals.append((enter, leave))
133 # A randomized gap outside the lock breaks the lock-step herd, so a poll-based lock hands off fairly and no
134 # contender is starved out of finishing its holds.
135 time.sleep(random.uniform(0, gap_max_seconds)) # ruff:ignore[suspicious-non-cryptographic-random-usage] - test dispersion, not cryptographic
136 return intervals, None
137
138
139def _resiliently(action: Callable[[], object]) -> str | None:

Callers

nothing calls this directly

Calls 4

_buildFunction · 0.85
_resilientlyFunction · 0.85
sleepMethod · 0.80
acquireMethod · 0.45

Tested by

no test coverage detected