diff options
author | Igor Zinkovsky <igor@deno.com> | 2023-04-28 12:16:17 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-28 12:16:17 -0700 |
commit | 10ae5ee26557107b22524b1a84ebb56ed7d23fb4 (patch) | |
tree | fb5388dff1531a6ac692bb52387697adc39d14b3 | |
parent | 6369098ad74a263e461478f94782bc469de15468 (diff) |
fix(ext/io) several sync fs fixes (#18886)
2 fixes related to sync fs:
* update the 2 sync methods on `Resource` trait to take `Rc<Self>`
(consistent with other methods)
* fix a bug in `StdFileResource::with_inner_and_metadata`, which
currently can trigger a panic if a sync method is called on a file with
a pending async operation. This could happen in the code path where
`File::try_clone`
[fails](https://github.com/denoland/deno/blob/39ece1fe0ddacc2cbf182403c9e7085bc01df5a6/ext/io/lib.rs#L485-L489).
-rw-r--r-- | core/resources.rs | 4 | ||||
-rw-r--r-- | ext/io/lib.rs | 15 |
2 files changed, 11 insertions, 8 deletions
diff --git a/core/resources.rs b/core/resources.rs index 6ca86e10b..84e6847fc 100644 --- a/core/resources.rs +++ b/core/resources.rs @@ -155,13 +155,13 @@ pub trait Resource: Any + 'static { } /// The same as [`read_byob()`][Resource::read_byob], but synchronous. - fn read_byob_sync(&self, data: &mut [u8]) -> Result<usize, Error> { + fn read_byob_sync(self: Rc<Self>, data: &mut [u8]) -> Result<usize, Error> { _ = data; Err(not_supported()) } /// The same as [`write()`][Resource::write], but synchronous. - fn write_sync(&self, data: &[u8]) -> Result<usize, Error> { + fn write_sync(self: Rc<Self>, data: &[u8]) -> Result<usize, Error> { _ = data; Err(not_supported()) } diff --git a/ext/io/lib.rs b/ext/io/lib.rs index c85b4baf6..23c087e16 100644 --- a/ext/io/lib.rs +++ b/ext/io/lib.rs @@ -461,13 +461,13 @@ impl StdFileResource { ) -> Result<TResult, E>, ) -> Option<Result<TResult, E>> { match self.cell.try_borrow_mut() { - Ok(mut cell) => { + Ok(mut cell) if cell.is_some() => { let mut file = cell.take().unwrap(); let result = action(&mut file.inner, &file.meta_data); cell.replace(file); Some(result) } - Err(_) => None, + _ => None, } } @@ -537,14 +537,14 @@ impl StdFileResource { .await } - fn read_byob_sync(&self, buf: &mut [u8]) -> Result<usize, AnyError> { + fn read_byob_sync(self: Rc<Self>, buf: &mut [u8]) -> Result<usize, AnyError> { self .with_inner_and_metadata(|inner, _| inner.read(buf)) .ok_or_else(resource_unavailable)? .map_err(Into::into) } - fn write_sync(&self, data: &[u8]) -> Result<usize, AnyError> { + fn write_sync(self: Rc<Self>, data: &[u8]) -> Result<usize, AnyError> { self .with_inner_and_metadata(|inner, _| inner.write_and_maybe_flush(data)) .ok_or_else(resource_unavailable)? @@ -694,12 +694,15 @@ impl Resource for StdFileResource { Box::pin(StdFileResource::write_all(self, view)) } - fn write_sync(&self, data: &[u8]) -> Result<usize, deno_core::anyhow::Error> { + fn write_sync( + self: Rc<Self>, + data: &[u8], + ) -> Result<usize, deno_core::anyhow::Error> { StdFileResource::write_sync(self, data) } fn read_byob_sync( - &self, + self: Rc<Self>, data: &mut [u8], ) -> Result<usize, deno_core::anyhow::Error> { StdFileResource::read_byob_sync(self, data) |