From 92fc2151515fb98150bf1dc5f3bece10306d3527 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Wed, 4 Nov 2026 12:28:37 +0100 Subject: [PATCH] BF keep the channel tracking cache in step with the handler Two bugs in the buffered ws_* tracking writes: - untrackChannelConnection() read array_keys($this->channel_connections) before it unset an emptied channel, so ws_active_channels kept listing a channel after its last socket unsubscribed or had its subscribe refused. The list is now read after the unset. - flushCacheBuffer() deletes after it writes. When a channel was emptied and joined again in the same loop tick, the buffered delete of ws_channel_connections_ ran after the buffered write and the cache showed no sockets for a channel that had one. A buffered write now cancels an earlier buffered delete of the same key. Tests: the last unsubscribe and a refused re-subscribe of the last tracked socket drop the channel from ws_active_channels; a channel emptied and joined in one tick keeps the new socket in its cache entry. Co-Authored-By: Claude Opus 5.5 --- src/Websocket/Handler.php | 18 ++++++++---- tests/HandlerLifecycleTest.php | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/Websocket/Handler.php b/src/Websocket/Handler.php index 3b755e8..4a6ddb8 100644 --- a/src/Websocket/Handler.php +++ b/src/Websocket/Handler.php @@ -1656,21 +1656,19 @@ class Handler implements MessageComponentInterface unset($this->channel_connections[$channel_name][$socketId]); - // Pre-compute active channels once - $activeChannels = array_keys($this->channel_connections); - if (empty($this->channel_connections[$channel_name])) { unset($this->channel_connections[$channel_name]); - // Buffer delete and update - can be batched + // Buffer delete and update - can be batched. Read the active + // channels after the unset, or the emptied one stays listed. $this->bufferCacheDelete('ws_channel_connections_' . $channel_name); - $this->bufferCacheWrite('ws_active_channels', $activeChannels); + $this->bufferCacheWrite('ws_active_channels', array_keys($this->channel_connections)); return; } // Buffer these writes $this->bufferCacheWrite('ws_channel_connections_' . $channel_name, array_keys($this->channel_connections[$channel_name])); - $this->bufferCacheWrite('ws_active_channels', $activeChannels); + $this->bufferCacheWrite('ws_active_channels', array_keys($this->channel_connections)); } protected function setRequest($message, $connection) @@ -1790,6 +1788,14 @@ class Handler implements MessageComponentInterface protected function bufferCacheWrite(string $key, $value): void { $this->cacheWriteBuffer[$key] = $value; + + // The flush deletes after it writes: a write must cancel a delete of + // the same key buffered earlier in the tick (a channel emptied and + // joined again), or the new value is lost. + if ($this->cacheDeleteBuffer) { + $this->cacheDeleteBuffer = array_values(array_diff($this->cacheDeleteBuffer, [$key])); + } + $this->scheduleCacheFlush(); } diff --git a/tests/HandlerLifecycleTest.php b/tests/HandlerLifecycleTest.php index a0ca444..2159676 100644 --- a/tests/HandlerLifecycleTest.php +++ b/tests/HandlerLifecycleTest.php @@ -842,6 +842,57 @@ class HandlerLifecycleTest extends TestCase $this->assertTrue($channel->hasConnection($conn2)); } + public function test_last_unsubscribe_drops_the_channel_from_active_channels() + { + $connection = $this->newActiveConnection(['lonely-channel', 'other-channel']); + $this->flushHandlerCacheBuffer(); + $this->assertContains('lonely-channel', WebsocketService::getActiveChannels()); + + $this->wsHandler->onMessage($connection, new Mocks\Message([ + 'event' => 'websocket.unsubscribe', + 'data' => ['channel' => 'lonely-channel'], + ])); + $this->flushHandlerCacheBuffer(); + + $this->assertNotContains('lonely-channel', WebsocketService::getActiveChannels()); + $this->assertContains('other-channel', WebsocketService::getActiveChannels()); + $this->assertSame([], WebsocketService::getChannelConnections('lonely-channel')); + } + + public function test_refused_resubscribe_of_the_last_socket_drops_the_channel_from_active_channels() + { + $connection = $this->newPrivateConnection('private-test'); + $this->channelManager->find('1234', 'private-test')->unsubscribe($connection); + $this->flushHandlerCacheBuffer(); + $this->assertContains('private-test', WebsocketService::getActiveChannels()); + + $this->wsHandler->onMessage($connection, new Mocks\Message([ + 'event' => 'websocket.subscribe', + 'data' => ['auth' => 'TestKey:invalid-signature', 'channel' => 'private-test'], + ])); + $this->flushHandlerCacheBuffer(); + + $this->assertNotContains('private-test', WebsocketService::getActiveChannels()); + } + + public function test_channel_emptied_and_joined_in_one_tick_keeps_its_cache_entry() + { + $first = $this->newActiveConnection(['busy-channel']); + $this->flushHandlerCacheBuffer(); + + // Same loop tick: the last socket leaves, another one joins. The + // buffered delete used to run after the buffered write. + $this->wsHandler->onMessage($first, new Mocks\Message([ + 'event' => 'websocket.unsubscribe', + 'data' => ['channel' => 'busy-channel'], + ])); + $second = $this->newActiveConnection(['busy-channel']); + $this->flushHandlerCacheBuffer(); + + $this->assertSame([$second->socketId], WebsocketService::getChannelConnections('busy-channel')); + $this->assertContains('busy-channel', WebsocketService::getActiveChannels()); + } + // ========================================================================= // Message without app context // =========================================================================