jonhoo / fantoccini

A high-level API for programmatically interacting with web pages through WebDriver.
Apache License 2.0
1.68k stars 125 forks source link

Panic when webdriver could not be reached #263

Closed spiderbiggen closed 3 months ago

spiderbiggen commented 3 months ago

I'm running fantoccini with rustls and sometimes my webdriver is temporarily unavailable/too slow too respond. Currently this results in a panic when attempting to establish a connection. Before 0.21.0 this would just result in an error and let me retry. Since 0.21.0 it instantly panics with the following exception. (This is on windows, but I've seen the same on linux)

thread 'main' panicked at [user_dir]\.cargo\registry\src\index.crates.io-6f17d22bba15001f\fantoccini-0.21.0\src\session.rs:604:17:
unexpected webdriver error; webdriver could not be reached (hyper_util): client error (Connect)
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\std\src\panicking.rs:652
   1: core::panicking::panic_fmt
             at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\core\src\panicking.rs:72
   2: fantoccini::session::Session<hyper_rustls::connector::HttpsConnector<hyper_util::client::legacy::connect::http::HttpConnector<hyper_util::client::legacy::connect::dns::GaiResolver> > >::map_handshake_response<hyper_rustls::connector::HttpsConnector<hyper_u
             at [user_dir]\.cargo\registry\src\index.crates.io-6f17d22bba15001f\fantoccini-0.21.0\src\session.rs:604
   3: core::ops::function::FnOnce::call_once<enum2$<core::result::Result<webdriver::response::NewSessionResponse,enum2$<fantoccini::error::NewSessionError> > > (*)(enum2$<core::result::Result<enum2$<serde_json::value::Value>,enum2$<fantoccini::error::CmdError> >
             at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9\library\core\src\ops\function.rs:250
   4: futures_util::fns::impl$0::call_once<enum2$<core::result::Result<webdriver::response::NewSessionResponse,enum2$<fantoccini::error::NewSessionError> > > (*)(enum2$<core::result::Result<enum2$<serde_json::value::Value>,enum2$<fantoccini::error::CmdError> > >
             at [user_dir]\.cargo\registry\src\index.crates.io-6f17d22bba15001f\futures-util-0.3.30\src\fns.rs:15
   5: futures_util::future::future::map::impl$2::poll<enum2$<fantoccini::session::impl$2::issue::async_fn_env$0<enum2$<webdriver::command::WebDriverCommand<webdriver::command::VoidWebDriverExtensionCommand> > > >,enum2$<core::result::Result<webdriver::response::
             at [user_dir]\.cargo\registry\src\index.crates.io-6f17d22bba15001f\futures-util-0.3.30\src\future\future\map.rs:57
   6: futures_util::future::future::impl$15::poll<enum2$<fantoccini::session::impl$2::issue::async_fn_env$0<enum2$<webdriver::command::WebDriverCommand<webdriver::command::VoidWebDriverExtensionCommand> > > >,enum2$<core::result::Result<webdriver::response::NewS
             at [user_dir]\.cargo\registry\src\index.crates.io-6f17d22bba15001f\futures-util-0.3.30\src\lib.rs:91
   7: fantoccini::session::impl$5::with_capabilities_and_connector::async_fn$0<hyper_rustls::connector::HttpsConnector<hyper_util::client::legacy::connect::http::HttpConnector<hyper_util::client::legacy::connect::dns::GaiResolver> > >
             at [user_dir]\.cargo\registry\src\index.crates.io-6f17d22bba15001f\fantoccini-0.21.0\src\session.rs:670
   8: fantoccini::client::impl$0::with_capabilities_and_connector::async_fn$0<hyper_rustls::connector::HttpsConnector<hyper_util::client::legacy::connect::http::HttpConnector<hyper_util::client::legacy::connect::dns::GaiResolver> > >
             at [user_dir]\.cargo\registry\src\index.crates.io-6f17d22bba15001f\fantoccini-0.21.0\src\client.rs:95
   9: fantoccini::impl$1::connect::async_fn$0<hyper_rustls::connector::HttpsConnector<hyper_util::client::legacy::connect::http::HttpConnector<hyper_util::client::legacy::connect::dns::GaiResolver> > >
             at [user_dir]\.cargo\registry\src\index.crates.io-6f17d22bba15001f\fantoccini-0.21.0\src\lib.rs:248
jonhoo commented 3 months ago

Oh interesting — this is a change from hyper underpinning us I think that makes us now hit

https://github.com/jonhoo/fantoccini/blob/a10e6fc2089bc7a3c876fdc83bb990c9777356c4/src/session.rs#L603-L605

That match needs another arm that matches on a hyper_util error

https://github.com/jonhoo/fantoccini/blob/a10e6fc2089bc7a3c876fdc83bb990c9777356c4/src/error.rs#L82-L83

And propagates it as a NewSessionError like we do for regular hyper errors

https://github.com/jonhoo/fantoccini/blob/a10e6fc2089bc7a3c876fdc83bb990c9777356c4/src/session.rs#L583

This means we'll have to add another variant to NewSessionError to bubble up errors from hyper_util

https://github.com/jonhoo/fantoccini/blob/a10e6fc2089bc7a3c876fdc83bb990c9777356c4/src/error.rs#L17-L18

I'm a bit strapped for time at the moment, but if you have a chance to put in a PR, I'd be happy to take a look!