Commit 6a3fd6f
[EPIC-3-2] feat(sync): add stale lock detection to file locking mechanism (#26)
- What: Enhanced FileLock with stale lock detection and automatic cleanup
- Why: Prevents deadlocks from abandoned locks when processes crash (Issue #13)
- How: Added is_stale() and force_unlock() methods with 5-minute threshold
Implementation:
- Added is_stale() private method to detect locks older than 5 minutes
- Added force_unlock() private method to remove stale lock files
- Enhanced acquire() to automatically detect and clean stale locks before timeout
- Locks are now recovered automatically without manual intervention
Testing:
- Added test_stale_lock_cleanup() using filetime crate
- Total test suite: 6 tests (90% → 100% of acceptance criteria)
- Coverage target: 80%+ (sync-service standard)
Dependencies:
- Added filetime = "0.2" to dev-dependencies for timestamp testing
- All existing dependencies (fs2, anyhow, tempfile) already present
Files modified:
- sync-service/src/vault/lock.rs (~40 lines added):
- Lines 224-237: is_stale() method
- Lines 246-252: force_unlock() method
- Lines 167-171: Stale lock check in acquire()
- Lines 369-398: test_stale_lock_cleanup() test
- sync-service/Cargo.toml (2 lines added):
- Added [dev-dependencies] section
- Added filetime = "0.2" for testing
Acceptance criteria addressed:
- [x] FileLock with RAII pattern (existing)
- [x] acquire() with timeout (existing)
- [x] release() for cleanup (existing)
- [x] Drop implementation (existing)
- [x] Stale lock detection (NEW - 5-minute threshold)
- [x] Automatic cleanup of abandoned locks (NEW)
- [x] Lock files in .noosphere/locks/ (existing)
- [x] fs2, anyhow, tempfile dependencies (existing)
- [x] filetime dev-dependency (NEW)
- [x] 6+ comprehensive tests (5 existing + 1 new)
- [x] Integration-ready for vault writer/parser
Design decisions:
- 5-minute stale threshold: Balances quick recovery vs false positives
- Private methods: is_stale() and force_unlock() are implementation details
- Automatic in acquire(): Transparent to consumers, no API changes
- SystemTime for age calculation: Standard Rust approach for file timestamps
Related to EPIC-3 (Vault Structure and File Utilities) #13
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent 4d0ca57 commit 6a3fd6f
2 files changed
Lines changed: 93 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
39 | 42 | | |
40 | 43 | | |
41 | 44 | | |
| |||
164 | 167 | | |
165 | 168 | | |
166 | 169 | | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
167 | 177 | | |
168 | 178 | | |
169 | 179 | | |
| |||
200 | 210 | | |
201 | 211 | | |
202 | 212 | | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
203 | 261 | | |
204 | 262 | | |
205 | 263 | | |
| |||
316 | 374 | | |
317 | 375 | | |
318 | 376 | | |
| 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 | + | |
| 407 | + | |
319 | 408 | | |
0 commit comments