@@ -133,46 +133,53 @@ impl ChartState {
133133 runtime : & dyn VegaFusionRuntimeTrait ,
134134 updates : Vec < ExportUpdateJSON > ,
135135 ) -> Result < Vec < ExportUpdateJSON > > {
136- let mut task_graph = self . task_graph . lock ( ) . map_err ( |err| {
137- VegaFusionError :: internal ( format ! ( "Failed to acquire task graph lock: {:?}" , err) )
138- } ) ?;
139- let server_to_client = self . server_to_client_value_indices . clone ( ) ;
140- let mut indices: Vec < NodeValueIndex > = Vec :: new ( ) ;
141- for export_update in & updates {
142- let var = match export_update. namespace {
143- ExportUpdateNamespace :: Signal => Variable :: new_signal ( & export_update. name ) ,
144- ExportUpdateNamespace :: Data => Variable :: new_data ( & export_update. name ) ,
145- } ;
146- let scoped_var: ScopedVariable = ( var, export_update. scope . clone ( ) ) ;
147- let node_value_index = * self
148- . task_graph_mapping
149- . get ( & scoped_var)
150- . with_context ( || format ! ( "No task graph node found for {scoped_var:?}" ) ) ?;
151-
152- let value = match export_update. namespace {
153- ExportUpdateNamespace :: Signal => {
154- TaskValue :: Scalar ( ScalarValue :: from_json ( & export_update. value ) ?)
155- }
156- ExportUpdateNamespace :: Data => {
157- TaskValue :: Table ( VegaFusionTable :: from_json ( & export_update. value ) ?)
158- }
159- } ;
160-
161- indices. extend ( task_graph. update_value ( node_value_index. node_index as usize , value) ?) ;
162- }
163-
164- // Filter to update nodes in the comm plan
165- let indices: Vec < _ > = indices
166- . iter ( )
167- . filter ( |& node| server_to_client. contains ( node) )
168- . cloned ( )
169- . collect ( ) ;
136+ // Scope the mutex guard to ensure it's dropped before the async call
137+ let ( indices, cloned_task_graph) = {
138+ let mut task_graph = self . task_graph . lock ( ) . map_err ( |err| {
139+ VegaFusionError :: internal ( format ! ( "Failed to acquire task graph lock: {:?}" , err) )
140+ } ) ?;
141+ let server_to_client = self . server_to_client_value_indices . clone ( ) ;
142+ let mut indices: Vec < NodeValueIndex > = Vec :: new ( ) ;
143+
144+ for export_update in & updates {
145+ let var = match export_update. namespace {
146+ ExportUpdateNamespace :: Signal => Variable :: new_signal ( & export_update. name ) ,
147+ ExportUpdateNamespace :: Data => Variable :: new_data ( & export_update. name ) ,
148+ } ;
149+ let scoped_var: ScopedVariable = ( var, export_update. scope . clone ( ) ) ;
150+ let node_value_index = * self
151+ . task_graph_mapping
152+ . get ( & scoped_var)
153+ . with_context ( || format ! ( "No task graph node found for {scoped_var:?}" ) ) ?;
154+
155+ let value = match export_update. namespace {
156+ ExportUpdateNamespace :: Signal => {
157+ TaskValue :: Scalar ( ScalarValue :: from_json ( & export_update. value ) ?)
158+ }
159+ ExportUpdateNamespace :: Data => {
160+ TaskValue :: Table ( VegaFusionTable :: from_json ( & export_update. value ) ?)
161+ }
162+ } ;
163+
164+ indices
165+ . extend ( task_graph. update_value ( node_value_index. node_index as usize , value) ?) ;
166+ }
167+
168+ // Filter to update nodes in the comm plan
169+ let indices: Vec < _ > = indices
170+ . iter ( )
171+ . filter ( |& node| server_to_client. contains ( node) )
172+ . cloned ( )
173+ . collect ( ) ;
170174
171- let cloned_task_graph = task_graph. clone ( ) ;
175+ // Clone the task graph while we still have the lock
176+ let cloned_task_graph = task_graph. clone ( ) ;
172177
173- // Drop the MutexGuard before await call to avoid warning
174- drop ( task_graph) ;
178+ // Return both values we need
179+ ( indices, cloned_task_graph)
180+ } ; // MutexGuard is dropped here
175181
182+ // Now we can safely make the async call
176183 let response_task_values = runtime
177184 . query_request (
178185 Arc :: new ( cloned_task_graph) ,
@@ -185,7 +192,6 @@ impl ChartState {
185192 . into_iter ( )
186193 . map ( |response_value| {
187194 let variable = response_value. variable ;
188-
189195 let scope = response_value. scope ;
190196 let value = response_value. value ;
191197
0 commit comments