Skip to content

Commit

Permalink
Fixes #248
Browse files Browse the repository at this point in the history
Signed-off-by: Vrtgs <[email protected]>
  • Loading branch information
Vrtgs committed Oct 21, 2024
1 parent 56a06ec commit 9b7838d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
20 changes: 10 additions & 10 deletions thirtyfour/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "thirtyfour"
version = "0.34.1"
version = "0.34.2"
authors = ["Steve Pryde <[email protected]>", "Vrtgs"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -38,18 +38,18 @@ debug_sync_quit = []


[dependencies]
async-trait = "0.1"
async-trait = "0.1.83"
base64 = "0.22"
futures-util = { version = "0.3.30", default-features = false, features = ["alloc"] }
futures-util = { version = "0.3.31", default-features = false, features = ["alloc"] }
http = "1"
indexmap = "2"
paste = "1"
serde = { version = "1.0.209", features = ["derive", "rc"] }
serde_json = { version = "1", features = ["preserve_order"] }
serde = { version = "1.0.210", features = ["derive", "rc"] }
serde_json = { version = "1.0.132", features = ["preserve_order"] }
serde_repr = "0.1.19"
stringmatch = "0.4"
thirtyfour-macros = { path = "../thirtyfour-macros", version = "0.1.3", optional = true }
thiserror = "1.0.63"
thiserror = "1.0.64"
arc-swap = "1"
tokio = { version = "1", features = [
"rt",
Expand All @@ -59,21 +59,21 @@ tokio = { version = "1", features = [
"sync",
] }
cfg-if = "1.0.0"
bytes = "1.7.1"
bytes = "1.7.2"
tracing = "0.1"
url = "2.5.2"
const_format = "0.2.32"
const_format = "0.2.33"

# Optional HTTP client. Not needed if you supply your own.
reqwest = { version = "0.12", default-features = false, features = [
reqwest = { version = "0.12.8", default-features = false, features = [
"json",
], optional = true }

[dev-dependencies]
assert_matches = "1.5"
axum = "0.7"
color-eyre = "0.6"
rstest = { version = "0.22.0", default-features = false }
rstest = { version = "0.23.0", default-features = false }
tower-http = { version = "0.6", features = ["fs"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tokio = { version = "1", features = ["rt-multi-thread"] }
Expand Down
23 changes: 18 additions & 5 deletions thirtyfour/src/session/handle.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use reqwest::Client;

Check failure on line 1 in thirtyfour/src/session/handle.rs

View workflow job for this annotation

GitHub Actions / check

unresolved import `reqwest`
use serde_json::Value;
use std::fmt::{Debug, Display, Formatter};
use std::future::Future;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;

use serde_json::Value;
use tokio::sync::OnceCell;
use url::{ParseError, Url};

Expand Down Expand Up @@ -79,8 +79,8 @@ impl SessionHandle {
/// See `WebDriver::clone_with_config()`.
pub(crate) fn clone_with_config(self: &SessionHandle, config: WebDriverConfig) -> Self {
Self {
client: self.client.clone(),
server_url: self.server_url.clone(),
client: Arc::clone(&self.client),
server_url: Arc::clone(&self.server_url),
session_id: self.session_id.clone(),
quit: Arc::clone(&self.quit),
config,
Expand Down Expand Up @@ -1189,6 +1189,19 @@ impl Drop for SessionHandle {
std::backtrace::Backtrace::force_capture()
);

let _ = support::block_on(self.quit());
let mut this = Self {
client: self.client.clone(),
server_url: self.server_url.clone(),
session_id: self.session_id.clone(),
config: self.config.clone(),
quit: Arc::clone(&self.quit),
};
support::spawn_blocked_future(|spawned| async move {
if spawned {
// Old I/O drivers may be destroyed at this point
this.client = Arc::new(Client::new());
}
let _ = this.quit().await;
});
}
}
42 changes: 42 additions & 0 deletions thirtyfour/src/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,48 @@ where
}
}

/// Helper to run the specified future and bind it to run before runtime shutdown
/// the bool is true if it is spawned
/// else it has been spawned in place
pub fn spawn_blocked_future<Fn, F>(future: Fn)
where
Fn: FnOnce(bool) -> F,
F: Future + Send + 'static,
{
macro_rules! spawn_off {
($future: expr) => {{
let future = $future;
let (tx, rx) = std::sync::mpsc::sync_channel(0);
tokio::task::spawn_blocking(move || {
let _ = tx.send(());
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("failed to create tokio runtime")
.block_on(future);
});
rx.recv().expect("could not spawn task");
}};
}

cfg_if::cfg_if! {
if #[cfg(feature = "tokio-multi-threaded")] {
use tokio::runtime::RuntimeFlavor;

match tokio::runtime::Handle::try_current() {
Ok(handle) if handle.runtime_flavor() == RuntimeFlavor::MultiThread => {
tokio::task::block_in_place(|| {
handle.block_on(future(false))
});
}
_ => spawn_off!(future(true)),
}
} else {
spawn_off!(future(true))
}
}
}

pub(crate) async fn write_file(
path: impl AsRef<Path>,
bytes: impl Into<Vec<u8>>,
Expand Down

0 comments on commit 9b7838d

Please sign in to comment.