@@ -15,7 +15,7 @@ use crate::util::sanitize_identifier;
1515pub struct WorkerState {
1616 pub worker_id : String ,
1717 pub is_leader : Rc < RefCell < bool > > ,
18- pub db : Rc < RefCell < Option < Rc < SQLiteDatabase > > > > ,
18+ pub db : Rc < RefCell < Option < SQLiteDatabase > > > ,
1919 pub channel : BroadcastChannel ,
2020 pub db_name : String ,
2121 pub pending_queries : Rc < RefCell < HashMap < String , PendingQuery > > > ,
@@ -75,11 +75,18 @@ impl WorkerState {
7575 let channel = channel. clone ( ) ;
7676
7777 spawn_local ( async move {
78- let database = db. borrow ( ) . clone ( ) ;
79- let result = if let Some ( database) = database {
80- database. exec ( & sql) . await
81- } else {
82- Err ( "Database not initialized" . to_string ( ) )
78+ let result = {
79+ // Extract database temporarily to avoid holding RefCell across await
80+ let db_opt = db. borrow_mut ( ) . take ( ) ;
81+ match db_opt {
82+ Some ( mut database) => {
83+ let result = database. exec ( & sql) . await ;
84+ // Put the database back
85+ * db. borrow_mut ( ) = Some ( database) ;
86+ result
87+ }
88+ None => Err ( "Database not initialized" . to_string ( ) ) ,
89+ }
8390 } ;
8491
8592 let response = match result {
@@ -158,7 +165,7 @@ impl WorkerState {
158165 spawn_local ( async move {
159166 match SQLiteDatabase :: initialize_opfs ( & db_name) . await {
160167 Ok ( database) => {
161- * db. borrow_mut ( ) = Some ( Rc :: new ( database) ) ;
168+ * db. borrow_mut ( ) = Some ( database) ;
162169
163170 let msg = ChannelMessage :: NewLeader {
164171 leader_id : worker_id. clone ( ) ,
@@ -190,11 +197,16 @@ impl WorkerState {
190197
191198 pub async fn execute_query ( & self , sql : String ) -> Result < String , String > {
192199 if * self . is_leader . borrow ( ) {
193- let database = self . db . borrow ( ) . clone ( ) ;
194- if let Some ( database) = database {
195- database. exec ( & sql) . await
196- } else {
197- Err ( "Database not initialized" . to_string ( ) )
200+ // Extract database temporarily to avoid holding RefCell across await
201+ let db_opt = self . db . borrow_mut ( ) . take ( ) ;
202+ match db_opt {
203+ Some ( mut database) => {
204+ let result = database. exec ( & sql) . await ;
205+ // Put the database back
206+ * self . db . borrow_mut ( ) = Some ( database) ;
207+ result
208+ }
209+ None => Err ( "Database not initialized" . to_string ( ) ) ,
198210 }
199211 } else {
200212 let query_id = Uuid :: new_v4 ( ) . to_string ( ) ;
0 commit comments