Commit 04dfeae
[EPIC-3-2] feat(sync): add stale lock detection to file locking mechanism
- 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 04dfeae
2 files changed
Lines changed: 84 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 | |
|---|---|---|---|
| |||
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
167 | 174 | | |
168 | 175 | | |
169 | 176 | | |
| |||
200 | 207 | | |
201 | 208 | | |
202 | 209 | | |
| 210 | + | |
| 211 | + | |
| 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 | + | |
203 | 252 | | |
204 | 253 | | |
205 | 254 | | |
| |||
316 | 365 | | |
317 | 366 | | |
318 | 367 | | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
319 | 399 | | |
0 commit comments