Skip to content

Commit af068a1

Browse files
committed
disconnect clients without dropping ModuleHost
1 parent 49cfb51 commit af068a1

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

crates/core/src/host/host_controller.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -981,14 +981,30 @@ impl Host {
981981

982982
let update_result =
983983
update_module(&replica_ctx.relational_db, &module, program, old_module_info, policy).await?;
984-
trace!("update result: {update_result:?}");
984+
985985
// Only replace the module + scheduler if the update succeeded.
986986
// Otherwise, we want the database to continue running with the old state.
987-
if update_result.was_successful() && !update_result.hotswap_disabled() {
988-
self.scheduler = scheduler;
989-
scheduler_starter.start(&module)?;
990-
let old_module = self.module.send_replace(module);
991-
old_module.exit().await;
987+
match update_result {
988+
UpdateDatabaseResult::NoUpdateNeeded | UpdateDatabaseResult::UpdatePerformed => {
989+
self.scheduler = scheduler;
990+
scheduler_starter.start(&module)?;
991+
let old_module = self.module.send_replace(module);
992+
old_module.exit().await;
993+
}
994+
995+
// To disconnect clients, drop `self.module` (a `watch::Sender`) so that subscribed
996+
// clients observe that the module is gone and will close their connections.
997+
// The `ws_client_actor` in `spacetimedb_client_api::routes::subscribe` handles
998+
// cleanup of client state on disconnection, so we don’t need to perform it here.
999+
UpdateDatabaseResult::UpdatePerformedWithClientDisconnect => {
1000+
let old_module = self.module.borrow().clone();
1001+
self.module = watch::Sender::new(module.clone());
1002+
1003+
self.scheduler = scheduler;
1004+
scheduler_starter.start(&module)?;
1005+
old_module.exit().await;
1006+
}
1007+
_ => {}
9921008
}
9931009

9941010
Ok(update_result)

0 commit comments

Comments
 (0)