From 1bf32450fd8395ab6ec597c18fbc5c2467148134 Mon Sep 17 00:00:00 2001 From: HueByte Date: Thu, 19 Feb 2026 12:25:16 +0100 Subject: [PATCH] fix: Improve shutdown process for IRC Gateway and Server Directory services --- src/EchoHub.Server.Irc/IrcGatewayService.cs | 15 +++++++++++---- .../Services/ServerDirectoryService.cs | 8 ++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/EchoHub.Server.Irc/IrcGatewayService.cs b/src/EchoHub.Server.Irc/IrcGatewayService.cs index 65c02ae..894f212 100644 --- a/src/EchoHub.Server.Irc/IrcGatewayService.cs +++ b/src/EchoHub.Server.Irc/IrcGatewayService.cs @@ -141,17 +141,24 @@ public sealed class IrcGatewayService : BackgroundService public override async Task StopAsync(CancellationToken cancellationToken) { + // Cancel ExecuteAsync first so listeners stop accepting + await base.StopAsync(cancellationToken); + + // Force-close any remaining client connections foreach (var (_, conn) in _connections) { try { - await conn.SendAsync("ERROR :Server shutting down"); - await conn.DisposeAsync(); + await conn.SendAsync("ERROR :Server shutting down") + .WaitAsync(TimeSpan.FromSeconds(2)); } catch { } + finally + { + try { await conn.DisposeAsync(); } + catch { } + } } _connections.Clear(); - - await base.StopAsync(cancellationToken); } } diff --git a/src/EchoHub.Server/Services/ServerDirectoryService.cs b/src/EchoHub.Server/Services/ServerDirectoryService.cs index f8734a9..9aa1e86 100644 --- a/src/EchoHub.Server/Services/ServerDirectoryService.cs +++ b/src/EchoHub.Server/Services/ServerDirectoryService.cs @@ -177,13 +177,9 @@ public sealed class ServerDirectoryService( public override async Task StopAsync(CancellationToken cancellationToken) { - if (_connection is not null) - { - await _connection.DisposeAsync(); - _connection = null; - } - + // Cancel ExecuteAsync first — it disposes the connection via await using await base.StopAsync(cancellationToken); + _connection = null; } ///