]> uap-core.de Git - note.git/commitdiff
improve backend shutdown
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 22 May 2026 13:59:25 +0000 (15:59 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 22 May 2026 13:59:25 +0000 (15:59 +0200)
application/src/backend.rs
application/src/main.rs

index 405034403df83418846e56393506083ea51411e0..1c537af8a5f9aca5de83f683ff8c499cf45f462b 100644 (file)
@@ -3,6 +3,7 @@ use std::pin::Pin;
 use sea_orm::{ActiveModelTrait, Database, DatabaseConnection, DbErr, EntityTrait, QueryFilter, ColumnTrait, Set, QueryOrder};
 use tokio::runtime::Runtime;
 use std::sync::{Arc};
+use std::thread::JoinHandle;
 use tokio::sync::{broadcast, mpsc};
 use migration::{Migrator, MigratorTrait};
 use ui_rs::ui;
@@ -175,7 +176,7 @@ impl Backend {
     /// messages to the backend via the tx Sender.
     ///
     /// The handle also contains a broadcast sender/receiver (btx, brx).
-    pub fn start(self) -> BackendHandle {
+    pub fn start(self) -> (BackendHandle, JoinHandle<()>) {
         let (tx, mut rx) = mpsc::unbounded_channel::<CmdFuture>();
         let broadcast_tx = self.broadcast.clone();
         let broadcast_rx = broadcast_tx.subscribe();
@@ -185,22 +186,51 @@ impl Backend {
 
         let rt = backend.rt.clone();
 
-        std::thread::spawn(move || {
+        let join = std::thread::spawn(move || {
             rt.block_on(async move {
-                while let Some(cmd) = rx.recv().await {
-                    tokio::spawn(async move {
-                        cmd.await
-                    });
+                let mut set = tokio::task::JoinSet::new();
+
+                loop {
+                    tokio::select! {
+                        cmd = rx.recv() => {
+                            if let Some(cmd) = cmd {
+                                set.spawn(async move {
+                                    cmd.await
+                                });
+                            } else {
+                                break;
+                            }
+                        }
+
+                        Some(result) = set.join_next() => {
+                            if let Err(e) = result {
+                                println!("task failed {:?}", e);
+                            }
+                        }
+
+                        else => {
+                            break;
+                        }
+                    }
+                }
+
+                // wait for remaining tasks on shutdown
+                while let Some(result) = set.join_next().await {
+                    if let Err(e) = result {
+                        println!("task failed {:?}", e);
+                    }
                 }
             });
         });
 
-        BackendHandle {
+        let backend_handle = BackendHandle {
             backend: backend_clone,
             tx: tx,
             btx: broadcast_tx,
             brx: broadcast_rx,
-        }
+        };
+
+        (backend_handle, join)
     }
 }
 
index fe08b483c809e6dc9c2e359e78531ac3af5440fa..9e66688c3cc4795dec51561130d4a3ee227a74db 100644 (file)
@@ -58,13 +58,16 @@ fn main() {
         }
     };
 
-    let handle = backend.start();
+    let (backend_handle, join_handle) = backend.start();
 
     let mut app = App {
-        backend: handle,
+        backend: backend_handle,
         notebooks: notebooks,
     };
     ui::app_run::<MainWindow>(&mut app);
+
+    drop(app);
+    let _ = join_handle.join();
 }
 
 struct ErrMsg {