heddle-objects 0.3.1

An AI-native version control system
Documentation
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// SPDX-License-Identifier: Apache-2.0
//! Repository locking for concurrent access.
//!
//! [`RepoLock`] guarantees three invariants:
//! - **Cross-process** exclusion via `flock(2)` on a lock file.
//! - **Cross-thread, same-process** exclusion: two threads never both hold the
//!   write lock.
//! - **Same-thread reentrancy**: the owning thread may re-acquire the write lock
//!   any number of times without blocking.
//!
//! The reentrancy invariant matters because `flock(2)` locks attach to the open
//! file description, not the process: a single thread that opens the lock file
//! twice and calls `flock` on the second fd blocks forever on its own first
//! lock. The canonical write lock is taken at the top of an import and then
//! re-taken by downstream writers on the same thread, so a non-reentrant
//! primitive self-deadlocks. We therefore hold the `flock` once on the outermost
//! acquisition and gate intra-process access through a per-lock-path registry.

use std::{
    collections::HashMap,
    fs::File,
    io,
    path::{Path, PathBuf},
    sync::{Arc, Condvar, Mutex, MutexGuard, OnceLock},
    thread::{self, ThreadId},
};

use fs2::FileExt;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum LockError {
    #[error("failed to acquire lock: {0}")]
    Acquire(#[source] io::Error),
    #[error("lock file not accessible: {0}")]
    Io(#[source] io::Error),
}

pub type Result<T> = std::result::Result<T, LockError>;

/// Intra-process state for a single lock path. `flock` holds the OS-level lock
/// file while owned; dropping it releases the cross-process lock.
struct GateState {
    owner: Option<ThreadId>,
    depth: usize,
    flock: Option<File>,
}

struct Entry {
    gate: Mutex<GateState>,
    cv: Condvar,
}

impl Entry {
    fn new() -> Self {
        Self {
            gate: Mutex::new(GateState {
                owner: None,
                depth: 0,
                flock: None,
            }),
            cv: Condvar::new(),
        }
    }
}

/// Process-global registry of per-lock-path gates, keyed by the canonical lock
/// path. Entries are created on first use and never removed (one small entry per
/// distinct lock path per process lifetime).
static REGISTRY: OnceLock<Mutex<HashMap<PathBuf, Arc<Entry>>>> = OnceLock::new();

fn registry() -> &'static Mutex<HashMap<PathBuf, Arc<Entry>>> {
    REGISTRY.get_or_init(|| Mutex::new(HashMap::new()))
}

fn entry_for(key: PathBuf) -> Arc<Entry> {
    let mut map = registry().lock().unwrap_or_else(|e| e.into_inner());
    Arc::clone(map.entry(key).or_insert_with(|| Arc::new(Entry::new())))
}

fn lock_gate(entry: &Entry) -> MutexGuard<'_, GateState> {
    entry.gate.lock().unwrap_or_else(|e| e.into_inner())
}

pub struct ReadLockGuard {
    // `None` when this is a no-op guard: the current thread already holds the
    // write lock, whose exclusive flock subsumes a shared read.
    _file: Option<File>,
}

impl Drop for ReadLockGuard {
    fn drop(&mut self) {
        if let Some(file) = &self._file {
            let _ = file.unlock();
        }
    }
}

pub struct WriteLockGuard {
    entry: Arc<Entry>,
}

impl Drop for WriteLockGuard {
    fn drop(&mut self) {
        let mut state = lock_gate(&self.entry);
        if state.depth > 0 {
            state.depth -= 1;
        }
        if state.depth == 0 {
            state.owner = None;
            // Dropping the File releases the cross-process flock.
            state.flock = None;
            self.entry.cv.notify_one();
        }
    }
}

pub struct RepoLock {
    lock_path: PathBuf,
}

impl RepoLock {
    pub fn new(repo_root: &Path) -> Self {
        let lock_path = repo_root.join(".heddle/locks/repo.lock");
        Self { lock_path }
    }

    pub fn at(lock_path: PathBuf) -> Self {
        Self { lock_path }
    }

    pub fn read(&self) -> Result<ReadLockGuard> {
        self.ensure_lock_dir()?;
        let entry = entry_for(self.registry_key());

        // If the current thread already holds the write lock, the exclusive
        // flock covers this read; hand back a no-op guard so a same-thread
        // read-under-write cannot deadlock against our own flock.
        {
            let state = lock_gate(&entry);
            if state.owner == Some(thread::current().id()) {
                return Ok(ReadLockGuard { _file: None });
            }
        }

        let file = self.open_lock_file()?;
        file.lock_shared().map_err(LockError::Acquire)?;
        Ok(ReadLockGuard { _file: Some(file) })
    }

