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 @@
11use std::{
2 cell::RefCell,
23 future::Future,
34 path::PathBuf,
45 rc::{Rc, Weak},
@@ -8,6 +9,7 @@ use std::{
89use tokio::{
910 runtime::Runtime,
1011 sync::mpsc::{channel, Receiver, Sender},
12 task::JoinHandle,
1113};
1214
1315use tracing::error;
@@ -83,7 +85,7 @@ pub enum ClientMessage {
8385 bool,
8486 Option<AmbiguityChange>,
8587 ),
86 RestoredRoom(Room),
88 RestoredRoom(OwnedRoomId),
8789}
8890
8991/// Struct representing an active connection to the homeserver.
@@ -97,8 +99,8 @@ pub enum ClientMessage {
9799/// loop drop the object.
98100#[derive(Debug, Clone)]
99101pub struct Connection {
100 #[allow(dead_code)]
101 receiver_task: Rc<Task<()>>,
102 receiver_task: Rc<RefCell<Option<Task<()>>>>,
103 sync_task: Rc<RefCell<Option<JoinHandle<()>>>>,
102104 client: Option<Client>,
103105 runtime: Option<Rc<Runtime>>,
104106}
@@ -128,6 +130,31 @@ impl Connection {
128130 .expect("Tokio error while sending a message")
129131 }
130132
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
131158 pub fn new(server: &MatrixServer, client: &Client) -> Self {
132159 let (tx, rx) = channel(10_000);
133160
@@ -140,7 +167,7 @@ impl Connection {
140167
141168 let runtime = Runtime::new().unwrap();
142169
143 runtime.spawn(Connection::sync_loop(
170 let sync_task = runtime.spawn(Connection::sync_loop(
144171 client.clone(),
145172 tx,
146173 server.user_name(),
@@ -150,9 +177,10 @@ impl Connection {
150177 ));
151178
152179 Self {
180 receiver_task: Rc::new(RefCell::new(Some(receiver_task))),
181 sync_task: Rc::new(RefCell::new(Some(sync_task))),
153182 client: Some(client.clone()),
154183 runtime: Some(runtime.into()),
155 receiver_task: receiver_task.into(),
156184 }
157185 }
158186
@@ -327,8 +355,8 @@ impl Connection {
327355 ClientMessage::SyncState(r, e) => {
328356 server.receive_joined_state_event(&r, e).await
329357 }
330 ClientMessage::RestoredRoom(room) => {
331 server.restore_room(room).await
358 ClientMessage::RestoredRoom(room_id) => {
359 server.restore_room_by_id(room_id).await
332360 }
333361 ClientMessage::ToDeviceEvent(e) => {
334362 server.receive_to_device_event(e).await
@@ -504,7 +532,9 @@ impl Connection {
504532 if !first_login {
505533 for room in client.joined_rooms() {
506534 if channel
507 .send(Ok(ClientMessage::RestoredRoom(room)))
535 .send(Ok(ClientMessage::RestoredRoom(
536 room.room_id().to_owned(),
537 )))
508538 .await
509539 .is_err()
510540 {
@@ -672,20 +702,21 @@ impl Connection {
672702
673703impl Drop for Connection {
674704 fn drop(&mut self) {
675 {
676 let runtime = self.runtime();
705 let Some(runtime) = self.runtime.as_ref().cloned() else {
706 return;
707 };
677708
678 if let Some(client) = self.client.take() {
679 let _guard = runtime.enter();
680 drop(client);
681 }
709 if Rc::strong_count(&runtime) == 1 {
710 self.stop_sync_task(&runtime);
711 self.stop_receiver_task(&runtime);
682712 }
683713
684 let Some(runtime) = self.runtime.as_ref() else {
685 return;
686 };
714 if let Some(client) = self.client.take() {
715 let _guard = runtime.enter();
716 drop(client);
717 }
687718
688 if Rc::strong_count(runtime) == 1 {
719 if Rc::strong_count(&runtime) == 1 {
689720 let runtime = self.runtime.take().expect("runtime disappeared");
690721 let handle = runtime.handle().clone();
691722 let _guard = handle.enter();
src/room/members.rs +1 −1
@@ -6,7 +6,7 @@ use tracing::{debug, error, info};
66
77use matrix_sdk::{
88 deserialized_responses::AmbiguityChange,
9 room::{Room, RoomMember},
9 room::RoomMember,
1010 ruma::{
1111 events::{
1212 room::{
src/server.rs +26 −3
@@ -622,6 +622,23 @@ impl InnerServer {
622622 self.settings.borrow().password.clone()
623623 }
624624
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
625642 pub async fn restore_room(&self, room: Room) {
626643 let homeserver = self
627644 .settings
@@ -1609,8 +1626,11 @@ impl InnerServer {
16091626 }
16101627
16111628 {
1612 let mut connection = self.connection.borrow_mut();
1613 connection.take();
1629 let connection = self.connection.borrow_mut().take();
1630 if let Some(connection) = connection.as_ref() {
1631 connection.shutdown();
1632 }
1633 drop(connection);
16141634 }
16151635
16161636 self.print_network(&format!(
@@ -1623,7 +1643,10 @@ impl InnerServer {
16231643
16241644 pub fn shutdown(&self) {
16251645 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 });
16271650
16281651 if let Some(runtime) = runtime {
16291652 let _guard = runtime.enter();