Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/auth/src/supabase_auth/_async/gotrue_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ def _notify_all_subscribers(
event: AuthChangeEvent,
session: Optional[Session],
) -> None:
for subscription in self._state_change_emitters.values():
for subscription in list(self._state_change_emitters.values()):
subscription.callback(event, session)

def _get_valid_session(
Expand Down
2 changes: 1 addition & 1 deletion src/auth/src/supabase_auth/_sync/gotrue_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ def _notify_all_subscribers(
event: AuthChangeEvent,
session: Optional[Session],
) -> None:
for subscription in self._state_change_emitters.values():
for subscription in list(self._state_change_emitters.values()):
subscription.callback(event, session)

def _get_valid_session(
Expand Down
31 changes: 31 additions & 0 deletions src/auth/tests/_async/test_subscriptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Optional

from supabase_auth.types import AuthChangeEvent, Session

from .clients import auth_client


async def test_auth_state_change_callback_can_unsubscribe_during_notification() -> None:
client = auth_client()
received: list[tuple[str, AuthChangeEvent]] = []

def unsubscribe_on_event(
event: AuthChangeEvent, session: Optional[Session]
) -> None:
received.append(("once", event))
subscription.unsubscribe()

subscription = client.on_auth_state_change(unsubscribe_on_event)
client.on_auth_state_change(
lambda event, session: received.append(("always", event))
)

await client.sign_out()
assert received == [("once", "SIGNED_OUT"), ("always", "SIGNED_OUT")]

await client.sign_out()
assert received == [
("once", "SIGNED_OUT"),
("always", "SIGNED_OUT"),
("always", "SIGNED_OUT"),
]
31 changes: 31 additions & 0 deletions src/auth/tests/_sync/test_subscriptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Optional

from supabase_auth.types import AuthChangeEvent, Session

from .clients import auth_client


def test_auth_state_change_callback_can_unsubscribe_during_notification() -> None:
client = auth_client()
received: list[tuple[str, AuthChangeEvent]] = []

def unsubscribe_on_event(
event: AuthChangeEvent, session: Optional[Session]
) -> None:
received.append(("once", event))
subscription.unsubscribe()

subscription = client.on_auth_state_change(unsubscribe_on_event)
client.on_auth_state_change(
lambda event, session: received.append(("always", event))
)

client.sign_out()
assert received == [("once", "SIGNED_OUT"), ("always", "SIGNED_OUT")]

client.sign_out()
assert received == [
("once", "SIGNED_OUT"),
("always", "SIGNED_OUT"),
("always", "SIGNED_OUT"),
]