summaryrefslogtreecommitdiff
path: root/ext/io/bi_pipe.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ext/io/bi_pipe.rs')
-rw-r--r--ext/io/bi_pipe.rs43
1 files changed, 21 insertions, 22 deletions
diff --git a/ext/io/bi_pipe.rs b/ext/io/bi_pipe.rs
index 402e383ac..3492e2f44 100644
--- a/ext/io/bi_pipe.rs
+++ b/ext/io/bi_pipe.rs
@@ -2,7 +2,6 @@
use std::rc::Rc;
-use deno_core::error::AnyError;
use deno_core::AsyncRefCell;
use deno_core::AsyncResult;
use deno_core::CancelHandle;
@@ -71,13 +70,16 @@ impl BiPipeResource {
pub async fn read(
self: Rc<Self>,
data: &mut [u8],
- ) -> Result<usize, AnyError> {
+ ) -> Result<usize, std::io::Error> {
let mut rd = RcRef::map(&self, |r| &r.read_half).borrow_mut().await;
let cancel_handle = RcRef::map(&self, |r| &r.cancel);
- Ok(rd.read(data).try_or_cancel(cancel_handle).await?)
+ rd.read(data).try_or_cancel(cancel_handle).await
}
- pub async fn write(self: Rc<Self>, data: &[u8]) -> Result<usize, AnyError> {
+ pub async fn write(
+ self: Rc<Self>,
+ data: &[u8],
+ ) -> Result<usize, std::io::Error> {
let mut wr = RcRef::map(self, |r| &r.write_half).borrow_mut().await;
let nwritten = wr.write(data).await?;
wr.flush().await?;
@@ -181,9 +183,10 @@ fn from_raw(
) -> Result<(BiPipeRead, BiPipeWrite), std::io::Error> {
use std::os::fd::FromRawFd;
// Safety: The fd is part of a pair of connected sockets
- let unix_stream = tokio::net::UnixStream::from_std(unsafe {
- std::os::unix::net::UnixStream::from_raw_fd(stream)
- })?;
+ let unix_stream =
+ unsafe { std::os::unix::net::UnixStream::from_raw_fd(stream) };
+ unix_stream.set_nonblocking(true)?;
+ let unix_stream = tokio::net::UnixStream::from_std(unix_stream)?;
let (read, write) = unix_stream.into_split();
Ok((BiPipeRead { inner: read }, BiPipeWrite { inner: write }))
}
@@ -270,15 +273,15 @@ impl_async_write!(for BiPipe -> self.write_end);
/// Creates both sides of a bidirectional pipe, returning the raw
/// handles to the underlying OS resources.
-pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError>
-{
+pub fn bi_pipe_pair_raw(
+) -> Result<(RawBiPipeHandle, RawBiPipeHandle), std::io::Error> {
#[cfg(unix)]
{
// SockFlag is broken on macOS
// https://github.com/nix-rust/nix/issues/861
let mut fds = [-1, -1];
#[cfg(not(target_os = "macos"))]
- let flags = libc::SOCK_CLOEXEC | libc::SOCK_NONBLOCK;
+ let flags = libc::SOCK_CLOEXEC;
#[cfg(target_os = "macos")]
let flags = 0;
@@ -293,19 +296,19 @@ pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError
)
};
if ret != 0 {
- return Err(std::io::Error::last_os_error().into());
+ return Err(std::io::Error::last_os_error());
}
if cfg!(target_os = "macos") {
let fcntl = |fd: i32, flag: libc::c_int| -> Result<(), std::io::Error> {
// SAFETY: libc call, fd is valid
- let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
+ let flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
if flags == -1 {
return Err(fail(fds));
}
// SAFETY: libc call, fd is valid
- let ret = unsafe { libc::fcntl(fd, libc::F_SETFL, flags | flag) };
+ let ret = unsafe { libc::fcntl(fd, libc::F_SETFD, flags | flag) };
if ret == -1 {
return Err(fail(fds));
}
@@ -321,13 +324,9 @@ pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError
std::io::Error::last_os_error()
}
- // SOCK_NONBLOCK is not supported on macOS.
- (fcntl)(fds[0], libc::O_NONBLOCK)?;
- (fcntl)(fds[1], libc::O_NONBLOCK)?;
-
// SOCK_CLOEXEC is not supported on macOS.
- (fcntl)(fds[0], libc::FD_CLOEXEC)?;
- (fcntl)(fds[1], libc::FD_CLOEXEC)?;
+ fcntl(fds[0], libc::FD_CLOEXEC)?;
+ fcntl(fds[1], libc::FD_CLOEXEC)?;
}
let fd1 = fds[0];
@@ -389,7 +388,7 @@ pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError
continue;
}
- return Err(err.into());
+ return Err(err);
}
break (path, hd1);
@@ -411,7 +410,7 @@ pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError
0,
);
if hd2 == INVALID_HANDLE_VALUE {
- return Err(io::Error::last_os_error().into());
+ return Err(io::Error::last_os_error());
}
// Will not block because we have create the pair.
@@ -419,7 +418,7 @@ pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError
let err = std::io::Error::last_os_error();
if err.raw_os_error() != Some(ERROR_PIPE_CONNECTED as i32) {
CloseHandle(hd2);
- return Err(err.into());
+ return Err(err);
}
}