    pub fn write(&self) -> Result<WriteLockGuard> {
        self.ensure_lock_dir()?;
        let entry = entry_for(self.registry_key());
        let tid = thread::current().id();
        let mut state = lock_gate(&entry);
        loop {
            match state.owner {
                Some(owner) if owner == tid => {
                    state.depth += 1;
                    return Ok(WriteLockGuard {
                        entry: Arc::clone(&entry),
                    });
                }
                None => {
                    // Acquire the cross-process flock once for the outermost
                    // holder. Holding the gate across this blocking call is
                    // intentional: other local threads must block here until we
                    // either win the flock or fail.
                    let file = self.open_lock_file()?;
                    file.lock_exclusive().map_err(LockError::Acquire)?;
                    state.owner = Some(tid);
                    state.depth = 1;
                    state.flock = Some(file);
                    return Ok(WriteLockGuard {
                        entry: Arc::clone(&entry),
                    });
                }
                Some(_) => {
                    state = entry.cv.wait(state).unwrap_or_else(|e| e.into_inner());
                }
            }
        }
    }

    pub fn try_read(&self) -> Result<Option<ReadLockGuard>> {
        self.ensure_lock_dir()?;
        let file = self.open_lock_file()?;

        match file.try_lock_shared() {
            Ok(()) => Ok(Some(ReadLockGuard { _file: Some(file) })),
            Err(_) => Ok(None),
        }
    }

    pub fn try_write(&self) -> Result<Option<WriteLockGuard>> {
        self.ensure_lock_dir()?;
        let entry = entry_for(self.registry_key());
        let mut state = lock_gate(&entry);
        // Non-blocking acquisition is NON-reentrant: a `try_write` while the lock
        // is held — by ANY thread, including this one — reports contention
        // (`None`). Reentrancy exists only to keep the BLOCKING `write()` from
        // self-deadlocking on its own `flock`; a `try_*` can never deadlock, so a
        // caller that uses it to detect contention (e.g. the undo/redo
        // serialization lock, heddle#355) must see "held" regardless of holder.
        match state.owner {
            Some(_) => Ok(None),
            None => {
                let file = self.open_lock_file()?;
                match file.try_lock_exclusive() {
                    Ok(()) => {
                        state.owner = Some(thread::current().id());
                        state.depth = 1;
                        state.flock = Some(file);
                        Ok(Some(WriteLockGuard {
                            entry: Arc::clone(&entry),
                        }))
                    }
                    Err(_) => Ok(None),
                }
            }
        }
    }

    fn ensure_lock_dir(&self) -> Result<()> {
        if let Some(parent) = self.lock_path.parent() {
            std::fs::create_dir_all(parent).map_err(LockError::Io)?;
        }
        Ok(())
    }

    /// Stable registry key for this lock path. The lock file itself may not exist
    /// yet, so canonicalize the (already-created) parent directory and re-join
    /// the filename rather than the whole path.
    fn registry_key(&self) -> PathBuf {
        match self.lock_path.parent() {
            Some(parent) => {
                let canon_parent = parent
                    .canonicalize()
                    .unwrap_or_else(|_| parent.to_path_buf());
                match self.lock_path.file_name() {
                    Some(name) => canon_parent.join(name),
                    None => canon_parent,
                }
            }
            None => self.lock_path.clone(),
        }
    }

    fn open_lock_file(&self) -> Result<File> {
        File::create(&self.lock_path).map_err(LockError::Io)
    }
}

pub trait RepositoryLockExt {
    fn locker(&self) -> RepoLock;
}

#[cfg(test)]
mod tests {
    use std::{
        sync::{
            Arc,
            mpsc::{self},
        },
        thread,
    };

    use tempfile::TempDir;

    use super::*;

    #[test]
    fn test_read_lock_acquired() {
        let temp = TempDir::new().unwrap();
        let lock = RepoLock::new(temp.path());

        let guard = lock.read().unwrap();
        assert!(std::mem::size_of_val(&guard) > 0);
    }

    #[test]
    fn test_write_lock_acquired() {
        let temp = TempDir::new().unwrap();
        let lock = RepoLock::new(temp.path());

        let guard = lock.write().unwrap();
        assert!(std::mem::size_of_val(&guard) > 0);
    }

