From 0839154f46cfdc92d2f174757cc18a9849e66424 Mon Sep 17 00:00:00 2001 From: Mateusz Wiktor Date: Mon, 23 Nov 2026 12:40:02 +0100 Subject: [PATCH 1/5] Send a leave request on unsubscribe with the default asyncio client The event engine subscription manager, the default for PubNubAsyncio, sent a leave request only when enable_presence_heartbeat was on. A default client therefore stayed an occupant of the channel until the presence timeout, and suppress_leave_events=False (the default) had no effect. The threaded and legacy asyncio managers send the leave unless suppress_leave_events is set. Send it directly when the presence engine does not, skip -pnpres channels and empty operations, and keep suppress_leave_events working. Add offline unit tests and a live test that checks here_now after the unsubscribe. Co-Authored-By: Claude Sonnet 5.5 --- .pubnub.yml | 13 +- CHANGELOG.md | 6 + pubnub/pubnub_asyncio.py | 15 +++ setup.py | 2 +- tests/integrational/asyncio/test_subscribe.py | 42 +++++++ tests/unit/test_asyncio_unsubscribe_leave.py | 111 ++++++++++++++++++ 6 files changed, 184 insertions(+), 5 deletions(-) create mode 100644 tests/unit/test_asyncio_unsubscribe_leave.py diff --git a/.pubnub.yml b/.pubnub.yml index 145c262b..22406a60 100644 --- a/.pubnub.yml +++ b/.pubnub.yml @@ -1,5 +1,5 @@ name: python -version: 10.9.0 +version: 10.9.1 schema: 1 scm: github.com/pubnub/python sdks: @@ -18,7 +18,7 @@ sdks: distributions: - distribution-type: library distribution-repository: package - package-name: pubnub-10.9.0 + package-name: pubnub-10.9.1 location: https://pypi.org/project/pubnub/ supported-platforms: supported-operating-systems: @@ -94,8 +94,8 @@ sdks: - distribution-type: library distribution-repository: git release - package-name: pubnub-10.9.0 - location: https://github.com/pubnub/python/releases/download/10.9.0/pubnub-10.9.0.tar.gz + package-name: pubnub-10.9.1 + location: https://github.com/pubnub/python/releases/download/10.9.1/pubnub-10.9.1.tar.gz supported-platforms: supported-operating-systems: Linux: @@ -169,6 +169,11 @@ sdks: license-url: https://github.com/encode/httpx/blob/master/LICENSE.md is-required: Required changelog: + - date: 2026-11-23 + version: 10.9.1 + changes: + - type: bug + text: "Unsubscribing with the default `PubNubAsyncio` client now sends a leave request unless `suppress_leave_events` is set, as the threaded and legacy asyncio subscription managers already do. The event engine subscription manager used to send it only when `enable_presence_heartbeat` was on, so the user ID stayed an occupant of the channel until the presence timeout." - date: 2026-11-20 version: 10.9.0 changes: diff --git a/CHANGELOG.md b/CHANGELOG.md index fb33d932..7c672261 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 10.9.1 +November 23 2026 + +#### Fixed +- Unsubscribing with the default `PubNubAsyncio` client now sends a leave request unless `suppress_leave_events` is set, as the threaded and legacy asyncio subscription managers already do. The event engine subscription manager used to send it only when `enable_presence_heartbeat` was on, so the user ID stayed an occupant of the channel until the presence timeout. + ## 10.9.0 November 20 2026 diff --git a/pubnub/pubnub_asyncio.py b/pubnub/pubnub_asyncio.py index 641e92ab..c36cca66 100644 --- a/pubnub/pubnub_asyncio.py +++ b/pubnub/pubnub_asyncio.py @@ -627,6 +627,21 @@ def adapt_unsubscribe_builder(self, unsubscribe_operation): groups=unsubscribe_operation.channel_groups_without_presence, suppress_leave=self._pubnub.config.suppress_leave_events )) + elif not self._pubnub.config.suppress_leave_events: + # Without presence heartbeats the presence engine is idle, so it sends no leave. The other subscription + # managers send one unless suppress_leave_events is set, and so must this one. + self._send_leave(unsubscribe_operation) + + def _send_leave(self, unsubscribe_operation): + channels = unsubscribe_operation.channels_without_presence + groups = unsubscribe_operation.channel_groups_without_presence + if not channels and not groups: + return + asyncio.ensure_future(self._send_leave_helper(channels, groups)) + + async def _send_leave_helper(self, channels, groups): + envelope = await Leave(self._pubnub).channels(channels).channel_groups(groups).future() + self._listener_manager.announce_status(envelope.status) def adapt_state_builder(self, state_operation): self.state_container.register_state(state_operation.state, diff --git a/setup.py b/setup.py index 48ba1adf..a4b21c3b 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='pubnub', - version='10.9.0', + version='10.9.1', description='PubNub Real-time push service in the cloud', author='PubNub', author_email='support@pubnub.com', diff --git a/tests/integrational/asyncio/test_subscribe.py b/tests/integrational/asyncio/test_subscribe.py index 5d1b4cb4..8c080b29 100644 --- a/tests/integrational/asyncio/test_subscribe.py +++ b/tests/integrational/asyncio/test_subscribe.py @@ -306,6 +306,48 @@ async def test_cg_subscribe_publish_unsubscribe(): await pubnub.stop() +async def occupants(pubnub, channel): + envelope = await pubnub.here_now().channels(channel).include_uuids(True).future() + assert not envelope.is_error(), envelope.status.error_data + return [o.uuid for c in envelope.result.channels for o in (c.occupants or [])] + + +async def wait_for_occupants(pubnub, channel, expected, seconds): + deadline = asyncio.get_running_loop().time() + seconds + found = await occupants(pubnub, channel) + while sorted(found) != sorted(expected) and asyncio.get_running_loop().time() < deadline: + await asyncio.sleep(0.5) + found = await occupants(pubnub, channel) + return found + + +@pytest.mark.asyncio +async def test_unsubscribe_sends_leave_without_presence_heartbeat(): + """The default client (enable_presence_heartbeat is False) tells the service that it left, so it is not an + occupant any more. Without the leave it stays listed until the presence timeout (300 s by default).""" + channel = gen_channel("test-subscribe-asyncio-leave-no-heartbeat-ch") + messenger_config = pnconf_env_copy() + messenger_config.enable_subscribe = True + messenger_config.uuid = "test-subscribe-asyncio-quiet-messenger" + messenger = PubNubAsyncio(messenger_config) + observer_config = pnconf_env_copy() + observer_config.uuid = "test-subscribe-asyncio-quiet-observer" + observer = PubNubAsyncio(observer_config) + + listener = SubscribeListener() + messenger.add_listener(listener) + messenger.subscribe().channels(channel).execute() + await listener.wait_for_connect() + assert await wait_for_occupants(observer, channel, [messenger.uuid], 15) == [messenger.uuid] + + messenger.unsubscribe().channels(channel).execute() + + assert await wait_for_occupants(observer, channel, [], 15) == [] + + await messenger.stop() + await observer.stop() + + # @pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/subscription/cg_join_leave.json', serializer='pn_json', # filter_query_parameters=['pnsdk', 'l_cg', 'l_pres', 'ee', 'tr']) @pytest.mark.asyncio diff --git a/tests/unit/test_asyncio_unsubscribe_leave.py b/tests/unit/test_asyncio_unsubscribe_leave.py new file mode 100644 index 00000000..e012f5a9 --- /dev/null +++ b/tests/unit/test_asyncio_unsubscribe_leave.py @@ -0,0 +1,111 @@ +"""Unsubscribing with the default asyncio client sends a leave request unless suppress_leave_events is set. + +The event engine subscription manager used to send it only when presence heartbeats were on, so a default client +stayed an occupant of the channel until the presence timeout. The sync client, the legacy asyncio manager and the +JavaScript SDK all send the leave by default. +""" +import asyncio +from types import SimpleNamespace +from unittest.mock import Mock, patch + +import pytest + +from pubnub.dtos import UnsubscribeOperation +from pubnub.event_engine.models import events +from pubnub.pubnub_asyncio import PubNubAsyncio +from tests.helper import pnconf_copy as _pnconf_copy + + +def pnconf_copy(): + config = _pnconf_copy() + config.enable_subscribe = True + return config + + +class FakeLeave: + """Stands in for the Leave endpoint: records the channels and groups, and answers with an acknowledgement.""" + + calls = [] + + def __init__(self, pubnub): + self._channels, self._groups = [], [] + + def channels(self, channels): + self._channels = channels + return self + + def channel_groups(self, groups): + self._groups = groups + return self + + async def future(self): + FakeLeave.calls.append((self._channels, self._groups)) + return SimpleNamespace(status=SimpleNamespace(category='ack')) + + +@pytest.fixture(autouse=True) +def fake_leave(): + FakeLeave.calls = [] + with patch('pubnub.pubnub_asyncio.Leave', FakeLeave): + yield + + +async def unsubscribe(config, channels=(), groups=()): + pubnub = PubNubAsyncio(config) + manager = pubnub._subscription_manager + manager.presence_engine.trigger = Mock() + announced = [] + manager._listener_manager.announce_status = announced.append + manager.adapt_unsubscribe_builder(UnsubscribeOperation(channels=list(channels), channel_groups=list(groups))) + await asyncio.sleep(0) + await asyncio.sleep(0) + await pubnub.stop() + return manager, announced + + +@pytest.mark.asyncio +async def test_default_config_sends_leave_for_channels_and_groups(): + manager, announced = await unsubscribe(pnconf_copy(), ['my_channel'], ['my_group']) + + assert FakeLeave.calls == [(['my_channel'], ['my_group'])] + assert [s.category for s in announced] == ['ack'] + manager.presence_engine.trigger.assert_not_called() + + +@pytest.mark.asyncio +async def test_presence_channels_are_not_left_twice(): + await unsubscribe(pnconf_copy(), ['my_channel', 'my_channel-pnpres']) + + assert FakeLeave.calls == [(['my_channel'], [])] + + +@pytest.mark.asyncio +async def test_suppress_leave_events_sends_no_leave(): + config = pnconf_copy() + config.suppress_leave_events = True + + await unsubscribe(config, ['my_channel']) + + assert FakeLeave.calls == [] + + +@pytest.mark.asyncio +async def test_nothing_to_leave_sends_no_request(): + await unsubscribe(pnconf_copy()) + + assert FakeLeave.calls == [] + + +@pytest.mark.asyncio +async def test_with_presence_heartbeat_the_presence_engine_leaves_and_no_second_request_is_sent(): + config = pnconf_copy() + config.enable_presence_heartbeat = True + config.set_presence_timeout_with_custom_interval(30, 10) + + manager, _ = await unsubscribe(config, ['my_channel']) + + assert FakeLeave.calls == [] + event = manager.presence_engine.trigger.call_args.kwargs['event'] + assert isinstance(event, events.HeartbeatLeftEvent) + assert event.channels == ['my_channel'] + assert event.suppress_leave is False From d753a746274cdbf67f6cece7fc5e6c832d5cc6d2 Mon Sep 17 00:00:00 2001 From: Mateusz Wiktor Date: Mon, 23 Nov 2026 13:23:45 +0100 Subject: [PATCH 2/5] Remove the live here_now test of the leave It needs a keyset that lists a client as an occupant when it subscribes without presence heartbeats. The keysets of the CI run do not, so the test failed there although the change works (it passes on a keyset with that presence setting). The offline unit tests cover the change on any keyset. Co-Authored-By: Claude Sonnet 5.5 --- tests/integrational/asyncio/test_subscribe.py | 42 ------------------- 1 file changed, 42 deletions(-) diff --git a/tests/integrational/asyncio/test_subscribe.py b/tests/integrational/asyncio/test_subscribe.py index 8c080b29..5d1b4cb4 100644 --- a/tests/integrational/asyncio/test_subscribe.py +++ b/tests/integrational/asyncio/test_subscribe.py @@ -306,48 +306,6 @@ async def test_cg_subscribe_publish_unsubscribe(): await pubnub.stop() -async def occupants(pubnub, channel): - envelope = await pubnub.here_now().channels(channel).include_uuids(True).future() - assert not envelope.is_error(), envelope.status.error_data - return [o.uuid for c in envelope.result.channels for o in (c.occupants or [])] - - -async def wait_for_occupants(pubnub, channel, expected, seconds): - deadline = asyncio.get_running_loop().time() + seconds - found = await occupants(pubnub, channel) - while sorted(found) != sorted(expected) and asyncio.get_running_loop().time() < deadline: - await asyncio.sleep(0.5) - found = await occupants(pubnub, channel) - return found - - -@pytest.mark.asyncio -async def test_unsubscribe_sends_leave_without_presence_heartbeat(): - """The default client (enable_presence_heartbeat is False) tells the service that it left, so it is not an - occupant any more. Without the leave it stays listed until the presence timeout (300 s by default).""" - channel = gen_channel("test-subscribe-asyncio-leave-no-heartbeat-ch") - messenger_config = pnconf_env_copy() - messenger_config.enable_subscribe = True - messenger_config.uuid = "test-subscribe-asyncio-quiet-messenger" - messenger = PubNubAsyncio(messenger_config) - observer_config = pnconf_env_copy() - observer_config.uuid = "test-subscribe-asyncio-quiet-observer" - observer = PubNubAsyncio(observer_config) - - listener = SubscribeListener() - messenger.add_listener(listener) - messenger.subscribe().channels(channel).execute() - await listener.wait_for_connect() - assert await wait_for_occupants(observer, channel, [messenger.uuid], 15) == [messenger.uuid] - - messenger.unsubscribe().channels(channel).execute() - - assert await wait_for_occupants(observer, channel, [], 15) == [] - - await messenger.stop() - await observer.stop() - - # @pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/subscription/cg_join_leave.json', serializer='pn_json', # filter_query_parameters=['pnsdk', 'l_cg', 'l_pres', 'ee', 'tr']) @pytest.mark.asyncio From a1c15859ded477477e31766556b5428ccebaea5c Mon Sep 17 00:00:00 2001 From: Mateusz Wiktor Date: Mon, 23 Nov 2026 13:30:56 +0100 Subject: [PATCH 3/5] Mention the missing leave event in the changelog Co-Authored-By: Claude Sonnet 5.5 --- .pubnub.yml | 2 +- CHANGELOG.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.pubnub.yml b/.pubnub.yml index 22406a60..37ddcb48 100644 --- a/.pubnub.yml +++ b/.pubnub.yml @@ -173,7 +173,7 @@ changelog: version: 10.9.1 changes: - type: bug - text: "Unsubscribing with the default `PubNubAsyncio` client now sends a leave request unless `suppress_leave_events` is set, as the threaded and legacy asyncio subscription managers already do. The event engine subscription manager used to send it only when `enable_presence_heartbeat` was on, so the user ID stayed an occupant of the channel until the presence timeout." + text: "Unsubscribing with the default `PubNubAsyncio` client now sends a leave request unless `suppress_leave_events` is set, as the threaded and legacy asyncio subscription managers already do. The event engine subscription manager used to send it only when `enable_presence_heartbeat` was on, so the user ID stayed an occupant of the channel until the presence timeout and the other subscribers did not receive its `leave` presence event." - date: 2026-11-20 version: 10.9.0 changes: diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c672261..1df89f49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ November 23 2026 #### Fixed -- Unsubscribing with the default `PubNubAsyncio` client now sends a leave request unless `suppress_leave_events` is set, as the threaded and legacy asyncio subscription managers already do. The event engine subscription manager used to send it only when `enable_presence_heartbeat` was on, so the user ID stayed an occupant of the channel until the presence timeout. +- Unsubscribing with the default `PubNubAsyncio` client now sends a leave request unless `suppress_leave_events` is set, as the threaded and legacy asyncio subscription managers already do. The event engine subscription manager used to send it only when `enable_presence_heartbeat` was on, so the user ID stayed an occupant of the channel until the presence timeout and the other subscribers did not receive its `leave` presence event. ## 10.9.0 November 20 2026 From 0d26ce72358a9cb80ab25075e83221c6d658e940 Mon Sep 17 00:00:00 2001 From: Mateusz Wiktor Date: Mon, 23 Nov 2026 13:33:19 +0100 Subject: [PATCH 4/5] Leave the version and changelog to the release bot Co-Authored-By: Claude Sonnet 5.5 --- .pubnub.yml | 13 ++++--------- CHANGELOG.md | 6 ------ setup.py | 2 +- 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/.pubnub.yml b/.pubnub.yml index 37ddcb48..145c262b 100644 --- a/.pubnub.yml +++ b/.pubnub.yml @@ -1,5 +1,5 @@ name: python -version: 10.9.1 +version: 10.9.0 schema: 1 scm: github.com/pubnub/python sdks: @@ -18,7 +18,7 @@ sdks: distributions: - distribution-type: library distribution-repository: package - package-name: pubnub-10.9.1 + package-name: pubnub-10.9.0 location: https://pypi.org/project/pubnub/ supported-platforms: supported-operating-systems: @@ -94,8 +94,8 @@ sdks: - distribution-type: library distribution-repository: git release - package-name: pubnub-10.9.1 - location: https://github.com/pubnub/python/releases/download/10.9.1/pubnub-10.9.1.tar.gz + package-name: pubnub-10.9.0 + location: https://github.com/pubnub/python/releases/download/10.9.0/pubnub-10.9.0.tar.gz supported-platforms: supported-operating-systems: Linux: @@ -169,11 +169,6 @@ sdks: license-url: https://github.com/encode/httpx/blob/master/LICENSE.md is-required: Required changelog: - - date: 2026-11-23 - version: 10.9.1 - changes: - - type: bug - text: "Unsubscribing with the default `PubNubAsyncio` client now sends a leave request unless `suppress_leave_events` is set, as the threaded and legacy asyncio subscription managers already do. The event engine subscription manager used to send it only when `enable_presence_heartbeat` was on, so the user ID stayed an occupant of the channel until the presence timeout and the other subscribers did not receive its `leave` presence event." - date: 2026-11-20 version: 10.9.0 changes: diff --git a/CHANGELOG.md b/CHANGELOG.md index 1df89f49..fb33d932 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,3 @@ -## 10.9.1 -November 23 2026 - -#### Fixed -- Unsubscribing with the default `PubNubAsyncio` client now sends a leave request unless `suppress_leave_events` is set, as the threaded and legacy asyncio subscription managers already do. The event engine subscription manager used to send it only when `enable_presence_heartbeat` was on, so the user ID stayed an occupant of the channel until the presence timeout and the other subscribers did not receive its `leave` presence event. - ## 10.9.0 November 20 2026 diff --git a/setup.py b/setup.py index a4b21c3b..48ba1adf 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='pubnub', - version='10.9.1', + version='10.9.0', description='PubNub Real-time push service in the cloud', author='PubNub', author_email='support@pubnub.com', From abbc66fdb42fa9fbfb457b446978bd3a083d8b1c Mon Sep 17 00:00:00 2001 From: PubNub Release Bot <120067856+pubnub-release-bot@users.noreply.github.com> Date: Mon, 23 Nov 2026 13:12:03 +0000 Subject: [PATCH 5/5] PubNub SDK 10.9.1 release. --- .pubnub.yml | 15 +++++++++++---- CHANGELOG.md | 9 +++++++++ setup.py | 2 +- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/.pubnub.yml b/.pubnub.yml index 145c262b..74177033 100644 --- a/.pubnub.yml +++ b/.pubnub.yml @@ -1,5 +1,5 @@ name: python -version: 10.9.0 +version: 10.9.1 schema: 1 scm: github.com/pubnub/python sdks: @@ -18,7 +18,7 @@ sdks: distributions: - distribution-type: library distribution-repository: package - package-name: pubnub-10.9.0 + package-name: pubnub-10.9.1 location: https://pypi.org/project/pubnub/ supported-platforms: supported-operating-systems: @@ -94,8 +94,8 @@ sdks: - distribution-type: library distribution-repository: git release - package-name: pubnub-10.9.0 - location: https://github.com/pubnub/python/releases/download/10.9.0/pubnub-10.9.0.tar.gz + package-name: pubnub-10.9.1 + location: https://github.com/pubnub/python/releases/download/10.9.1/pubnub-10.9.1.tar.gz supported-platforms: supported-operating-systems: Linux: @@ -169,6 +169,13 @@ sdks: license-url: https://github.com/encode/httpx/blob/master/LICENSE.md is-required: Required changelog: + - date: 2026-11-23 + version: 10.9.1 + changes: + - type: bug + text: "Unsubscribing with the default `PubNubAsyncio` client now sends a leave request unless `suppress_leave_events` is set, as the threaded and legacy asyncio subscription managers already do. The event engine subscription manager used to send it only when `enable_presence_heartbeat` was on, so the user ID stayed an occupant of the channel until the presence timeout and the other subscribers did not receive its `leave` presence event." + - type: improvement + text: "Add offline unit tests for the leave request on unsubscribe." - date: 2026-11-20 version: 10.9.0 changes: diff --git a/CHANGELOG.md b/CHANGELOG.md index fb33d932..588fd4fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## 10.9.1 +November 23 2026 + +#### Fixed +- Unsubscribing with the default `PubNubAsyncio` client now sends a leave request unless `suppress_leave_events` is set, as the threaded and legacy asyncio subscription managers already do. The event engine subscription manager used to send it only when `enable_presence_heartbeat` was on, so the user ID stayed an occupant of the channel until the presence timeout and the other subscribers did not receive its `leave` presence event. + +#### Modified +- Add offline unit tests for the leave request on unsubscribe. + ## 10.9.0 November 20 2026 diff --git a/setup.py b/setup.py index 48ba1adf..a4b21c3b 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='pubnub', - version='10.9.0', + version='10.9.1', description='PubNub Real-time push service in the cloud', author='PubNub', author_email='support@pubnub.com',