Skip to content

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

@omar238sh

Description

@omar238sh

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions