From 53b447b6a6df75bd940727048d599c6141b7dfe2 Mon Sep 17 00:00:00 2001 From: UkoeHB <37489173+UkoeHB@users.noreply.github.com> Date: Wed, 4 Sep 2024 18:18:02 -0500 Subject: [PATCH] guarantee SessionActor is dropped before close is handled (#107) --- CHANGELOG.md | 5 +++++ src/session.rs | 7 ++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cca05d..2f50ea6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## v0.6.4 + +- Guarantee the internal `SessionActor` is dropped before `ServerExt::on_disconnect` is called. + + ## v0.6.3 - Allow users to use tokio v2.4.0 in their projects. See [#106](https://github.com/gbaranski/ezsockets/pull/106). diff --git a/src/session.rs b/src/session.rs index ca7d2dd..100e8b5 100644 --- a/src/session.rs +++ b/src/session.rs @@ -73,7 +73,7 @@ impl Session { closed_indicator: Arc::new(Mutex::new(Some(closed_receiver))), }; let session = session_fn(handle.clone()); - let mut actor = SessionActor::new( + let actor = SessionActor::new( session, session_id, to_socket_receiver, @@ -82,6 +82,7 @@ impl Session { ); tokio::spawn(async move { + // Note: SessionActor::run consumes the actor, guaranteeing it is dropped before the close event is sent. let result = actor.run().await; closed_sender.send(result).unwrap_or_default(); }); @@ -172,7 +173,7 @@ impl Session { } pub(crate) struct SessionActor { - pub extension: E, + extension: E, id: E::ID, to_socket_receiver: mpsc::UnboundedReceiver, session_call_receiver: mpsc::UnboundedReceiver, @@ -196,7 +197,7 @@ impl SessionActor { } } - pub(crate) async fn run(&mut self) -> Result, Error> { + pub(crate) async fn run(mut self) -> Result, Error> { loop { tokio::select! { biased;