Skip to content

Commit

Permalink
Fix borrow mut panic #204 (#205)
Browse files Browse the repository at this point in the history
* Fix borrow mut panic #204
  • Loading branch information
fafhrd91 committed May 30, 2023
1 parent efc817f commit adb74ed
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 27 deletions.
2 changes: 1 addition & 1 deletion ntex-connect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ log = "0.4"
thiserror = "1.0"

ntex-tokio = { version = "0.2.3", optional = true }
ntex-glommio = { version = "0.2.3", optional = true }
ntex-glommio = { version = "0.2.4", optional = true }
ntex-async-std = { version = "0.2.2", optional = true }

# openssl
Expand Down
4 changes: 4 additions & 0 deletions ntex-glommio/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [0.2.4] - 2023-05-30

* Fix borrow mut panic #204

## [0.2.3] - 2023-04-11

* Chore upgrade glommio to 0.8
Expand Down
2 changes: 1 addition & 1 deletion ntex-glommio/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-glommio"
version = "0.2.3"
version = "0.2.4"
authors = ["ntex contributors <team@ntex.rs>"]
description = "glommio intergration for ntex framework"
keywords = ["network", "framework", "async", "futures"]
Expand Down
59 changes: 36 additions & 23 deletions ntex-glommio/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ enum IoWriteState {
}

enum Shutdown {
None(Pin<Box<dyn Future<Output = glommio::Result<(), ()>>>>),
Flush,
Close(Pin<Box<dyn Future<Output = glommio::Result<(), ()>>>>),
Stopping(u16),
}

Expand Down Expand Up @@ -177,11 +178,7 @@ impl Future for WriteTask {
sleep(time)
};

let io = this.io.clone();
let fut = Box::pin(async move {
io.0.borrow().shutdown(std::net::Shutdown::Write).await
});
this.st = IoWriteState::Shutdown(timeout, Shutdown::None(fut));
this.st = IoWriteState::Shutdown(timeout, Shutdown::Flush);
self.poll(cx)
}
Poll::Ready(WriteStatus::Terminate) => {
Expand All @@ -199,16 +196,18 @@ impl Future for WriteTask {
// use disconnect timeout, otherwise it could hang forever.
loop {
match st {
Shutdown::None(ref mut fut) => {
Shutdown::Flush => {
// flush write buffer
let mut io = this.io.0.borrow_mut();
match this.state.with_buf(|buf| flush_io(&mut *io, buf, cx)) {
Poll::Ready(Ok(())) => {
if ready!(fut.poll(cx)).is_err() {
this.state.close(None);
return Poll::Ready(());
}
*st = Shutdown::Stopping(0);
let io = this.io.clone();
let fut = Box::pin(async move {
io.0.borrow()

Check warning on line 206 in ntex-glommio/src/io.rs

View workflow job for this annotation

GitHub Actions / clippy

this `RefCell` reference is held across an `await` point

warning: this `RefCell` reference is held across an `await` point --> ntex-glommio/src/io.rs:206:41 | 206 | ... io.0.borrow() | ^^^^^^^^^^^^^ | = help: ensure the reference is dropped before calling `await` note: these are all the `await` points this reference is held through --> ntex-glommio/src/io.rs:205:67 | 205 | ... let fut = Box::pin(async move { | _____________________________________________________^ 206 | | ... io.0.borrow() 207 | | ... .shutdown(std::net::Shutdown::Write) 208 | | ... .await 209 | | ... }); | |_______________________^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_refcell_ref = note: `#[warn(clippy::await_holding_refcell_ref)]` on by default
.shutdown(std::net::Shutdown::Write)
.await
});
*st = Shutdown::Close(fut);
continue;
}
Poll::Ready(Err(err)) => {
Expand All @@ -222,6 +221,14 @@ impl Future for WriteTask {
Poll::Pending => (),
}
}
Shutdown::Close(ref mut fut) => {
if ready!(fut.poll(cx)).is_err() {
this.state.close(None);
return Poll::Ready(());
}
*st = Shutdown::Stopping(0);
continue;
}
Shutdown::Stopping(ref mut count) => {
// read until 0 or err
let mut buf = [0u8; 512];
Expand Down Expand Up @@ -479,11 +486,7 @@ impl Future for UnixWriteTask {
sleep(time)
};

let io = this.io.clone();
let fut = Box::pin(async move {
io.0.borrow().shutdown(std::net::Shutdown::Write).await
});
this.st = IoWriteState::Shutdown(timeout, Shutdown::None(fut));
this.st = IoWriteState::Shutdown(timeout, Shutdown::Flush);
self.poll(cx)
}
Poll::Ready(WriteStatus::Terminate) => {
Expand All @@ -501,16 +504,18 @@ impl Future for UnixWriteTask {
// use disconnect timeout, otherwise it could hang forever.
loop {
match st {
Shutdown::None(ref mut fut) => {
Shutdown::Flush => {
// flush write buffer
let mut io = this.io.0.borrow_mut();
match this.state.with_buf(|buf| flush_io(&mut *io, buf, cx)) {
Poll::Ready(Ok(())) => {
if ready!(fut.poll(cx)).is_err() {
this.state.close(None);
return Poll::Ready(());
}
*st = Shutdown::Stopping(0);
let io = this.io.clone();
let fut = Box::pin(async move {
io.0.borrow()

Check warning on line 514 in ntex-glommio/src/io.rs

View workflow job for this annotation

GitHub Actions / clippy

this `RefCell` reference is held across an `await` point

warning: this `RefCell` reference is held across an `await` point --> ntex-glommio/src/io.rs:514:41 | 514 | ... io.0.borrow() | ^^^^^^^^^^^^^ | = help: ensure the reference is dropped before calling `await` note: these are all the `await` points this reference is held through --> ntex-glommio/src/io.rs:513:67 | 513 | ... let fut = Box::pin(async move { | _____________________________________________________^ 514 | | ... io.0.borrow() 515 | | ... .shutdown(std::net::Shutdown::Write) 516 | | ... .await 517 | | ... }); | |_______________________^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_refcell_ref
.shutdown(std::net::Shutdown::Write)
.await
});
*st = Shutdown::Close(fut);
continue;
}
Poll::Ready(Err(err)) => {
Expand All @@ -524,6 +529,14 @@ impl Future for UnixWriteTask {
Poll::Pending => (),
}
}
Shutdown::Close(ref mut fut) => {
if ready!(fut.poll(cx)).is_err() {
this.state.close(None);
return Poll::Ready(());
}
*st = Shutdown::Stopping(0);
continue;
}
Shutdown::Stopping(ref mut count) => {
// read until 0 or err
let mut buf = [0u8; 512];
Expand Down
2 changes: 1 addition & 1 deletion ntex-util/src/future/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Utilities for futures
use std::{future::Future, mem, pin::Pin, task::Context, task::Poll};

pub use futures_core::Stream;
pub use futures_core::{Stream, TryFuture};
pub use futures_sink::Sink;

mod either;
Expand Down
2 changes: 1 addition & 1 deletion ntex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ ntex-rt = "0.4.9"
ntex-io = "0.2.10"
ntex-tls = "0.2.4"
ntex-tokio = { version = "0.2.3", optional = true }
ntex-glommio = { version = "0.2.3", optional = true }
ntex-glommio = { version = "0.2.4", optional = true }
ntex-async-std = { version = "0.2.1", optional = true }

async-oneshot = "0.5.0"
Expand Down

0 comments on commit adb74ed

Please sign in to comment.