Commit e91ca56
authored
Fix NetBox state change race conditions with unified locking and connection limiting (#1839)
Implemented critical fixes to prevent race conditions when multiple
state changes occur simultaneously for NetBox nodes:
1. Unified Lock Keys:
- All three state-setting functions now use same lock key per device
- Previously each function used different locks allowing parallel execution
- Prevents race conditions when multiple states change in quick succession
- Fixed incorrect lock key in set_power_state (was using provision_state key)
- Lock key: lock_osism_tasks_netbox_{device_name}
2. NetBox Connection Limiting:
- Implemented custom Redis-based semaphore (pottery lacks RedisSemaphore)
- Default: maximum 5 concurrent operations per NetBox instance
- Configurable via NETBOX_MAX_CONNECTIONS environment variable
- Prevents overloading NetBox services during parallel operations
- Works for both primary and secondary NetBox instances
- Uses Redis sorted sets for distributed coordination
3. Increased Timeouts:
- Lock acquisition timeout: 20s -> 120s (2 minutes)
- Auto-release time: 60s -> 300s (5 minutes)
- Handles high-concurrency scenarios during node provisioning and sync
Technical Implementation:
- Custom RedisSemaphore class using Redis ZSET operations
- Helper function _update_netbox_device_field() wraps all NetBox updates
- Per-URL semaphore with MD5 hash-based key generation
- Automatic cleanup of expired semaphore holders (60s TTL)
AI-assisted: Claude Code
Signed-off-by: Christian Berendt <berendt@osism.tech>1 parent e8dc124 commit e91ca56
3 files changed
Lines changed: 149 additions & 39 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
18 | 40 | | |
19 | 41 | | |
20 | 42 | | |
| |||
84 | 106 | | |
85 | 107 | | |
86 | 108 | | |
87 | | - | |
88 | | - | |
| 109 | + | |
| 110 | + | |
89 | 111 | | |
90 | | - | |
| 112 | + | |
91 | 113 | | |
92 | 114 | | |
93 | 115 | | |
94 | 116 | | |
95 | 117 | | |
96 | 118 | | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
102 | 122 | | |
103 | 123 | | |
104 | 124 | | |
| |||
123 | 143 | | |
124 | 144 | | |
125 | 145 | | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
131 | 149 | | |
132 | 150 | | |
133 | 151 | | |
| |||
156 | 174 | | |
157 | 175 | | |
158 | 176 | | |
159 | | - | |
160 | | - | |
| 177 | + | |
| 178 | + | |
161 | 179 | | |
162 | | - | |
| 180 | + | |
163 | 181 | | |
164 | 182 | | |
165 | 183 | | |
166 | 184 | | |
167 | 185 | | |
168 | 186 | | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
174 | 190 | | |
175 | 191 | | |
176 | 192 | | |
| |||
195 | 211 | | |
196 | 212 | | |
197 | 213 | | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
203 | 217 | | |
204 | 218 | | |
205 | 219 | | |
| |||
232 | 246 | | |
233 | 247 | | |
234 | 248 | | |
235 | | - | |
236 | | - | |
| 249 | + | |
| 250 | + | |
237 | 251 | | |
238 | | - | |
| 252 | + | |
239 | 253 | | |
240 | 254 | | |
241 | 255 | | |
242 | 256 | | |
243 | 257 | | |
244 | 258 | | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
250 | 262 | | |
251 | 263 | | |
252 | 264 | | |
| |||
271 | 283 | | |
272 | 284 | | |
273 | 285 | | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
279 | 289 | | |
280 | 290 | | |
281 | 291 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
18 | 19 | | |
19 | 20 | | |
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 | + | |
21 | 90 | | |
22 | 91 | | |
23 | 92 | | |
| |||
291 | 360 | | |
292 | 361 | | |
293 | 362 | | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
294 | 391 | | |
295 | 392 | | |
296 | 393 | | |
| |||
0 commit comments