Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: python
version: 10.9.0
version: 10.9.1
schema: 1
scm: github.com/pubnub/python
sdks:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
15 changes: 15 additions & 0 deletions pubnub/pubnub_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
111 changes: 111 additions & 0 deletions tests/unit/test_asyncio_unsubscribe_leave.py
Original file line number Diff line number Diff line change
@@ -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
Loading