Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: TimeoutLayer doesn't work as expected for writer #4330

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 61 additions & 19 deletions core/src/layers/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub struct TimeoutAccessor<A: Accessor> {
}

impl<A: Accessor> TimeoutAccessor<A> {
async fn timeout<F: Future<Output = Result<T>>, T>(&self, op: Operation, fut: F) -> Result<T> {
async fn timeout<F: Future<Output=Result<T>>, T>(&self, op: Operation, fut: F) -> Result<T> {
tokio::time::timeout(self.timeout, fut).await.map_err(|_| {
Error::new(ErrorKind::Unexpected, "operation timeout reached")
.with_operation(op)
Expand All @@ -174,7 +174,7 @@ impl<A: Accessor> TimeoutAccessor<A> {
})?
}

async fn io_timeout<F: Future<Output = Result<T>>, T>(
async fn io_timeout<F: Future<Output=Result<T>>, T>(
&self,
op: Operation,
fut: F,
Expand All @@ -191,7 +191,7 @@ impl<A: Accessor> TimeoutAccessor<A> {
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(target_arch = "wasm32", async_trait(? Send))]
impl<A: Accessor> LayeredAccessor for TimeoutAccessor<A> {
type Inner = A;
type Reader = TimeoutWrapper<A::Reader>;
Expand Down Expand Up @@ -334,28 +334,70 @@ impl<R: oio::Read> oio::Read for TimeoutWrapper<R> {
}

impl<R: oio::Write> oio::Write for TimeoutWrapper<R> {
fn poll_write(&mut self, cx: &mut Context<'_>, bs: &dyn oio::WriteBuf) -> Poll<Result<usize>> {
self.poll_timeout(cx, WriteOperation::Write.into_static())?;
async fn poll_write(&mut self, cx: &mut Context<'_>, bs: &dyn oio::WriteBuf) -> Poll<Result<usize>> {
let sleep = self
.sleep
.get_or_insert_with(|| Box::pin(tokio::time::sleep(self.timeout)));

let v = ready!(self.inner.poll_write(cx, bs));
self.sleep = None;
Poll::Ready(v)
tokio::select! {
_ = sleep.as_mut() => {
self.sleep = None;
Ready(Err(
Error::new(ErrorKind::Unexpected, "io operation timeout reached")
.with_operation(WriteOperation::Write.into_static())
.with_context("io_timeout", self.timeout.as_secs_f64().to_string())
.set_temporary(),
))
}
result = self.inner.poll_write(cx, bs) => {
self.sleep = None;
Ready(result)
}
}
}

fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Result<()>> {
self.poll_timeout(cx, WriteOperation::Close.into_static())?;
async fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Result<()>> {
let sleep = self
.sleep
.get_or_insert_with(|| Box::pin(tokio::time::sleep(self.timeout)));

let v = ready!(self.inner.poll_close(cx));
self.sleep = None;
Poll::Ready(v)
tokio::select! {
_ = sleep.as_mut() => {
self.sleep = None;
Ready(Err(
Error::new(ErrorKind::Unexpected, "io operation timeout reached")
.with_operation(WriteOperation::Write.into_static())
.with_context("io_timeout", self.timeout.as_secs_f64().to_string())
.set_temporary(),
))
}
result = self.inner.poll_close(cx) => {
self.sleep = None;
Ready(result)
}
}
}

fn poll_abort(&mut self, cx: &mut Context<'_>) -> Poll<Result<()>> {
self.poll_timeout(cx, WriteOperation::Abort.into_static())?;
async fn poll_abort(&mut self, cx: &mut Context<'_>) -> Poll<Result<()>> {
let sleep = self
.sleep
.get_or_insert_with(|| Box::pin(tokio::time::sleep(self.timeout)));

let v = ready!(self.inner.poll_abort(cx));
self.sleep = None;
Poll::Ready(v)
tokio::select! {
_ = sleep.as_mut() => {
self.sleep = None;
Ready(Err(
Error::new(ErrorKind::Unexpected, "io operation timeout reached")
.with_operation(WriteOperation::Write.into_static())
.with_context("io_timeout", self.timeout.as_secs_f64().to_string())
.set_temporary(),
))
}
result = self.inner.poll_abort(cx) => {
self.sleep = None;
Ready(result)
}
}
}
}

Expand Down Expand Up @@ -393,7 +435,7 @@ mod tests {
struct MockService;

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(target_arch = "wasm32", async_trait(? Send))]
impl Accessor for MockService {
type Reader = MockReader;
type Writer = ();
Expand Down
Loading