thirtyfour icon indicating copy to clipboard operation
thirtyfour copied to clipboard

Ensure driver.quit().await? runs on panic / graceful shutdown

Open omar238sh opened this issue 4 months ago • 1 comments

Hello,

I’m using thirtyfour in an async context with tokio. Currently, when my Rust program panics, the WebDriver session remains open and is not automatically closed.

I would like a way to ensure that:

driver.quit().await?;

is always executed, even if the program panics, so that the browser instance is properly cleaned up.

I tried using Drop, but since quit() is async, I cannot directly call .await inside Drop. My current workaround is to use tokio::runtime::Handle::current().block_on(...) inside Drop, but this feels hacky.

Example

struct DriverGuard {
    driver: Option<WebDriver>,
}

impl Drop for DriverGuard {
    fn drop(&mut self) {
        if let Some(driver) = self.driver.take() {
            let _ = tokio::runtime::Handle::current().block_on(async {
                let _ = driver.quit().await;
            });
        }
    }
}

This works, but I wonder if there is a more idiomatic solution that thirtyfour could support, for example:

  • An API for "graceful shutdown" on panic.
  • A helper guard type that implements this pattern.
  • Or exposing some way to integrate driver.quit() with panic hooks.

Question

Is there an officially recommended way to guarantee driver.quit().await? is executed when the program panics, or should I keep using a custom guard with block_on?

Thanks in advance!

omar238sh avatar Aug 26 '25 08:08 omar238sh

the WebDriver already does this, you can, and should, test this out yourself, I suspect that you may be leaking the WebDriver or leaking a handle to the WebDriver

vrtgs avatar Oct 01 '25 19:10 vrtgs