Commit ac3e9390e0

ac3e9390e0b2812a3f8a64020396c4bd8f2dc653

parent: 0a7cdb16aa

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-09 06:49 UTC
committer: <strk@kbt.io>

fix: stop sync tasks before shutdown

Layout: unified · split

src/connection.rs +49 −18
@@ -1,4 +1,5 @@
1use std::{ 1use std::{
2 cell::RefCell,
2 future::Future, 3 future::Future,
3 path::PathBuf, 4 path::PathBuf,
4 rc::{Rc, Weak}, 5 rc::{Rc, Weak},
@@ -8,6 +9,7 @@ use std::{
8use tokio::{ 9use tokio::{
9 runtime::Runtime, 10 runtime::Runtime,
10 sync::mpsc::{channel, Receiver, Sender}, 11 sync::mpsc::{channel, Receiver, Sender},
12 task::JoinHandle,
11}; 13};
12 14
13use tracing::error; 15use tracing::error;
@@ -83,7 +85,7 @@ pub enum ClientMessage {
83 bool, 85 bool,
84 Option<AmbiguityChange>, 86 Option<AmbiguityChange>,
85 ), 87 ),
86 RestoredRoom(Room), 88 RestoredRoom(OwnedRoomId),
87} 89}
88 90
89/// Struct representing an active connection to the homeserver. 91/// Struct representing an active connection to the homeserver.
@@ -97,8 +99,8 @@ pub enum ClientMessage {
97/// loop drop the object. 99/// loop drop the object.
98#[derive(Debug, Clone)] 100#[derive(Debug, Clone)]
99pub struct Connection { 101pub struct Connection {
100 #[allow(dead_code)] 102 receiver_task: Rc<RefCell<Option<Task<()>>>>,
101 receiver_task: Rc<Task<()>>, 103 sync_task: Rc<RefCell<Option<JoinHandle<()>>>>,
102 client: Option<Client>, 104 client: Option<Client>,
103 runtime: Option<Rc<Runtime>>, 105 runtime: Option<Rc<Runtime>>,
104} 106}
@@ -128,6 +130,31 @@ impl Connection {
128 .expect("Tokio error while sending a message") 130 .expect("Tokio error while sending a message")
129 } 131 }
130 132
133 pub fn shutdown(&self) {
134 let runtime = self.runtime();
135
136 self.stop_sync_task(&runtime);
137 self.stop_receiver_task(&runtime);
138 }
139
140 fn stop_sync_task(&self, runtime: &Runtime) {
141 let Some(task) = self.sync_task.borrow_mut().take() else {
142 return;
143 };
144
145 task.abort();
146 let _ = runtime.block_on(async { task.await });
147 }
148
149 fn stop_receiver_task(&self, runtime: &Runtime) {
150 let Some(task) = self.receiver_task.borrow_mut().take() else {
151 return;
152 };
153
154 let _guard = runtime.enter();
155 drop(task);
156 }
157
131 pub fn new(server: &MatrixServer, client: &Client) -> Self { 158 pub fn new(server: &MatrixServer, client: &Client) -> Self {
132 let (tx, rx) = channel(10_000); 159 let (tx, rx) = channel(10_000);
133 160
@@ -140,7 +167,7 @@ impl Connection {
140 167
141 let runtime = Runtime::new().unwrap(); 168 let runtime = Runtime::new().unwrap();
142 169
143 runtime.spawn(Connection::sync_loop( 170 let sync_task = runtime.spawn(Connection::sync_loop(
144 client.clone(), 171 client.clone(),
145 tx, 172 tx,
146 server.user_name(), 173 server.user_name(),
@@ -150,9 +177,10 @@ impl Connection {
150 )); 177 ));
151 178
152 Self { 179 Self {
180 receiver_task: Rc::new(RefCell::new(Some(receiver_task))),
181 sync_task: Rc::new(RefCell::new(Some(sync_task))),
153 client: Some(client.clone()), 182 client: Some(client.clone()),
154 runtime: Some(runtime.into()), 183 runtime: Some(runtime.into()),
155 receiver_task: receiver_task.into(),
156 } 184 }
157 } 185 }
158 186
@@ -327,8 +355,8 @@ impl Connection {
327 ClientMessage::SyncState(r, e) => { 355 ClientMessage::SyncState(r, e) => {
328 server.receive_joined_state_event(&r, e).await 356 server.receive_joined_state_event(&r, e).await
329 } 357 }
330 ClientMessage::RestoredRoom(room) => { 358 ClientMessage::RestoredRoom(room_id) => {
331 server.restore_room(room).await 359 server.restore_room_by_id(room_id).await
332 } 360 }
333 ClientMessage::ToDeviceEvent(e) => { 361 ClientMessage::ToDeviceEvent(e) => {
334 server.receive_to_device_event(e).await 362 server.receive_to_device_event(e).await
@@ -504,7 +532,9 @@ impl Connection {
504 if !first_login { 532 if !first_login {
505 for room in client.joined_rooms() { 533 for room in client.joined_rooms() {
506 if channel 534 if channel
507 .send(Ok(ClientMessage::RestoredRoom(room))) 535 .send(Ok(ClientMessage::RestoredRoom(
536 room.room_id().to_owned(),
537 )))
508 .await 538 .await
509 .is_err() 539 .is_err()
510 { 540 {
@@ -672,20 +702,21 @@ impl Connection {
672 702
673impl Drop for Connection { 703impl Drop for Connection {
674 fn drop(&mut self) { 704 fn drop(&mut self) {
675 { 705 let Some(runtime) = self.runtime.as_ref().cloned() else {
676 let runtime = self.runtime(); 706 return;
707 };
677 708
678 if let Some(client) = self.client.take() { 709 if Rc::strong_count(&runtime) == 1 {
679 let _guard = runtime.enter(); 710 self.stop_sync_task(&runtime);
680 drop(client); 711 self.stop_receiver_task(&runtime);
681 }
682 } 712 }
683 713
684 let Some(runtime) = self.runtime.as_ref() else { 714 if let Some(client) = self.client.take() {
685 return; 715 let _guard = runtime.enter();
686 }; 716 drop(client);
717 }
687 718
688 if Rc::strong_count(runtime) == 1 { 719 if Rc::strong_count(&runtime) == 1 {
689 let runtime = self.runtime.take().expect("runtime disappeared"); 720 let runtime = self.runtime.take().expect("runtime disappeared");
690 let handle = runtime.handle().clone(); 721 let handle = runtime.handle().clone();
691 let _guard = handle.enter(); 722 let _guard = handle.enter();
src/room/members.rs +1 −1
@@ -6,7 +6,7 @@ use tracing::{debug, error, info};
6 6
7use matrix_sdk::{ 7use matrix_sdk::{
8 deserialized_responses::AmbiguityChange, 8 deserialized_responses::AmbiguityChange,
9 room::{Room, RoomMember}, 9 room::RoomMember,
10 ruma::{ 10 ruma::{
11 events::{ 11 events::{
12 room::{ 12 room::{
src/server.rs +26 −3
@@ -622,6 +622,23 @@ impl InnerServer {
622 self.settings.borrow().password.clone() 622 self.settings.borrow().password.clone()
623 } 623 }
624 624
625 pub async fn restore_room_by_id(&self, room_id: OwnedRoomId) {
626 if self.rooms.borrow().contains_key(&room_id) {
627 return;
628 }
629
630 let Some(client) = self.get_client() else {
631 return;
632 };
633
634 let Some(room) = client.get_room(&room_id) else {
635 error!("Can't restore room {}, room not found", room_id);
636 return;
637 };
638
639 self.restore_room(room).await;
640 }
641
625 pub async fn restore_room(&self, room: Room) { 642 pub async fn restore_room(&self, room: Room) {
626 let homeserver = self 643 let homeserver = self
627 .settings 644 .settings
@@ -1609,8 +1626,11 @@ impl InnerServer {
1609 } 1626 }
1610 1627
1611 { 1628 {
1612 let mut connection = self.connection.borrow_mut(); 1629 let connection = self.connection.borrow_mut().take();
1613 connection.take(); 1630 if let Some(connection) = connection.as_ref() {
1631 connection.shutdown();
1632 }
1633 drop(connection);
1614 } 1634 }
1615 1635
1616 self.print_network(&format!( 1636 self.print_network(&format!(
@@ -1623,7 +1643,10 @@ impl InnerServer {
1623 1643
1624 pub fn shutdown(&self) { 1644 pub fn shutdown(&self) {
1625 let connection = self.connection.borrow_mut().take(); 1645 let connection = self.connection.borrow_mut().take();
1626 let runtime = connection.as_ref().map(|c| c.runtime()); 1646 let runtime = connection.as_ref().map(|c| {
1647 c.shutdown();
1648 c.runtime()
1649 });
1627 1650
1628 if let Some(runtime) = runtime { 1651 if let Some(runtime) = runtime {
1629 let _guard = runtime.enter(); 1652 let _guard = runtime.enter();