Commit e7f7066
authored
fix(circuit_breaker): make probe election per-thread and synchronize local counters (#8323)
* fix(circuit_breaker): make probe election per-thread and synchronize local counters
The half-open probe owner id was minted once per process, so every thread
in a process passed the owner check once any sibling won the election and
all of them probed the recovering backend. The id is now a per-thread
uuid (pid-aware for forked workers); the existing DynamoDB conditional
write then elects one prober across threads and processes with no
protocol change.
The in-memory failure/success counters and observed-state map are now
guarded by a lock, with threshold crossings detected atomically so a
trip is persisted exactly once. Persistence settings are keyed per
circuit name instead of living as shared instance attributes, and the
local cache no longer raises into the protected call on concurrent
expiry.
* test(circuit_breaker): cover losing the expired-lease probe takeover election
The takeover's failure arm was untested: when another environment wins
the conditional election, the caller must get the open-circuit response
and the protected function must not run.
* fix(circuit_breaker): address review feedback
- Make the counter race test catch a missing lock: park readers mid-increment so an unsynchronized read-modify-write deterministically loses updates (fails 10/10 unlocked, passes 10/10 locked)
- Document that on_circuit_open and on_transition callbacks can run concurrently and must be thread-safe
- Use dict.get's default instead of `or` for the per-circuit settings lookup
* fix(circuit_breaker): avoid LRUDict.pop when evicting expired cache entries
- On Python 3.10, OrderedDict.pop re-enters the subclass __getitem__ after
detaching the linked-list node, so LRUDict.pop raises KeyError for a present
key and corrupts the dict (fixed in CPython 3.11) — this failed 14 tests on
the 3.10 CI job only
- Evict with a guarded del instead, which never calls subclass hooks; the
surrounding lock already makes the get-then-delete atomic, and the
try/except keeps the never-raise-into-the-protected-call contract explicit1 parent bc78b7f commit e7f7066
7 files changed
Lines changed: 433 additions & 105 deletions
File tree
- aws_lambda_powertools/utilities/circuit_breaker_alpha
- persistence
- docs/utilities
- tests/functional/circuit_breaker_alpha
Lines changed: 59 additions & 23 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| |||
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
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 | + | |
42 | 67 | | |
43 | 68 | | |
44 | 69 | | |
| |||
111 | 136 | | |
112 | 137 | | |
113 | 138 | | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
118 | 144 | | |
119 | 145 | | |
120 | 146 | | |
121 | | - | |
| 147 | + | |
| 148 | + | |
122 | 149 | | |
123 | 150 | | |
124 | 151 | | |
125 | 152 | | |
126 | | - | |
| 153 | + | |
127 | 154 | | |
128 | 155 | | |
129 | 156 | | |
130 | 157 | | |
131 | | - | |
132 | | - | |
133 | | - | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
134 | 162 | | |
135 | 163 | | |
136 | 164 | | |
137 | 165 | | |
138 | 166 | | |
139 | | - | |
| 167 | + | |
140 | 168 | | |
141 | 169 | | |
142 | 170 | | |
| |||
148 | 176 | | |
149 | 177 | | |
150 | 178 | | |
151 | | - | |
152 | | - | |
153 | | - | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
154 | 187 | | |
155 | 188 | | |
156 | 189 | | |
| |||
159 | 192 | | |
160 | 193 | | |
161 | 194 | | |
162 | | - | |
163 | 195 | | |
164 | 196 | | |
165 | 197 | | |
166 | | - | |
| 198 | + | |
| 199 | + | |
167 | 200 | | |
168 | 201 | | |
169 | 202 | | |
| |||
176 | 209 | | |
177 | 210 | | |
178 | 211 | | |
179 | | - | |
| 212 | + | |
| 213 | + | |
180 | 214 | | |
181 | 215 | | |
182 | 216 | | |
183 | | - | |
184 | | - | |
185 | | - | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
186 | 224 | | |
187 | 225 | | |
188 | | - | |
189 | | - | |
190 | 226 | | |
191 | 227 | | |
192 | 228 | | |
| |||
Lines changed: 70 additions & 34 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | | - | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
35 | 47 | | |
36 | 48 | | |
37 | 49 | | |
| |||
50 | 62 | | |
51 | 63 | | |
52 | 64 | | |
53 | | - | |
54 | | - | |
55 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
56 | 72 | | |
57 | 73 | | |
58 | 74 | | |
59 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
60 | 79 | | |
61 | 80 | | |
62 | 81 | | |
63 | | - | |
| 82 | + | |
64 | 83 | | |
65 | | - | |
| 84 | + | |
66 | 85 | | |
67 | 86 | | |
68 | 87 | | |
69 | 88 | | |
70 | 89 | | |
71 | 90 | | |
72 | 91 | | |
73 | | - | |
| 92 | + | |
74 | 93 | | |
75 | | - | |
76 | | - | |
77 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
78 | 104 | | |
79 | 105 | | |
80 | 106 | | |
81 | 107 | | |
82 | 108 | | |
83 | 109 | | |
84 | | - | |
| 110 | + | |
85 | 111 | | |
86 | 112 | | |
87 | 113 | | |
88 | 114 | | |
89 | 115 | | |
90 | 116 | | |
91 | | - | |
| 117 | + | |
| 118 | + | |
92 | 119 | | |
93 | 120 | | |
94 | 121 | | |
95 | | - | |
96 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
97 | 125 | | |
98 | 126 | | |
99 | 127 | | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
110 | 145 | | |
111 | 146 | | |
112 | 147 | | |
| |||
175 | 210 | | |
176 | 211 | | |
177 | 212 | | |
178 | | - | |
| 213 | + | |
179 | 214 | | |
180 | 215 | | |
181 | 216 | | |
182 | 217 | | |
183 | 218 | | |
184 | 219 | | |
185 | | - | |
| 220 | + | |
186 | 221 | | |
187 | 222 | | |
188 | 223 | | |
189 | 224 | | |
190 | | - | |
| 225 | + | |
191 | 226 | | |
192 | 227 | | |
193 | 228 | | |
194 | 229 | | |
195 | 230 | | |
196 | 231 | | |
197 | 232 | | |
198 | | - | |
| 233 | + | |
| 234 | + | |
199 | 235 | | |
200 | 236 | | |
201 | 237 | | |
202 | 238 | | |
203 | 239 | | |
204 | 240 | | |
205 | | - | |
206 | | - | |
| 241 | + | |
| 242 | + | |
207 | 243 | | |
208 | 244 | | |
209 | | - | |
| 245 | + | |
210 | 246 | | |
211 | 247 | | |
212 | 248 | | |
213 | 249 | | |
214 | 250 | | |
215 | 251 | | |
216 | | - | |
| 252 | + | |
217 | 253 | | |
218 | 254 | | |
219 | 255 | | |
| |||
228 | 264 | | |
229 | 265 | | |
230 | 266 | | |
231 | | - | |
| 267 | + | |
232 | 268 | | |
233 | 269 | | |
234 | 270 | | |
| |||
245 | 281 | | |
246 | 282 | | |
247 | 283 | | |
248 | | - | |
| 284 | + | |
249 | 285 | | |
250 | 286 | | |
251 | 287 | | |
| |||
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
182 | 183 | | |
183 | 184 | | |
184 | 185 | | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
185 | 199 | | |
186 | 200 | | |
187 | 201 | | |
| |||
0 commit comments