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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! A fixed-inode file lock — mutual exclusion of refresh between processes.
//!
//! This is one half of REQ-15 (**between** processes). The other half (single-flight within
//! the same process) is handled by [`super::cell::StateCell`].
//!
//! **Why `try_lock()` + retry rather than a blocking `lock()`.**
//! `flock(2)` is a blocking syscall and **has no timeout argument** (the "flock(LOCK_EX) with
//! a timeout" of design §7.3 is, taken literally, an API that does not exist). The only form
//! that can produce a finite timeout is a non-blocking attempt + backoff retry + deadline.
//!
//! A2 Task 7 ([`super::TokenManager`]) calls this module, so every `dead_code` attribute that
//! Task 5 had attached has been removed.
use ;
use ;
use Path;
use ;
/// The overall deadline for acquiring the lock.
///
/// It is comfortably longer than one refresh by a sibling process (HTTP round trip + save to
/// disk), and short enough to finish before a person feels that it "has hung".
pub const LOCK_DEADLINE: Duration = from_secs;
/// The first retry interval. Most contention is resolved by this single retry.
pub const LOCK_RETRY_INITIAL: Duration = from_millis;
/// The upper bound on the retry interval. The exponential growth stops here — without a
/// bound, the caller would sleep for one more last interval (which grows to the order of
/// seconds) even after the sibling has released the lock.
pub const LOCK_RETRY_MAX: Duration = from_millis;
/// A failure to acquire the lock.
pub
/// A guard that lives for as long as the lock is held. It releases the lock in `Drop`.
///
/// This type **does not delete the lock file** — on the success, failure or panic path
/// alike. Deleting it would give rise to a process holding the existing inode and a process
/// taking a new inode at the same time, breaking mutual exclusion (design §7.3). Cleanup when
/// a process dies is done not by `unlink` but by **the kernel reclaiming the fd** — a flock
/// is released when the open file description is closed.
pub
/// Acquires the exclusive lock within `deadline`.
///
/// On success the lock is held for as long as the returned [`LockGuard`] lives. If the lock
/// file does not exist it is created; if it does, it is **opened as is** — not truncated
/// (no contents are written so there is nothing to cut, and truncating would change the file
/// from the point of view of another holder).
///
/// # Threat model — the lock file has no `ino`/`dev` re-check (intentional)
///
/// `TokenStore::load` re-verifies by `ino`/`dev` that the file it opens is the one it has
/// just checked. **That is not done here.** Unlike the tokens, the lock file's contents are
/// neither read nor written, so what is lost when an inode swapped in by someone else is
/// taken is not *confidentiality* but only *mutual exclusion*. And whoever can do that swap
/// is someone who can write to the token directory, and that person can replace
/// `tokens.json` itself — as the doc of `TokenStore::ensure_private_dir` says, that threat
/// **cannot be prevented by file-level checks** and is prevented by the premise that the
/// directory is 0700. Adding a re-check to the lock alone would be an asymmetry that guards
/// the weaker asset more heavily, and the window between the check and `flock` does not
/// close anyway (it is enough to swap after the check).
///
/// That is, there is one accepted premise: **only the owner can write to the token
/// directory.** If that premise breaks, it is not the integrity of the lock but the tokens
/// themselves that have already collapsed.
pub
/// Retries with exponential backoff until `attempt` succeeds, giving up at `deadline`.
///
/// It is split out of [`acquire`] for two reasons. (1) This function is the whole of the
/// "finite timeout" logic of REQ-15, and (2) the distinction between `TryLockError::Error`
/// (a real failure — stop immediately) and `WouldBlock` (contention — retry) **cannot be
/// tested without injection** — there is no way to make `flock` return an error other than
/// `EWOULDBLOCK` on a local Linux filesystem. Blurring that distinction would mean retrying
/// pointlessly up to the deadline on a disk error, so it is better to have a testable seam.
///
/// The deadline check being at the **end** of the loop is intentional: `deadline == 0` means
/// not "do not attempt" but "do not retry", so there is always exactly one attempt.