Skip to content

Commit 0dd9a5a

Browse files
CopilotZeliardM
andcommitted
Fix: Improve deprecated method implementations and __del__ safety
Co-authored-by: ZeliardM <140266236+ZeliardM@users.noreply.github.com>
1 parent 73bd4ac commit 0dd9a5a

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

src/arlo/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def __init__(self, nativeId: str, arlo_device: dict, arlo_basestation: dict, arl
3939

4040
def __del__(self) -> None:
4141
self.stop_subscriptions = True
42-
asyncio.create_task(self.cancel_tasks()) # Cancel all tasks in destructor
42+
# Cancel all tasks synchronously - safe for destructor
43+
self.cancel_pending_tasks()
4344
self._cleanup()
4445

4546
async def _delayed_init(self) -> None:

src/arlo/util.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,39 @@ def cancel_pending_tasks(self, tag: Optional[str] = None) -> None:
143143
"""
144144
DEPRECATED: Use cancel_tasks() instead.
145145
Kept for backward compatibility during migration.
146+
147+
Note: This is a synchronous wrapper that schedules the async cancel_tasks.
148+
For proper cleanup, use the async cancel_tasks() directly.
146149
"""
147-
asyncio.create_task(self.cancel_tasks(tags=tag, await_completion=False))
150+
if not hasattr(self, 'background_tasks'):
151+
return
152+
# Synchronous cancellation - just cancel without awaiting
153+
tag_set = None
154+
if tag is not None:
155+
tag_set = {tag} if isinstance(tag, str) else set(tag)
156+
157+
for task in list(self.background_tasks):
158+
task_tag = getattr(task, '_task_tag', None)
159+
should_cancel = False
160+
if tag_set is None:
161+
should_cancel = True
162+
elif task_tag in tag_set:
163+
should_cancel = True
164+
165+
if should_cancel:
166+
if not task.done():
167+
task.cancel()
168+
self.background_tasks.discard(task)
148169

149170
def cancel_tasks_by_tag(self, tag: str) -> None:
150171
"""
151172
DEPRECATED: Use cancel_tasks(tag) instead.
152173
Kept for backward compatibility during migration.
174+
175+
Note: This is a synchronous wrapper. For proper cleanup with await,
176+
use the async cancel_tasks() directly.
153177
"""
154-
asyncio.create_task(self.cancel_tasks(tags=tag, await_completion=False))
178+
self.cancel_pending_tasks(tag)
155179

156180
async def cancel_and_await_tasks_by_tag(self, tag: str) -> None:
157181
"""

src/arlo/webrtc_sip.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ def __init__(self, intercom: ArloIntercom) -> None:
212212

213213
def __del__(self) -> None:
214214
self.stop_subscriptions = True
215-
asyncio.create_task(self.cancel_tasks()) # Cancel all tasks in destructor
215+
# Cancel all tasks synchronously - safe for destructor
216+
self.cancel_pending_tasks()
216217

217218
async def delayed_init(self) -> None:
218219
try:

0 commit comments

Comments
 (0)