Skip to content

Fix asynchronous client shutdown without re-entering the event loop - #382

Open
samirhvbr wants to merge 1 commit into
isaackogan:masterfrom
samirhvbr:fix/async-client-close
Open

samirhvbr wants to merge 1 commit into
isaackogan:masterfrom
samirhvbr:fix/async-client-close

Conversation

@samirhvbr

Copy link
Copy Markdown

Awaiting client.close() from an ordinary coroutine currently raises RuntimeError: This event loop is already running and produces an unawaited-disconnect warning. disconnect(close_client=True) reaches the same path. This reproduces on master at 4158e6a (7.0.1) without contacting TikTok:

import asyncio
from TikTokLive import TikTokLiveClient

async def main():
    client = TikTokLiveClient(unique_id="@creator")
    await client.close()

asyncio.run(main())

Make async close() await this client's disconnection and close the HTTP session in finally, instead of calling the synchronous loop-driving _clean_tasks(). Let disconnect(close_client=True) close the HTTP session directly so it does not recurse through close(). This drains the client's WebSocket task and resets its state while leaving the application's loop and unrelated tasks running. The HTTP session is still closed if WebSocket cleanup fails.

Update the README and remove the now-obsolete async-close limitation/workarounds from the documentation. Public signatures are unchanged; this PR focuses on these two shutdown entry points.

Validation on macOS / Apple Silicon:

  • All four new regressions fail before the fix and pass afterward. They cover both shutdown entry points, unrelated-task isolation, draining/resetting client state, and HTTP cleanup on a WebSocket error.
  • python -m pytest: 21 passed on each of Python 3.10, 3.11, and 3.12.
  • Documentation script tests: 25 passed; the pinned-pnpm documentation build passes its content checks, generation, and internal-link validation; SEO gate: 18 passed.
  • The exact curated mypy command from .github/workflows/lint.yml passes. The changed client module also passes with --disable-error-code unused-ignore. The full-tree command from CONTRIBUTING retains the same two pre-existing unused type: ignore[misc] errors in listener decorators that were present before this change.
  • No live TikTok/signing-service requests or Windows/Linux runtime session was exercised. WebSocket task cleanup uses an offline fixture; the idle-client and HTTP-session-close checks use the real client.

AI assistance: prepared with Codex on behalf of @samirhvbr; the reported reproductions and checks were executed locally.

@isaackogan

Copy link
Copy Markdown
Owner

Can I get a reviewer for this?

@AndersonBY AndersonBY left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit 67ae313. All 21 tests pass on Python 3.12/Linux, and both shutdown entry points work with an established local WebSocket connection. One remaining shutdown case needs attention:

[P2] Handle shutdown while the WebSocket connection is still being established

If close() runs immediately after await start(), or during the WebSocket handshake, the underlying WebSocket disconnect returns early because connected is still false. The client then waits for its connection task without stopping it. The socket can subsequently connect and remain active, leaving close() pending and the HTTP session open until the peer disconnects or another disconnect call occurs.

This was reproduced with the real client and a local WebSocket server, mocking only the signed-response fetch. A second explicit disconnect released the pending close.

The underlying disconnect race predates this PR. This change exposes it through close() and moves HTTP cleanup behind that wait; previously, close() closed HTTP before raising the event-loop RuntimeError.

@isaackogan

Copy link
Copy Markdown
Owner

Ty, @samirhvbr can you look into this? If you're able to address the concerns, I am happy to merge and release today.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants