Commit 5b3ab9b
authored
fix(schema): Add file locking to prevent concurrent cache corruption (#4598)
* fix(schema): Add file locking to prevent concurrent cache corruption
When multiple cfn-lint processes run concurrently (e.g., pre-commit hook
partitions), they can all attempt to update the schema cache simultaneously.
The previous implementation deleted and extracted files in-place without
synchronization, causing FileNotFoundError when one process deletes a file
another process is about to unlink.
This change:
- Adds cross-platform file locking (fcntl on Unix, msvcrt on Windows)
- Extracts schemas to a temp directory then atomically swaps via rename
- Re-checks version after acquiring lock to skip redundant downloads
- Adds comprehensive tests for locking, atomic replacement, and concurrency
Fixes #4597
* test(schema): Add pragma no cover for platform-specific code
Windows-specific lock paths and defensive OSError handling cannot be
tested on Linux CI. Mark them with pragma: no cover to satisfy
coverage requirements.
* style(schema): Fix ruff E501 line-length and unused imports
Apply ruff formatting to wrap long lines (E501) and remove unused
os/sys imports flagged by the lint step.
* fix(schema): Add type: ignore for Windows-only msvcrt attributes
mypy on non-Windows platforms flags msvcrt.locking/LK_NBLCK/LK_UNLCK as
missing attributes since they only exist in the Windows typeshed stub.
The calls are guarded by sys.platform == 'win32' at runtime, so suppress
the attr-defined errors.
* fix(schema): Add type: ignore for Unix-only fcntl attributes
mypy runs on both Linux and Windows in CI. On Windows, fcntl.flock/
LOCK_EX/LOCK_NB/LOCK_UN are flagged as missing (Unix-only), mirroring
the msvcrt case on Linux. Suppress attr-defined on both platform paths;
warn_unused_ignores is disabled so the inactive-platform ignores are
harmless.
* fix(schema): Log accurate messages per update failure mode
Address CR feedback: the broad 'except Exception' around the file_lock
block mislabeled any error from _update_locked (BadZipFile, extraction
or atomic-replace OSError) as 'Failed to acquire schema cache lock'.
- Outer handler now distinguishes TimeoutError (lock wait), OSError
(lock acquisition), and an unexpected-error fallback.
- _update_locked wraps extraction + atomic replace and logs an accurate
'Failed to extract and install schema cache' message, so filesystem/
zip errors are no longer reported as lock failures.
- Add tests asserting each failure mode logs its correct message and
that update/extract errors are never labeled as lock errors.
* fix(schema): Handle network errors in version checks distinctly
Address CR follow-up: url_has_newer_version() makes a network request and
URLError subclasses OSError, so a network failure during the under-lock
re-check landed in the outer 'except OSError' and was mislabeled as
'Failed to acquire schema cache lock'.
- Wrap both version checks (the pre-lock check in update() and the
re-check in _update_locked) and log 'Failed to check schema version'.
- The pre-lock check was previously unwrapped and would crash with an
uncaught URLError; it now returns 2 consistently.
- _update_locked now never raises — it always returns an exit code — so
the outer handler only ever sees genuine lock-acquisition errors.
- Add tests for network errors at both version-check call sites.
* fix(schema): Let force bypass the pre-lock version check
Address CR follow-up: 'url_has_newer_version() or force' evaluated the
network check before force, so a --force update on an unreachable network
raised URLError and returned 2 instead of proceeding. Swap operands to
'force or url_has_newer_version()' so force short-circuits, matching the
order already used in _update_locked.
- Fix test_update_initial_version_check_network_error to use force=False
(the previous force=True assertion locked in the bug).
- Add test_update_force_bypasses_version_check asserting force=True
downloads without ever invoking the version check.1 parent 49c86cd commit 5b3ab9b
4 files changed
Lines changed: 941 additions & 29 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| |||
24 | 26 | | |
25 | 27 | | |
26 | 28 | | |
| 29 | + | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
| |||
298 | 301 | | |
299 | 302 | | |
300 | 303 | | |
301 | | - | |
302 | | - | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
303 | 307 | | |
304 | 308 | | |
305 | 309 | | |
306 | 310 | | |
307 | 311 | | |
308 | 312 | | |
309 | | - | |
310 | | - | |
311 | | - | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
312 | 367 | | |
313 | 368 | | |
314 | 369 | | |
315 | 370 | | |
316 | 371 | | |
317 | 372 | | |
318 | 373 | | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
| 374 | + | |
| 375 | + | |
331 | 376 | | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
343 | 407 | | |
344 | 408 | | |
345 | 409 | | |
346 | | - | |
| 410 | + | |
347 | 411 | | |
348 | 412 | | |
349 | 413 | | |
| |||
354 | 418 | | |
355 | 419 | | |
356 | 420 | | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
357 | 457 | | |
358 | 458 | | |
359 | 459 | | |
| |||
0 commit comments