BF clear the per-channel socket lists when tracking is reset

WebsocketService::resetAllTracking(), which the server runs at start,
forgot "ws_channel_connections" without a channel suffix, a key nothing
writes. The real lists, ws_channel_connections_<channel>, outlived a
restart or crash, and getChannelConnections() returned dead sockets until
the channel was tracked again. The ws_socket_auth_<socket> entries stayed
behind the same way.

resetAllTracking() now reads ws_active_channels and ws_socket_authed_users
before it forgets them and deletes the per-channel lists and per-socket
auth entries they name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-11-04 12:29:04 +01:00
parent 92fc215151
commit f04ace1eea
2 changed files with 37 additions and 0 deletions

View File

@ -161,6 +161,20 @@ class WebsocketService
public static function resetAllTracking() public static function resetAllTracking()
{ {
// The socket lists live under one key per channel. Drop the ones the
// index names, or a restart leaves dead sockets listed until the
// channel is tracked again.
foreach ((array) (cache()->get('ws_active_channels') ?? []) as $channelName) {
if (is_string($channelName) && $channelName !== '') {
cache()->forget('ws_channel_connections_' . $channelName);
}
}
// Same for the per-socket auth entries of the sockets listed as authed.
foreach (array_keys((array) (cache()->get('ws_socket_authed_users') ?? [])) as $socketId) {
cache()->forget('ws_socket_auth_' . str()->slug((string) $socketId));
}
cache()->forget('ws_active_channels'); cache()->forget('ws_active_channels');
cache()->forget('ws_socket_auth'); cache()->forget('ws_socket_auth');
cache()->forget('ws_socket_auth_users'); cache()->forget('ws_socket_auth_users');

View File

@ -37,6 +37,29 @@ class WebsocketServiceTest extends TestCase
$this->assertEmpty(WebsocketService::getAuthedUsers()); $this->assertEmpty(WebsocketService::getAuthedUsers());
} }
public function test_reset_all_tracking_clears_the_per_channel_socket_lists()
{
cache()->forever('ws_active_channels', ['websocket', 'private-user']);
cache()->forever('ws_channel_connections_websocket', ['1.1', '2.2']);
cache()->forever('ws_channel_connections_private-user', ['1.1']);
WebsocketService::resetAllTracking();
$this->assertSame([], WebsocketService::getChannelConnections('websocket'));
$this->assertSame([], WebsocketService::getChannelConnections('private-user'));
}
public function test_reset_all_tracking_clears_the_per_socket_auth_of_authed_sockets()
{
WebsocketService::setUserAuthed('1.123456', (object) ['id' => 42]);
$this->assertNotNull(WebsocketService::getAuth('1.123456'));
WebsocketService::resetAllTracking();
$this->assertNull(WebsocketService::getAuth('1.123456'));
$this->assertEmpty(WebsocketService::getAuthedUsers());
}
// ========================================================================= // =========================================================================
// User auth tracking // User auth tracking
// ========================================================================= // =========================================================================