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/aiousbwatcher/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _async_unregister_callback(self, callback: Callable[[], None]) -> None:
self._callbacks.remove(callback)

def _async_call_callbacks(self) -> None:
for callback in self._callbacks:
for callback in list(self._callbacks):
try:
callback()
except Exception as e:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ def broken_callback() -> None:
assert not called


@pytest.mark.asyncio
async def test_aiousbwatcher_callback_unregisters_during_fire() -> None:
watcher = AIOUSBWatcher()
fired: list[str] = []

def first() -> None:
fired.append("first")
unregister_first()

def second() -> None:
fired.append("second")

unregister_first = watcher.async_register_callback(first)
watcher.async_register_callback(second)

watcher._async_call_callbacks()

assert sorted(fired) == ["first", "second"]


@pytest.mark.asyncio
@pytest.mark.skipif(
platform != "linux", reason="Inotify not available on this platform"
Expand Down
Loading