|
| 1 | +From ebdd010a3858d82ba8d430e4852fae0e026e47b6 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Zanie Blue <contact@zanie.dev> |
| 3 | +Date: Thu, 5 Mar 2026 13:05:53 -0600 |
| 4 | +Subject: [PATCH] Fix file locks on Android |
| 5 | + |
| 6 | +--- |
| 7 | + crates/uv-fs/src/locked_file.rs | 85 ++++++++++++++++++++++++++++++++- |
| 8 | + 1 file changed, 83 insertions(+), 2 deletions(-) |
| 9 | + |
| 10 | +diff --git a/crates/uv-fs/src/locked_file.rs b/crates/uv-fs/src/locked_file.rs |
| 11 | +index 548692e1061e2..1098beedb9d72 100644 |
| 12 | +--- a/crates/uv-fs/src/locked_file.rs |
| 13 | ++++ b/crates/uv-fs/src/locked_file.rs |
| 14 | +@@ -98,6 +98,12 @@ pub enum LockedFileMode { |
| 15 | + impl LockedFileMode { |
| 16 | + /// Try to lock the file and return an error if the lock is already acquired by another process |
| 17 | + /// and cannot be acquired immediately. |
| 18 | ++ /// |
| 19 | ++ /// On Android, [`std::fs::File::try_lock`] is not supported |
| 20 | ++ /// (see [rust-lang/rust#148325]), so we use [`rustix::fs::flock`] directly. |
| 21 | ++ /// |
| 22 | ++ /// [rust-lang/rust#148325]: https://github.com/rust-lang/rust/issues/148325 |
| 23 | ++ #[cfg(not(target_os = "android"))] |
| 24 | + fn try_lock(self, file: &fs_err::File) -> Result<(), std::fs::TryLockError> { |
| 25 | + match self { |
| 26 | + Self::Exclusive => file.try_lock()?, |
| 27 | +@@ -106,7 +112,38 @@ impl LockedFileMode { |
| 28 | + Ok(()) |
| 29 | + } |
| 30 | + |
| 31 | ++ /// Try to lock the file and return an error if the lock is already acquired by another process |
| 32 | ++ /// and cannot be acquired immediately. |
| 33 | ++ /// |
| 34 | ++ /// Android-specific implementation using [`rustix::fs::flock`] because |
| 35 | ++ /// [`std::fs::File::try_lock`] always returns `Unsupported` on Android |
| 36 | ++ /// (see [rust-lang/rust#148325]). |
| 37 | ++ /// |
| 38 | ++ /// [rust-lang/rust#148325]: https://github.com/rust-lang/rust/issues/148325 |
| 39 | ++ #[cfg(target_os = "android")] |
| 40 | ++ fn try_lock(self, file: &fs_err::File) -> Result<(), std::fs::TryLockError> { |
| 41 | ++ use std::os::fd::AsFd; |
| 42 | ++ |
| 43 | ++ let operation = match self { |
| 44 | ++ Self::Exclusive => rustix::fs::FlockOperation::NonBlockingLockExclusive, |
| 45 | ++ Self::Shared => rustix::fs::FlockOperation::NonBlockingLockShared, |
| 46 | ++ }; |
| 47 | ++ rustix::fs::flock(file.as_fd(), operation).map_err(|errno| { |
| 48 | ++ if errno == rustix::io::Errno::WOULDBLOCK { |
| 49 | ++ std::fs::TryLockError::WouldBlock |
| 50 | ++ } else { |
| 51 | ++ std::fs::TryLockError::Error(io::Error::from_raw_os_error(errno.raw_os_error())) |
| 52 | ++ } |
| 53 | ++ }) |
| 54 | ++ } |
| 55 | ++ |
| 56 | + /// Lock the file, blocking until the lock becomes available if necessary. |
| 57 | ++ /// |
| 58 | ++ /// On Android, [`std::fs::File::lock`] is not supported |
| 59 | ++ /// (see [rust-lang/rust#148325]), so we use [`rustix::fs::flock`] directly. |
| 60 | ++ /// |
| 61 | ++ /// [rust-lang/rust#148325]: https://github.com/rust-lang/rust/issues/148325 |
| 62 | ++ #[cfg(not(target_os = "android"))] |
| 63 | + fn lock(self, file: &fs_err::File) -> Result<(), io::Error> { |
| 64 | + match self { |
| 65 | + Self::Exclusive => file.lock()?, |
| 66 | +@@ -114,6 +151,25 @@ impl LockedFileMode { |
| 67 | + } |
| 68 | + Ok(()) |
| 69 | + } |
| 70 | ++ |
| 71 | ++ /// Lock the file, blocking until the lock becomes available if necessary. |
| 72 | ++ /// |
| 73 | ++ /// Android-specific implementation using [`rustix::fs::flock`] because |
| 74 | ++ /// [`std::fs::File::lock`] always returns `Unsupported` on Android |
| 75 | ++ /// (see [rust-lang/rust#148325]). |
| 76 | ++ /// |
| 77 | ++ /// [rust-lang/rust#148325]: https://github.com/rust-lang/rust/issues/148325 |
| 78 | ++ #[cfg(target_os = "android")] |
| 79 | ++ fn lock(self, file: &fs_err::File) -> Result<(), io::Error> { |
| 80 | ++ use std::os::fd::AsFd; |
| 81 | ++ |
| 82 | ++ let operation = match self { |
| 83 | ++ Self::Exclusive => rustix::fs::FlockOperation::LockExclusive, |
| 84 | ++ Self::Shared => rustix::fs::FlockOperation::LockShared, |
| 85 | ++ }; |
| 86 | ++ rustix::fs::flock(file.as_fd(), operation) |
| 87 | ++ .map_err(|errno| io::Error::from_raw_os_error(errno.raw_os_error())) |
| 88 | ++ } |
| 89 | + } |
| 90 | + |
| 91 | + impl Display for LockedFileMode { |
| 92 | +@@ -335,13 +391,38 @@ impl LockedFile { |
| 93 | + .open(path.as_ref()) |
| 94 | + .map_err(Into::into) |
| 95 | + } |
| 96 | ++ |
| 97 | ++ /// Unlock the file. |
| 98 | ++ /// |
| 99 | ++ /// On Android, [`std::fs::File::unlock`] is not supported |
| 100 | ++ /// (see [rust-lang/rust#148325]), so we use [`rustix::fs::flock`] directly. |
| 101 | ++ /// |
| 102 | ++ /// [rust-lang/rust#148325]: https://github.com/rust-lang/rust/issues/148325 |
| 103 | ++ #[cfg(not(target_os = "android"))] |
| 104 | ++ fn unlock(&self) -> Result<(), io::Error> { |
| 105 | ++ self.0.unlock() |
| 106 | ++ } |
| 107 | ++ |
| 108 | ++ /// Unlock the file. |
| 109 | ++ /// |
| 110 | ++ /// Android-specific implementation using [`rustix::fs::flock`] because |
| 111 | ++ /// [`std::fs::File::unlock`] always returns `Unsupported` on Android |
| 112 | ++ /// (see [rust-lang/rust#148325]). |
| 113 | ++ /// |
| 114 | ++ /// [rust-lang/rust#148325]: https://github.com/rust-lang/rust/issues/148325 |
| 115 | ++ #[cfg(target_os = "android")] |
| 116 | ++ fn unlock(&self) -> Result<(), io::Error> { |
| 117 | ++ use std::os::fd::AsFd; |
| 118 | ++ |
| 119 | ++ rustix::fs::flock(self.0.as_fd(), rustix::fs::FlockOperation::Unlock) |
| 120 | ++ .map_err(|errno| io::Error::from_raw_os_error(errno.raw_os_error())) |
| 121 | ++ } |
| 122 | + } |
| 123 | + |
| 124 | + #[cfg(feature = "tokio")] |
| 125 | + impl Drop for LockedFile { |
| 126 | +- /// Unlock the file. |
| 127 | + fn drop(&mut self) { |
| 128 | +- if let Err(err) = self.0.unlock() { |
| 129 | ++ if let Err(err) = self.unlock() { |
| 130 | + error!( |
| 131 | + "Failed to unlock resource at `{}`; program may be stuck: {err}", |
| 132 | + self.0.path().display() |
0 commit comments