Commit e0283a9
committed
Switch to per-file locking.
Replace the Locked contextmanager (which locks a directory, preventing
other (cooperative) processes to access it) by a private _lock_path
contextmanager, which locks a single file (or directory).
- The finer grained lock avoids locking out the entire tex cache when
handling usetex, which is useful when running multiple processes at
once.
- Python3 implements the `"x"` ("exclusive") mode to open, which we can
use instead of relying on `makedirs` to achieve a race-free operation
on the filesystem.
- The previous implementation allowed multiple threads of a single
process to acquire the same lock, but (for the use cases here, e.g.
running a tex subprocess) this is actually undesirable. Removing this
behavior also simplifies the implementation.
- As far as I can tell, the previous implementation was actually racy:
in
retries = 50
sleeptime = 0.1
while retries:
files = glob.glob(self.pattern)
if files and not files[0].endswith(self.end):
time.sleep(sleeptime)
retries -= 1
else:
break
else:
err_str = _lockstr.format(self.pattern)
raise self.TimeoutError(err_str)
# <----- HERE
if not files:
try:
os.makedirs(self.lock_path)
except OSError:
pass
else: # PID lock already here --- someone else will remove it.
self.remove = False
multiple processes can reach "HERE" at the same time and each
successfully create their own lock.1 parent e54939a commit e0283a9
4 files changed
Lines changed: 46 additions & 13 deletions
File tree
- doc/api/next_api_changes
- lib/matplotlib
- cbook
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
2486 | 2487 | | |
2487 | 2488 | | |
2488 | 2489 | | |
| 2490 | + | |
2489 | 2491 | | |
2490 | 2492 | | |
2491 | 2493 | | |
| |||
2541 | 2543 | | |
2542 | 2544 | | |
2543 | 2545 | | |
| 2546 | + | |
| 2547 | + | |
| 2548 | + | |
| 2549 | + | |
| 2550 | + | |
| 2551 | + | |
| 2552 | + | |
| 2553 | + | |
| 2554 | + | |
| 2555 | + | |
| 2556 | + | |
| 2557 | + | |
| 2558 | + | |
| 2559 | + | |
| 2560 | + | |
| 2561 | + | |
| 2562 | + | |
| 2563 | + | |
| 2564 | + | |
| 2565 | + | |
| 2566 | + | |
| 2567 | + | |
| 2568 | + | |
| 2569 | + | |
| 2570 | + | |
| 2571 | + | |
| 2572 | + | |
| 2573 | + | |
| 2574 | + | |
| 2575 | + | |
| 2576 | + | |
| 2577 | + | |
| 2578 | + | |
| 2579 | + | |
2544 | 2580 | | |
2545 | 2581 | | |
2546 | 2582 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1446 | 1446 | | |
1447 | 1447 | | |
1448 | 1448 | | |
1449 | | - | |
| 1449 | + | |
1450 | 1450 | | |
1451 | 1451 | | |
1452 | 1452 | | |
| |||
1459 | 1459 | | |
1460 | 1460 | | |
1461 | 1461 | | |
1462 | | - | |
| 1462 | + | |
1463 | 1463 | | |
1464 | | - | |
| 1464 | + | |
1465 | 1465 | | |
1466 | 1466 | | |
1467 | 1467 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | | - | |
38 | 36 | | |
39 | 37 | | |
40 | 38 | | |
| 39 | + | |
41 | 40 | | |
42 | 41 | | |
43 | 42 | | |
44 | 43 | | |
45 | 44 | | |
| 45 | + | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
52 | 51 | | |
| 52 | + | |
53 | 53 | | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
| 54 | + | |
59 | 55 | | |
60 | 56 | | |
61 | 57 | | |
| |||
340 | 336 | | |
341 | 337 | | |
342 | 338 | | |
343 | | - | |
| 339 | + | |
344 | 340 | | |
345 | 341 | | |
346 | 342 | | |
| |||
436 | 432 | | |
437 | 433 | | |
438 | 434 | | |
439 | | - | |
| 435 | + | |
440 | 436 | | |
441 | 437 | | |
442 | 438 | | |
| |||
0 commit comments