Commit 6ed8040ddb

6ed8040ddb8bd3306fe26803446b63c718aeddac

parent: 04e248d064

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-08 11:19 UTC
committer: <strk@kbt.io>

fix: release room SDK state during shutdown

Layout: unified · split

src/room/buffer.rs +12 −5
@@ -17,16 +17,18 @@ use weechat::{
17 17
18use crate::{render::RenderedEvent, utils::ToTag}; 18use crate::{render::RenderedEvent, utils::ToTag};
19 19
20use super::{active_room, SharedRoom};
21
20#[derive(Clone)] 22#[derive(Clone)]
21pub struct RoomBuffer { 23pub struct RoomBuffer {
22 room: Room, 24 room: SharedRoom,
23 runtime: Handle, 25 runtime: Handle,
24 pub(super) inner: Rc<RefCell<Option<BufferHandle>>>, 26 pub(super) inner: Rc<RefCell<Option<BufferHandle>>>,
25 thread_buffers: Rc<RefCell<HashMap<OwnedEventId, BufferHandle>>>, 27 thread_buffers: Rc<RefCell<HashMap<OwnedEventId, BufferHandle>>>,
26} 28}
27 29
28impl RoomBuffer { 30impl RoomBuffer {
29 pub fn new(room: Room, runtime: Handle) -> Self { 31 pub fn new(room: SharedRoom, runtime: Handle) -> Self {
30 Self { 32 Self {
31 room, 33 room,
32 runtime, 34 runtime,
@@ -280,7 +282,9 @@ impl RoomBuffer {
280 282
281 pub fn set_topic(&self) { 283 pub fn set_topic(&self) {
282 if let Ok(buffer) = self.buffer_handle().upgrade() { 284 if let Ok(buffer) = self.buffer_handle().upgrade() {
283 buffer.set_title(&self.room.topic().unwrap_or_default()); 285 buffer.set_title(
286 &active_room(&self.room).topic().unwrap_or_default(),
287 );
284 } 288 }
285 } 289 }
286 290
@@ -293,7 +297,9 @@ impl RoomBuffer {
293 } 297 }
294 298
295 pub fn update_parent_spaces(&self) { 299 pub fn update_parent_spaces(&self) {
296 let spaces = self.runtime.block_on(parent_spaces(self.room.clone())); 300 let spaces = self
301 .runtime
302 .block_on(parent_spaces(active_room(&self.room)));
297 303
298 if let Ok(buffer) = self.buffer_handle().upgrade() { 304 if let Ok(buffer) = self.buffer_handle().upgrade() {
299 match spaces { 305 match spaces {
@@ -332,11 +338,12 @@ impl RoomBuffer {
332 } 338 }
333 339
334 fn alias(&self) -> Option<OwnedRoomAliasId> { 340 fn alias(&self) -> Option<OwnedRoomAliasId> {
335 self.room.canonical_alias() 341 active_room(&self.room).canonical_alias()
336 } 342 }
337 343
338 pub fn calculate_buffer_name(&self) -> String { 344 pub fn calculate_buffer_name(&self) -> String {
339 let room = self.room.clone(); 345 let room = self.room.clone();
346 let room = active_room(&self.room);
340 let is_direct = 347 let is_direct =
341 self.runtime.block_on(room.is_direct()).unwrap_or(false); 348 self.runtime.block_on(room.is_direct()).unwrap_or(false);
342 349
src/room/members.rs +13 −6
@@ -25,12 +25,14 @@ use weechat::{
25}; 25};
26 26
27use crate::{ 27use crate::{
28 config::Config, render::render_membership, room::buffer::RoomBuffer, 28 config::Config,
29 render::render_membership,
30 room::{active_room, buffer::RoomBuffer, SharedRoom},
29}; 31};
30 32
31#[derive(Clone)] 33#[derive(Clone)]
32pub struct Members { 34pub struct Members {
33 room: Room, 35 room: SharedRoom,
34 pub(super) runtime: Handle, 36 pub(super) runtime: Handle,
35 ambiguity_map: Rc<DashMap<OwnedUserId, bool>>, 37 ambiguity_map: Rc<DashMap<OwnedUserId, bool>>,
36 nicks: Rc<DashMap<OwnedUserId, String>>, 38 nicks: Rc<DashMap<OwnedUserId, String>>,
@@ -55,7 +57,7 @@ impl PartialEq for WeechatRoomMember {
55 57
56impl Members { 58impl Members {
57 pub fn new( 59 pub fn new(
58 room: Room, 60 room: SharedRoom,
59 runtime: Handle, 61 runtime: Handle,
60 config: Rc<RefCell<Config>>, 62 config: Rc<RefCell<Config>>,
61 buffer: RoomBuffer, 63 buffer: RoomBuffer,
@@ -98,7 +100,7 @@ impl Members {
98 } 100 }
99 101
100 pub async fn restore_member(&self, user_id: OwnedUserId) { 102 pub async fn restore_member(&self, user_id: OwnedUserId) {
101 let room = self.room.clone(); 103 let room = active_room(&self.room);
102 let user = user_id.to_owned(); 104 let user = user_id.to_owned();
103 105
104 match self 106 match self
@@ -215,14 +217,15 @@ impl Members {
215 217
216 /// Retrieve a reference to a Weechat room member by user ID. 218 /// Retrieve a reference to a Weechat room member by user ID.
217 pub async fn get(&self, user_id: &UserId) -> Option<WeechatRoomMember> { 219 pub async fn get(&self, user_id: &UserId) -> Option<WeechatRoomMember> {
218 let color = if self.room.own_user_id() == user_id { 220 let sdk_room = active_room(&self.room);
221 let color = if sdk_room.own_user_id() == user_id {
219 "weechat.color.chat_nick_self".into() 222 "weechat.color.chat_nick_self".into()
220 } else { 223 } else {
221 Weechat::info_get("nick_color_name", user_id.as_str()) 224 Weechat::info_get("nick_color_name", user_id.as_str())
222 .expect("Couldn't get the nick color name") 225 .expect("Couldn't get the nick color name")
223 }; 226 };
224 227
225 let room = self.room.clone(); 228 let room = sdk_room.clone();
226 let user = user_id.to_owned(); 229 let user = user_id.to_owned();
227 230
228 match self 231 match self
@@ -271,6 +274,10 @@ impl Members {
271 } 274 }
272 } 275 }
273 276
277 fn room(&self) -> Room {
278 active_room(&self.room)
279 }
280
274 pub fn mark_active( 281 pub fn mark_active(
275 &self, 282 &self,
276 user_id: &UserId, 283 user_id: &UserId,
src/room/mod.rs +30 −15
@@ -96,6 +96,15 @@ pub struct RoomHandle {
96 inner: MatrixRoom, 96 inner: MatrixRoom,
97} 97}
98 98
99pub(super) type SharedRoom = Rc<RefCell<Option<Room>>>;
100
101pub(super) fn active_room(room: &SharedRoom) -> Room {
102 room.borrow()
103 .as_ref()
104 .expect("Matrix room was already shut down")
105 .clone()
106}
107
99#[derive(Debug, Clone, Eq, PartialEq)] 108#[derive(Debug, Clone, Eq, PartialEq)]
100pub enum PrevBatch { 109pub enum PrevBatch {
101 Forward(String), 110 Forward(String),
@@ -159,7 +168,7 @@ pub struct MatrixRoom {
159 homeserver: Rc<Url>, 168 homeserver: Rc<Url>,
160 room_id: Rc<RoomId>, 169 room_id: Rc<RoomId>,
161 own_user_id: Rc<UserId>, 170 own_user_id: Rc<UserId>,
162 room: Room, 171 room: SharedRoom,
163 buffer: RoomBuffer, 172 buffer: RoomBuffer,
164 173
165 config: Rc<RefCell<Config>>, 174 config: Rc<RefCell<Config>>,
@@ -315,6 +324,8 @@ impl RoomHandle {
315 room_id: &RoomId, 324 room_id: &RoomId,
316 own_user_id: &UserId, 325 own_user_id: &UserId,
317 ) -> Self { 326 ) -> Self {
327 let room = Rc::new(RefCell::new(Some(room)));
328 let sdk_room = active_room(&room);
318 let buffer = RoomBuffer::new(room.clone(), runtime.clone()); 329 let buffer = RoomBuffer::new(room.clone(), runtime.clone());
319 let members = Members::new( 330 let members = Members::new(
320 room.clone(), 331 room.clone(),
@@ -324,7 +335,7 @@ impl RoomHandle {
324 ); 335 );
325 336
326 let own_nick = runtime 337 let own_nick = runtime
327 .block_on(room.get_member_no_sync(own_user_id)) 338 .block_on(sdk_room.get_member_no_sync(own_user_id))
328 .ok() 339 .ok()
329 .flatten() 340 .flatten()
330 .map(|m| m.name().to_owned()) 341 .map(|m| m.name().to_owned())
@@ -343,7 +354,7 @@ impl RoomHandle {
343 connection: connection.clone(), 354 connection: connection.clone(),
344 config, 355 config,
345 prev_batch: Rc::new(RefCell::new( 356 prev_batch: Rc::new(RefCell::new(
346 room.last_prev_batch().map(PrevBatch::Backwards), 357 sdk_room.last_prev_batch().map(PrevBatch::Backwards),
347 )), 358 )),
348 latest_event_id: Rc::new(RefCell::new(None)), 359 latest_event_id: Rc::new(RefCell::new(None)),
349 latest_read_event_id: Rc::new(RefCell::new(None)), 360 latest_read_event_id: Rc::new(RefCell::new(None)),
@@ -511,10 +522,14 @@ impl MatrixRoom {
511 self.buffer.owns_buffer(buffer) 522 self.buffer.owns_buffer(buffer)
512 } 523 }
513 524
525 pub fn release_sdk_state(&self) {
526 self.room.borrow_mut().take();
527 }
528
514 pub fn is_encrypted(&self) -> bool { 529 pub fn is_encrypted(&self) -> bool {
515 self.members 530 self.members
516 .runtime 531 .runtime
517 .block_on(self.room.latest_encryption_state()) 532 .block_on(self.room().latest_encryption_state())
518 .map(|s| s.is_encrypted()) 533 .map(|s| s.is_encrypted())
519 .unwrap_or_default() 534 .unwrap_or_default()
520 } 535 }
@@ -522,23 +537,23 @@ impl MatrixRoom {
522 pub fn contains_only_verified_devices(&self) -> bool { 537 pub fn contains_only_verified_devices(&self) -> bool {
523 self.members 538 self.members
524 .runtime 539 .runtime
525 .block_on(self.room.contains_only_verified_devices()) 540 .block_on(self.room().contains_only_verified_devices())
526 .unwrap_or_default() 541 .unwrap_or_default()
527 } 542 }
528 543
529 pub fn is_public(&self) -> bool { 544 pub fn is_public(&self) -> bool {
530 self.room.is_public().unwrap_or_default() 545 self.room().is_public().unwrap_or_default()
531 } 546 }
532 547
533 pub fn is_direct(&self) -> bool { 548 pub fn is_direct(&self) -> bool {
534 self.members 549 self.members
535 .runtime 550 .runtime
536 .block_on(self.room.is_direct()) 551 .block_on(self.room().is_direct())
537 .unwrap_or_default() 552 .unwrap_or_default()
538 } 553 }
539 554
540 pub fn alias(&self) -> Option<OwnedRoomAliasId> { 555 pub fn alias(&self) -> Option<OwnedRoomAliasId> {
541 self.room.canonical_alias() 556 self.room().canonical_alias()
542 } 557 }
543 558
544 pub fn room_id(&self) -> &RoomId { 559 pub fn room_id(&self) -> &RoomId {
@@ -950,7 +965,7 @@ impl MatrixRoom {
950 self.queue_outgoing_message(&transaction_id, &content).await; 965 self.queue_outgoing_message(&transaction_id, &content).await;
951 match c 966 match c
952 .send_message( 967 .send_message(
953 self.room().clone(), 968 self.room(),
954 AnyMessageLikeEventContent::RoomMessage(content), 969 AnyMessageLikeEventContent::RoomMessage(content),
955 Some(transaction_id.clone()), 970 Some(transaction_id.clone()),
956 ) 971 )
@@ -1087,7 +1102,7 @@ impl MatrixRoom {
1087 1102
1088 match connection 1103 match connection
1089 .spawn({ 1104 .spawn({
1090 let room = self.room().clone(); 1105 let room = self.room();
1091 async move { 1106 async move {
1092 room.send_attachment(filename, &content_type, data, config) 1107 room.send_attachment(filename, &content_type, data, config)
1093 .await 1108 .await
@@ -1133,7 +1148,7 @@ impl MatrixRoom {
1133 } 1148 }
1134 1149
1135 let connection = self.connection.clone(); 1150 let connection = self.connection.clone();
1136 let room = self.room().clone(); 1151 let room = self.room();
1137 1152
1138 let send = |typing: bool| async move { 1153 let send = |typing: bool| async move {
1139 let connection = connection.borrow().clone(); 1154 let connection = connection.borrow().clone();
@@ -1249,7 +1264,7 @@ impl MatrixRoom {
1249 Weechat::bar_item_update("matrix_modes"); 1264 Weechat::bar_item_update("matrix_modes");
1250 1265
1251 if let Some(connection) = connection { 1266 if let Some(connection) = connection {
1252 let room = self.room().clone(); 1267 let room = self.room();
1253 let room_id = room.room_id().to_owned(); 1268 let room_id = room.room_id().to_owned();
1254 1269
1255 if let Ok(r) = connection.room_messages(room, prev_batch).await { 1270 if let Ok(r) = connection.room_messages(room, prev_batch).await {
@@ -1488,7 +1503,7 @@ impl MatrixRoom {
1488 if let Ok(buffer) = self.buffer_handle().upgrade() { 1503 if let Ok(buffer) = self.buffer_handle().upgrade() {
1489 if buffer.num_lines() == 0 { 1504 if buffer.num_lines() == 0 {
1490 *self.prev_batch.borrow_mut() = 1505 *self.prev_batch.borrow_mut() =
1491 self.room.last_prev_batch().map(PrevBatch::Backwards); 1506 self.room().last_prev_batch().map(PrevBatch::Backwards);
1492 } 1507 }
1493 } 1508 }
1494 } 1509 }
@@ -1565,8 +1580,8 @@ impl MatrixRoom {
1565 } 1580 }
1566 } 1581 }
1567 1582
1568 pub fn room(&self) -> &Room { 1583 pub fn room(&self) -> Room {
1569 &self.room 1584 active_room(&self.room)
1570 } 1585 }
1571 1586
1572 pub async fn handle_sync_state_event( 1587 pub async fn handle_sync_state_event(
src/server.rs +18 −3
@@ -1622,11 +1622,26 @@ impl InnerServer {
1622 } 1622 }
1623 1623
1624 pub fn shutdown(&self) { 1624 pub fn shutdown(&self) {
1625 let mut connection = self.connection.borrow_mut(); 1625 let connection = self.connection.borrow_mut().take();
1626 connection.take(); 1626 let runtime = connection.as_ref().map(|c| c.runtime.clone());
1627 drop(connection);
1628 1627
1628 if let Some(runtime) = runtime {
1629 let _guard = runtime.enter();
1630 self.shutdown_sdk_state();
1631 drop(connection);
1632 } else {
1633 let runtime = self.servers.runtime().to_owned();
1634 let _guard = runtime.enter();
1635 self.shutdown_sdk_state();
1636 }
1637 }
1638
1639 fn shutdown_sdk_state(&self) {
1629 self.verification_buffers.borrow_mut().clear(); 1640 self.verification_buffers.borrow_mut().clear();
1641
1642 for room in self.rooms.borrow().values() {
1643 room.release_sdk_state();
1644 }
1630 self.rooms.borrow_mut().clear(); 1645 self.rooms.borrow_mut().clear();
1631 1646
1632 let mut client = self.client.borrow_mut(); 1647 let mut client = self.client.borrow_mut();