Skip to content

Commit 10ae5ee

Browse files
author
Igor Zinkovsky
authored
fix(ext/io) several sync fs fixes (denoland#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).
1 parent 6369098 commit 10ae5ee

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

core/resources.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,13 @@ pub trait Resource: Any + 'static {
155155
}
156156

157157
/// The same as [`read_byob()`][Resource::read_byob], but synchronous.
158-
fn read_byob_sync(&self, data: &mut [u8]) -> Result<usize, Error> {
158+
fn read_byob_sync(self: Rc<Self>, data: &mut [u8]) -> Result<usize, Error> {
159159
_ = data;
160160
Err(not_supported())
161161
}
162162

163163
/// The same as [`write()`][Resource::write], but synchronous.
164-
fn write_sync(&self, data: &[u8]) -> Result<usize, Error> {
164+
fn write_sync(self: Rc<Self>, data: &[u8]) -> Result<usize, Error> {
165165
_ = data;
166166
Err(not_supported())
167167
}

ext/io/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -461,13 +461,13 @@ impl StdFileResource {
461461
) -> Result<TResult, E>,
462462
) -> Option<Result<TResult, E>> {
463463
match self.cell.try_borrow_mut() {
464-
Ok(mut cell) => {
464+
Ok(mut cell) if cell.is_some() => {
465465
let mut file = cell.take().unwrap();
466466
let result = action(&mut file.inner, &file.meta_data);
467467
cell.replace(file);
468468
Some(result)
469469
}
470-
Err(_) => None,
470+
_ => None,
471471
}
472472
}
473473

@@ -537,14 +537,14 @@ impl StdFileResource {
537537
.await
538538
}
539539

540-
fn read_byob_sync(&self, buf: &mut [u8]) -> Result<usize, AnyError> {
540+
fn read_byob_sync(self: Rc<Self>, buf: &mut [u8]) -> Result<usize, AnyError> {
541541
self
542542
.with_inner_and_metadata(|inner, _| inner.read(buf))
543543
.ok_or_else(resource_unavailable)?
544544
.map_err(Into::into)
545545
}
546546

547-
fn write_sync(&self, data: &[u8]) -> Result<usize, AnyError> {
547+
fn write_sync(self: Rc<Self>, data: &[u8]) -> Result<usize, AnyError> {
548548
self
549549
.with_inner_and_metadata(|inner, _| inner.write_and_maybe_flush(data))
550550
.ok_or_else(resource_unavailable)?
@@ -694,12 +694,15 @@ impl Resource for StdFileResource {
694694
Box::pin(StdFileResource::write_all(self, view))
695695
}
696696

697-
fn write_sync(&self, data: &[u8]) -> Result<usize, deno_core::anyhow::Error> {
697+
fn write_sync(
698+
self: Rc<Self>,
699+
data: &[u8],
700+
) -> Result<usize, deno_core::anyhow::Error> {
698701
StdFileResource::write_sync(self, data)
699702
}
700703

701704
fn read_byob_sync(
702-
&self,
705+
self: Rc<Self>,
703706
data: &mut [u8],
704707
) -> Result<usize, deno_core::anyhow::Error> {
705708
StdFileResource::read_byob_sync(self, data)

0 commit comments

Comments
 (0)