Commit 0a7cdb16aa

0a7cdb16aab975bd39aba1905bb4eaab88268c58

parent: 6ed8040ddb

Unsigned

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

fix: drop connection runtime under entered handle

Layout: unified · split

src/connection.rs +28 −6
@@ -100,10 +100,17 @@ pub struct Connection {
100100 #[allow(dead_code)]
101101 receiver_task: Rc<Task<()>>,
102102 client: Option<Client>,
103 pub runtime: Rc<Runtime>,
103 runtime: Option<Rc<Runtime>>,
104104}
105105
106106impl Connection {
107 pub fn runtime(&self) -> Rc<Runtime> {
108 self.runtime
109 .as_ref()
110 .expect("Connection runtime was already dropped")
111 .clone()
112 }
113
107114 pub fn client(&self) -> &Client {
108115 self.client
109116 .as_ref()
@@ -115,7 +122,7 @@ impl Connection {
115122 F: Future + Send + 'static,
116123 F::Output: Send + 'static,
117124 {
118 self.runtime
125 self.runtime()
119126 .spawn(future)
120127 .await
121128 .expect("Tokio error while sending a message")
@@ -144,7 +151,7 @@ impl Connection {
144151
145152 Self {
146153 client: Some(client.clone()),
147 runtime: runtime.into(),
154 runtime: Some(runtime.into()),
148155 receiver_task: receiver_task.into(),
149156 }
150157 }
@@ -665,9 +672,24 @@ impl Connection {
665672
666673impl Drop for Connection {
667674 fn drop(&mut self) {
668 if let Some(client) = self.client.take() {
669 let _guard = self.runtime.enter();
670 drop(client);
675 {
676 let runtime = self.runtime();
677
678 if let Some(client) = self.client.take() {
679 let _guard = runtime.enter();
680 drop(client);
681 }
682 }
683
684 let Some(runtime) = self.runtime.as_ref() else {
685 return;
686 };
687
688 if Rc::strong_count(runtime) == 1 {
689 let runtime = self.runtime.take().expect("runtime disappeared");
690 let handle = runtime.handle().clone();
691 let _guard = handle.enter();
692 drop(runtime);
671693 }
672694 }
673695}
src/room/mod.rs +1
@@ -523,6 +523,7 @@ impl MatrixRoom {
523523 }
524524
525525 pub fn release_sdk_state(&self) {
526 self.verification.release_sdk_state();
526527 self.room.borrow_mut().take();
527528 }
528529
src/room/verification.rs +4
@@ -71,6 +71,10 @@ impl Verification {
7171 }
7272 }
7373
74 pub fn release_sdk_state(&self) {
75 self.inner.borrow_mut().take();
76 }
77
7478 pub async fn confirm(&self) {
7579 let connection = self.connection.borrow().clone();
7680
src/server.rs +1 −1
@@ -1623,7 +1623,7 @@ impl InnerServer {
16231623
16241624 pub fn shutdown(&self) {
16251625 let connection = self.connection.borrow_mut().take();
1626 let runtime = connection.as_ref().map(|c| c.runtime.clone());
1626 let runtime = connection.as_ref().map(|c| c.runtime());
16271627
16281628 if let Some(runtime) = runtime {
16291629 let _guard = runtime.enter();