aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-proof / src / lock.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Exclusive advisory file locks (flock). The kernel releases a lock when its |
| 17 | //! holder exits or dies, so a crashed run can never leave a stale key behind. |
| 18 | |
| 19 | use std::fs::{self, File, OpenOptions}; |
| 20 | use std::io; |
| 21 | use std::os::unix::io::AsRawFd; |
| 22 | use std::path::Path; |
| 23 | |
| 24 | pub struct FileLock { |
| 25 | _file: File, |
| 26 | } |
| 27 | |
| 28 | fn open(path: &Path) -> io::Result<File> { |
| 29 | if let Some(parent) = path.parent() { |
| 30 | fs::create_dir_all(parent)?; |
| 31 | } |
| 32 | OpenOptions::new() |
| 33 | .create(true) |
| 34 | .read(true) |
| 35 | .write(true) |
| 36 | .truncate(false) |
| 37 | .open(path) |
| 38 | } |
| 39 | |
| 40 | impl FileLock { |
| 41 | /// Block until the lock is ours. |
| 42 | pub fn acquire(path: &Path) -> io::Result<Self> { |
| 43 | let file = open(path)?; |
| 44 | loop { |
| 45 | if unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX) } == 0 { |
| 46 | return Ok(Self { _file: file }); |
| 47 | } |
| 48 | let err = io::Error::last_os_error(); |
| 49 | if err.kind() != io::ErrorKind::Interrupted { |
| 50 | return Err(err); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /// Take the lock if it is free right now; `None` if someone else holds it. |
| 56 | pub fn try_acquire(path: &Path) -> io::Result<Option<Self>> { |
| 57 | let file = open(path)?; |
| 58 | if unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } == 0 { |
| 59 | return Ok(Some(Self { _file: file })); |
| 60 | } |
| 61 | let err = io::Error::last_os_error(); |
| 62 | if err.raw_os_error() == Some(libc::EWOULDBLOCK) { |
| 63 | Ok(None) |
| 64 | } else { |
| 65 | Err(err) |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | #[cfg(test)] |
| 71 | mod tests { |
| 72 | use super::*; |
| 73 | use crate::testutil::temp_dir; |
| 74 | |
| 75 | #[test] |
| 76 | fn second_holder_is_refused_until_release() { |
| 77 | let path = temp_dir("lock").join("k.lock"); |
| 78 | let first = FileLock::try_acquire(&path).unwrap(); |
| 79 | assert!(first.is_some()); |
| 80 | assert!(FileLock::try_acquire(&path).unwrap().is_none()); |
| 81 | drop(first); |
| 82 | // Another test forking a child between our open and its exec gives the |
| 83 | // child a copy of the lock's open file description until exec closes |
| 84 | // it (CLOEXEC), so the release can lag by microseconds. Allow that. |
| 85 | let freed = (0..100).any(|_| { |
| 86 | FileLock::try_acquire(&path).unwrap().is_some() || { |
| 87 | std::thread::sleep(std::time::Duration::from_millis(10)); |
| 88 | false |
| 89 | } |
| 90 | }); |
| 91 | assert!(freed, "lock still held 1 s after release"); |
| 92 | } |
| 93 | } |