@@ -622,27 +622,35 @@ impl StaticExporterBuilder {
622622
623623 /// Create a new WebDriver instance based on the spawn_webdriver flag
624624 fn create_webdriver ( & self ) -> Result < WebDriver > {
625- match self . spawn_webdriver {
626- // Try to connect to existing WebDriver or spawn new if not available
627- true => WebDriver :: connect_or_spawn ( self . webdriver_port ) ,
628- // Create the WebDriver instance without spawning
629- false => WebDriver :: new ( self . webdriver_port ) ,
630- }
625+ let port = self . webdriver_port ;
626+ let in_async = tokio:: runtime:: Handle :: try_current ( ) . is_ok ( ) ;
627+
628+ let run_create_fn = |spawn : bool | -> Result < WebDriver > {
629+ let work = move || {
630+ if spawn {
631+ WebDriver :: connect_or_spawn ( port)
632+ } else {
633+ WebDriver :: new ( port)
634+ }
635+ } ;
636+ if in_async {
637+ std:: thread:: spawn ( work)
638+ . join ( )
639+ . map_err ( |_| anyhow ! ( "failed to join webdriver thread" ) ) ?
640+ } else {
641+ work ( )
642+ }
643+ } ;
644+
645+ run_create_fn ( self . spawn_webdriver )
631646 }
632647}
633648
634649// Async builder for async-first exporter (added without reordering existing items)
635650impl StaticExporterBuilder {
636651 /// Build an async exporter for use within async contexts.
637652 pub fn build_async ( & self ) -> Result < AsyncStaticExporter > {
638- let wd = if self . spawn_webdriver {
639- let port = self . webdriver_port ;
640- std:: thread:: spawn ( move || WebDriver :: connect_or_spawn ( port) )
641- . join ( )
642- . map_err ( |_| anyhow ! ( "failed to join webdriver spawn thread" ) ) ??
643- } else {
644- WebDriver :: new ( self . webdriver_port ) ?
645- } ;
653+ let wd = self . create_webdriver ( ) ?;
646654 Ok ( AsyncStaticExporter {
647655 webdriver_port : self . webdriver_port ,
648656 webdriver_url : self . webdriver_url . clone ( ) ,
@@ -760,6 +768,13 @@ impl StaticExporter {
760768 height : usize ,
761769 scale : f64 ,
762770 ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
771+ if tokio:: runtime:: Handle :: try_current ( ) . is_ok ( ) {
772+ return Err ( anyhow ! (
773+ "StaticExporter sync methods cannot be used inside an async context. \
774+ Use StaticExporterBuilder::build_async() and the associated AsyncStaticExporter::write_fig(...)."
775+ )
776+ . into ( ) ) ;
777+ }
763778 let rt = self . runtime . clone ( ) ;
764779 rt. block_on (
765780 self . inner
@@ -818,6 +833,13 @@ impl StaticExporter {
818833 height : usize ,
819834 scale : f64 ,
820835 ) -> Result < String , Box < dyn std:: error:: Error > > {
836+ if tokio:: runtime:: Handle :: try_current ( ) . is_ok ( ) {
837+ return Err ( anyhow ! (
838+ "StaticExporter sync methods cannot be used inside an async context. \
839+ Use StaticExporterBuilder::build_async() and the associated AsyncStaticExporter::write_to_string(...)."
840+ )
841+ . into ( ) ) ;
842+ }
821843 let rt = self . runtime . clone ( ) ;
822844 rt. block_on (
823845 self . inner
@@ -932,14 +954,8 @@ impl StaticExporter {
932954 /// Explicitly close the WebDriver session and stop the driver.
933955 /// Prefer calling this in long-running applications to ensure deterministic cleanup.
934956 pub fn close ( & mut self ) {
935- if let Some ( client) = self . inner . webdriver_client . take ( ) {
936- let runtime = self . runtime . clone ( ) ;
937- runtime. block_on ( async {
938- if let Err ( e) = client. close ( ) . await {
939- error ! ( "Failed to close WebDriver client: {e}" ) ;
940- }
941- } ) ;
942- }
957+ let runtime = self . runtime . clone ( ) ;
958+ runtime. block_on ( self . inner . close ( ) ) ;
943959 }
944960}
945961
0 commit comments