GeezerVison is showing github.com — fix(fspy): replace the IPC file lock with an in-mapping close gate · voidzero-dev/vite-task@1d7e957
File treeExpand file treeCollapse file tree Open diff view settings fspy_preload_unix/src/client fspy_preload_windows/src/windows
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: CHANGELOG.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | # Changelog | 2 | 2 |
| | 3 | +- **Changed** Automatic file-access tracking now stops the moment a task's root process exits. Accesses made by leftover descendants afterwards are no longer recorded and never delay `vp run`, and a task that still had a traced process mid-write at that instant is conservatively not cached ([#544](https://github.com/voidzero-dev/vite-task/issues/544), [#396](https://github.com/voidzero-dev/vite-task/issues/396), [#577](https://github.com/voidzero-dev/vite-task/pull/577)). | 3 | 4 | - **Fixed** Automatic file-access tracking now backs its shared memory with a sparse temporary file on every platform, so tasks are still tracked inside coding-agent sandboxes that deny POSIX shared memory and Unix domain sockets ([#563](https://github.com/voidzero-dev/vite-task/issues/563), [#576](https://github.com/voidzero-dev/vite-task/pull/576)). | 4 | 5 | - **Added** Tasks now run with `VP_RUN=1` set, so tools can tell they are running under `vp run` instead of being invoked directly ([#570](https://github.com/voidzero-dev/vite-task/pull/570)). | 5 | 6 | - **Fixed** The task cache now supports much larger automatically tracked input sets without hitting wincode's default 4 MiB sequence preallocation limit ([#554](https://github.com/voidzero-dev/vite-task/pull/554)). | |
Collapse file Copy file name to clipboardExpand all lines: Cargo.lock -3Lines changed: 0 additions & 3 deletions Collapse file Copy file name to clipboard -34Lines changed: 0 additions & 34 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -use std::io; | 2 | | - | 3 | | -use fspy_shared::ipc::{ | 4 | | - PathAccess, | 5 | | - channel::{Receiver, ReceiverLockGuard}, | 6 | | -}; | 7 | | -use tokio::task::spawn_blocking; | 8 | | - | 9 | 1 | // Shared memory size for storing path accesses. | 10 | 2 | // 4 GiB is large enough to store path accesses in almost any realistic scenario. | 11 | 3 | // This doesn't allocate physical memory until it's actually used. | 12 | 4 | pub const SHM_CAPACITY: usize = 4 * 1024 * 1024 * 1024; | 13 | | - | 14 | | -#[ouroboros::self_referencing] | 15 | | -pub struct OwnedReceiverLockGuard { | 16 | | - /// Owns the shared memory | 17 | | - receiver: Receiver, | 18 | | - /// Borrows the shared memory and owns the file lock | 19 | | - #[borrows(receiver)] | 20 | | - #[covariant] | 21 | | - lock_guard: ReceiverLockGuard<'this>, | 22 | | -} | 23 | | - | 24 | | -impl OwnedReceiverLockGuard { | 25 | | - pub fn lock(receiver: Receiver) -> io::Result<Self> { | 26 | | - Self::try_new(receiver, fspy_shared::ipc::channel::Receiver::lock) | 27 | | - } | 28 | | - | 29 | | - pub async fn lock_async(receiver: Receiver) -> io::Result<Self> { | 30 | | - spawn_blocking(move || Self::lock(receiver)).await.expect("lock task panicked") | 31 | | - } | 32 | | - | 33 | | - pub fn iter_path_accesses(&self) -> impl Iterator<Item = PathAccess<'_>> { | 34 | | - self.borrow_lock_guard() | 35 | | - .iter_frames() | 36 | | - .map(|frame| wincode::deserialize_exact(frame).unwrap()) | 37 | | - } | 38 | | -} |
Collapse file Copy file name to clipboardExpand all lines: crates/fspy/src/unix/mod.rs +33-9Lines changed: 33 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,7 +10,10 @@ use std::{io, path::Path}; | 10 | 10 | use fspy_seccomp_unotify::supervisor::supervise; | 11 | 11 | use fspy_shared::ipc::PathAccess; | 12 | 12 | #[cfg(not(target_env = "musl"))] | 13 | | -use fspy_shared::ipc::{NativeStr, channel::channel}; | | 13 | +use fspy_shared::ipc::{ | | 14 | + NativeStr, | | 15 | + channel::{ChannelFrames, channel}, | | 16 | +}; | 14 | 17 | #[cfg(target_os = "macos")] | 15 | 18 | use fspy_shared_unix::payload::Artifacts; | 16 | 19 | use fspy_shared_unix::{ | @@ -25,7 +28,7 @@ use tokio::task::spawn_blocking; | 25 | 28 | use tokio_util::sync::CancellationToken; | 26 | 29 |
| 27 | 30 | #[cfg(not(target_env = "musl"))] | 28 | | -use crate::ipc::{OwnedReceiverLockGuard, SHM_CAPACITY}; | | 31 | +use crate::ipc::SHM_CAPACITY; | 29 | 32 | use crate::{ChildTermination, Command, TrackedChild, arena::PathAccessArena, error::SpawnError}; | 30 | 33 |
| 31 | 34 | #[derive(Debug)] | @@ -158,15 +161,24 @@ impl SpyImpl { | 158 | 161 | ); | 159 | 162 | let arenas = arenas.collect::<Vec<_>>(); | 160 | 163 |
| 161 | | - // Lock the ipc channel after the child has exited. | 162 | | - // We are not interested in path accesses from descendants after the main child has exited. | | 164 | + // Close the ipc channel now that the child has exited. We are not | | 165 | + // interested in path accesses from descendants after the main child | | 166 | + // has exited, and we do not wait for them either: closing is a | | 167 | + // single atomic operation that also fences out later writers. | | 168 | + // A close error means a traced process was still mid-write; its | | 169 | + // frames cannot be read safely, so the run is incomplete. | 163 | 170 | #[cfg(not(target_env = "musl"))] | 164 | | - let ipc_receiver_lock_guard = | 165 | | - OwnedReceiverLockGuard::lock_async(ipc_receiver).await?; | | 171 | + let (shm_frames, incomplete) = ipc_receiver | | 172 | + .close() | | 173 | + .map_or_else(|_| (None, true), |frames| (Some(frames), false)); | 166 | 174 | let path_accesses = PathAccessIterable { | 167 | 175 | arenas, | 168 | 176 | #[cfg(not(target_env = "musl"))] | 169 | | - ipc_receiver_lock_guard, | | 177 | + shm_frames, | | 178 | + #[cfg(not(target_env = "musl"))] | | 179 | + incomplete, | | 180 | + #[cfg(target_env = "musl")] | | 181 | + incomplete: false, | 170 | 182 | }; | 171 | 183 |
| 172 | 184 | io::Result::Ok(ChildTermination { status, path_accesses }) | @@ -179,8 +191,12 @@ impl SpyImpl { | 179 | 191 |
| 180 | 192 | pub struct PathAccessIterable { | 181 | 193 | arenas: Vec<PathAccessArena>, | | 194 | + /// `None` when the channel could not be frozen for reading. | 182 | 195 | #[cfg(not(target_env = "musl"))] | 183 | | - ipc_receiver_lock_guard: OwnedReceiverLockGuard, | | 196 | + shm_frames: Option<ChannelFrames>, | | 197 | + /// A traced process was still writing when the channel closed, so the | | 198 | + /// recorded accesses are not a complete picture of the run. | | 199 | + incomplete: bool, | 184 | 200 | } | 185 | 201 |
| 186 | 202 | impl PathAccessIterable { | @@ -190,12 +206,20 @@ impl PathAccessIterable { | 190 | 206 |
| 191 | 207 | #[cfg(not(target_env = "musl"))] | 192 | 208 | { | 193 | | - let accesses_in_shm = self.ipc_receiver_lock_guard.iter_path_accesses(); | | 209 | + let accesses_in_shm = | | 210 | + self.shm_frames.iter().flat_map(ChannelFrames::iter_path_accesses); | 194 | 211 | accesses_in_shm.chain(accesses_in_arena) | 195 | 212 | } | 196 | 213 | #[cfg(target_env = "musl")] | 197 | 214 | { | 198 | 215 | accesses_in_arena | 199 | 216 | } | 200 | 217 | } | | 218 | + | | 219 | + /// Whether tracking was cut short, which makes the accesses above an | | 220 | + /// incomplete record of the run. | | 221 | + #[must_use] | | 222 | + pub const fn is_incomplete(&self) -> bool { | | 223 | + self.incomplete | | 224 | + } | 201 | 225 | } |
Collapse file Copy file name to clipboardExpand all lines: crates/fspy/src/windows/mod.rs +27-11Lines changed: 27 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,7 +8,10 @@ use std::{ | 8 | 8 |
| 9 | 9 | use fspy_detours_sys::{DetourCopyPayloadToProcess, DetourUpdateProcessWithDll}; | 10 | 10 | use fspy_shared::{ | 11 | | - ipc::{PathAccess, channel::channel}, | | 11 | + ipc::{ | | 12 | + PathAccess, | | 13 | + channel::{ChannelFrames, channel}, | | 14 | + }, | 12 | 15 | windows::{PAYLOAD_ID, Payload}, | 13 | 16 | }; | 14 | 17 | use futures_util::FutureExt; | @@ -21,21 +24,29 @@ use winapi::{ | 21 | 24 | use winsafe::co::{CP, WC}; | 22 | 25 |
| 23 | 26 | use crate::{ | 24 | | - ChildTermination, TrackedChild, | 25 | | - command::Command, | 26 | | - error::SpawnError, | 27 | | - ipc::{OwnedReceiverLockGuard, SHM_CAPACITY}, | | 27 | + ChildTermination, TrackedChild, command::Command, error::SpawnError, ipc::SHM_CAPACITY, | 28 | 28 | }; | 29 | 29 |
| 30 | 30 | const INTERPOSE_CDYLIB: Artifact = artifact!("fspy_preload"); | 31 | 31 |
| 32 | 32 | pub struct PathAccessIterable { | 33 | | - ipc_receiver_lock_guard: OwnedReceiverLockGuard, | | 33 | + /// `None` when the channel could not be frozen for reading. | | 34 | + shm_frames: Option<ChannelFrames>, | | 35 | + /// A traced process was still writing when the channel closed, so the | | 36 | + /// recorded accesses are not a complete picture of the run. | | 37 | + incomplete: bool, | 34 | 38 | } | 35 | 39 |
| 36 | 40 | impl PathAccessIterable { | 37 | 41 | pub fn iter(&self) -> impl Iterator<Item = PathAccess<'_>> { | 38 | | - self.ipc_receiver_lock_guard.iter_path_accesses() | | 42 | + self.shm_frames.iter().flat_map(ChannelFrames::iter_path_accesses) | | 43 | + } | | 44 | + | | 45 | + /// Whether tracking was cut short, which makes the accesses above an | | 46 | + /// incomplete record of the run. | | 47 | + #[must_use] | | 48 | + pub const fn is_incomplete(&self) -> bool { | | 49 | + self.incomplete | 39 | 50 | } | 40 | 51 | } | 41 | 52 |
| @@ -159,10 +170,15 @@ impl SpyImpl { | 159 | 170 | child.wait().await? | 160 | 171 | } | 161 | 172 | }; | 162 | | - // Lock the ipc channel after the child has exited. | 163 | | - // We are not interested in path accesses from descendants after the main child has exited. | 164 | | - let ipc_receiver_lock_guard = OwnedReceiverLockGuard::lock_async(receiver).await?; | 165 | | - let path_accesses = PathAccessIterable { ipc_receiver_lock_guard }; | | 173 | + // Close the ipc channel now that the child has exited. We are not | | 174 | + // interested in path accesses from descendants after the main child | | 175 | + // has exited, and we do not wait for them either: closing is a | | 176 | + // single atomic operation that also fences out later writers. | | 177 | + // A close error means a traced process was still mid-write; its | | 178 | + // frames cannot be read safely, so the run is incomplete. | | 179 | + let (shm_frames, incomplete) = | | 180 | + receiver.close().map_or_else(|_| (None, true), |frames| (Some(frames), false)); | | 181 | + let path_accesses = PathAccessIterable { shm_frames, incomplete }; | 166 | 182 |
| 167 | 183 | io::Result::Ok(ChildTermination { status, path_accesses }) | 168 | 184 | }) | |
Collapse file Copy file name to clipboardExpand all lines: crates/fspy_preload_unix/src/client/mod.rs +13-4Lines changed: 13 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,7 +7,10 @@ use std::{ | 7 | 7 | }; | 8 | 8 |
| 9 | 9 | use convert::{ToAbsolutePath, ToAccessMode}; | 10 | | -use fspy_shared::ipc::{PathAccess, channel::Sender}; | | 10 | +use fspy_shared::ipc::{ | | 11 | + PathAccess, | | 12 | + channel::{ClaimError, Sender}, | | 13 | +}; | 11 | 14 | use fspy_shared_unix::{ | 12 | 15 | exec::ExecResolveConfig, | 13 | 16 | payload::EncodedPayload, | @@ -78,9 +81,15 @@ impl Client { | 78 | 81 | let frame_size = NonZeroUsize::new(serialized_size) | 79 | 82 | .expect("fspy: encoded PathAccess should never be empty"); | 80 | 83 |
| 81 | | - let mut frame = ipc_sender | 82 | | - .claim_frame(frame_size) | 83 | | - .expect("fspy: failed to claim frame in shared memory"); | | 84 | + let mut frame = match ipc_sender.claim_frame(frame_size) { | | 85 | + Ok(frame) => frame, | | 86 | + // The channel was closed because the traced root process exited. | | 87 | + // Accesses from whatever is left behind are dropped by design. | | 88 | + Err(ClaimError::Closed) => return Ok(()), | | 89 | + Err(ClaimError::Capacity) => { | | 90 | + panic!("fspy: failed to claim frame in shared memory") | | 91 | + } | | 92 | + }; | 84 | 93 | let mut writer: &mut [u8] = &mut frame; | 85 | 94 | PathAccess::serialize_into(&mut writer, &path_access)?; | 86 | 95 | assert_eq!(writer.len(), 0); | |
Collapse file Copy file name to clipboardExpand all lines: crates/fspy_preload_windows/src/windows/client.rs +11-2Lines changed: 11 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,10 @@ use std::{cell::SyncUnsafeCell, ffi::CStr, mem::MaybeUninit}; | 2 | 2 |
| 3 | 3 | use fspy_detours_sys::DetourCopyPayloadToProcess; | 4 | 4 | use fspy_shared::{ | 5 | | - ipc::{PathAccess, channel::Sender}, | | 5 | + ipc::{ | | 6 | + PathAccess, | | 7 | + channel::{ClaimError, Sender, WriteEncodedError}, | | 8 | + }, | 6 | 9 | windows::{PAYLOAD_ID, Payload}, | 7 | 10 | }; | 8 | 11 | use winapi::{shared::minwindef::BOOL, um::winnt::HANDLE}; | @@ -40,7 +43,13 @@ impl<'a> Client<'a> { | 40 | 43 | let Some(sender) = &self.ipc_sender else { | 41 | 44 | return; | 42 | 45 | }; | 43 | | - sender.write_encoded(&access).expect("failed to send path access"); | | 46 | + match sender.write_encoded(&access) { | | 47 | + Ok(()) | | 48 | + // The channel was closed because the traced root process exited. | | 49 | + // Accesses from whatever is left behind are dropped by design. | | 50 | + | Err(WriteEncodedError::Claim(ClaimError::Closed)) => {} | | 51 | + Err(err) => panic!("failed to send path access: {err:?}"), | | 52 | + } | 44 | 53 | } | 45 | 54 |
| 46 | 55 | pub unsafe fn prepare_child_process(&self, child_handle: HANDLE) -> BOOL { | |
Collapse file Copy file name to clipboardExpand all lines: crates/fspy_shared/Cargo.toml -3Lines changed: 0 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,8 +14,6 @@ bytemuck = { workspace = true, features = ["must_cast", "derive"] } | 14 | 14 | fspy_shm = { workspace = true } | 15 | 15 | native_str = { workspace = true } | 16 | 16 | thiserror = { workspace = true } | 17 | | -tracing = { workspace = true } | 18 | | -uuid = { workspace = true, features = ["v4"] } | 19 | 17 | vite_path = { workspace = true } | 20 | 18 |
| 21 | 19 | [target.'cfg(target_os = "windows")'.dependencies] | @@ -27,7 +25,6 @@ assert2 = { workspace = true } | 27 | 25 | ctor = { workspace = true } | 28 | 26 | rustc-hash = { workspace = true } | 29 | 27 | subprocess_test = { workspace = true } | 30 | | -tokio = { workspace = true, features = ["macros", "net", "rt-multi-thread", "time"] } | 31 | 28 |
| 32 | 29 | [lints] | 33 | 30 | workspace = true | |
|