    #[test]
    fn test_multiple_readers() {
        let temp = TempDir::new().unwrap();
        let lock = Arc::new(RepoLock::new(temp.path()));

        let mut handles = vec![];
        for _ in 0..10 {
            let lock = Arc::clone(&lock);
            let handle = thread::spawn(move || {
                let _guard = lock.read().unwrap();
                thread::sleep(std::time::Duration::from_millis(10));
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }
    }

    #[test]
    fn test_writer_excludes_reader() {
        let temp = TempDir::new().unwrap();
        let lock = Arc::new(RepoLock::new(temp.path()));

        let _write_guard = lock.write().unwrap();
        let read_result = lock.try_read().unwrap();
        assert!(read_result.is_none(), "Reader should be blocked by writer");
    }

    #[test]
    fn test_reader_excludes_writer() {
        let temp = TempDir::new().unwrap();
        let lock = Arc::new(RepoLock::new(temp.path()));

        let _read_guard = lock.read().unwrap();
        let write_result = lock.try_write().unwrap();
        assert!(write_result.is_none(), "Writer should be blocked by reader");
    }

    #[test]
    fn test_lock_released_on_drop() {
        let temp = TempDir::new().unwrap();
        let lock = RepoLock::new(temp.path());

        {
            let _guard = lock.write().unwrap();
        }

        let _guard2 = lock.read().unwrap();
    }

    /// The owning thread may re-take the write lock without blocking on its own
    /// flock — the regression that self-deadlocked the canonical import lock.
    #[test]
    fn same_thread_write_is_reentrant() {
        let temp = TempDir::new().unwrap();
        let lock = RepoLock::new(temp.path());

        let _a = lock.write().unwrap();
        let _b = lock.write().unwrap();
        // Reaching here without hanging is the assertion (harness timeout is the
        // backstop on regression).
    }

    /// A read taken by the thread that already holds the write lock must not
    /// block against its own exclusive flock.
    #[test]
    fn same_thread_read_under_write_does_not_deadlock() {
        let temp = TempDir::new().unwrap();
        let lock = RepoLock::new(temp.path());

        let _w = lock.write().unwrap();
        let _r = lock.read().unwrap();
    }

    /// Reentrancy is strictly per-thread: while one thread holds the write lock,
    /// a different thread is excluded.
    #[test]
    fn distinct_threads_still_exclude() {
        let temp = TempDir::new().unwrap();
        let lock = Arc::new(RepoLock::new(temp.path()));

        let (acquired_tx, acquired_rx) = mpsc::channel();
        let (release_tx, release_rx) = mpsc::channel();
        let lock_a = Arc::clone(&lock);
        let handle = thread::spawn(move || {
            let _g = lock_a.write().unwrap();
            acquired_tx.send(()).unwrap();
            release_rx.recv().unwrap();
        });

        acquired_rx.recv().unwrap();
        assert!(
            lock.try_write().unwrap().is_none(),
            "a second thread must not acquire the write lock"
        );

        release_tx.send(()).unwrap();
        handle.join().unwrap();

        assert!(
            lock.try_write().unwrap().is_some(),
            "write lock is available once the owning thread releases"
        );
    }

    /// A reentrant (depth > 1) hold keeps the lock until the OUTERMOST guard
    /// drops; other threads stay excluded across the inner drops.
    #[test]
    fn reentrant_release_keeps_lock_until_outermost_drop() {
        let temp = TempDir::new().unwrap();
        let lock = Arc::new(RepoLock::new(temp.path()));

        let a1 = lock.write().unwrap();
        let a2 = lock.write().unwrap();

        let other = |lock: &Arc<RepoLock>| {
            let lock = Arc::clone(lock);
            thread::spawn(move || lock.try_write().unwrap().is_none())
                .join()
                .unwrap()
        };

        assert!(other(&lock), "excluded while held at depth 2");
        drop(a2);
        assert!(other(&lock), "still excluded while held at depth 1");
        drop(a1);

        let lock_b = Arc::clone(&lock);
        let now_available = thread::spawn(move || lock_b.try_write().unwrap().is_some())
            .join()
            .unwrap();
        assert!(now_available, "available after the outermost guard drops");
    }

    /// `try_write` is intentionally NON-reentrant: even the thread that already
    /// holds the write lock gets `None`, not a nested guard. Reentrancy exists
    /// only so the BLOCKING `write()` can't self-deadlock on its own `flock`; a
    /// non-blocking `try_*` can never deadlock, and callers use it to DETECT
    /// contention (the undo/redo serialization lock, heddle#355), so it must
    /// report "held" regardless of holder. Do NOT "fix" this to mirror
    /// `write()`'s reentrancy.
    #[test]
    fn try_write_is_non_reentrant_even_for_owner() {
        let temp = TempDir::new().unwrap();
        let lock = RepoLock::new(temp.path());

        let _held = lock.write().unwrap();
        assert!(
            lock.try_write().unwrap().is_none(),
            "try_write must report contention even for the lock's own owner thread"
        );
    }
}