Harden TDLib large-media download path and address review follow-ups#18
Harden TDLib large-media download path and address review follow-ups#18speech115 wants to merge 2 commits into
Conversation
- Fix hang on unauthorized TDLib session: start(wait_login=False) plus a bounded readiness poll (start_client_ready) so a revoked/absent session raises TdlibDownloadError and falls back to Telethon instead of blocking forever while holding the download lock. - Make try_acquire_with_timeout async (asyncio.sleep) so lock polling no longer blocks the event loop shared with the live Telethon client. - Copy the downloaded file to dest inside download_via_tdlib and delete the TDLib-side copy (best-effort) so files_directory doesn't grow unbounded. - Support messageAnimation in extract_file_id_from_message so large GIFs routed as video/* don't dead-end into a wasted TDLib start + fallback. - Make the DB encryption key configurable via TELEGRAM_TDLIB_DB_ENCRYPTION_KEY instead of a hardcoded public constant guarding the account auth key. - Tolerate a malformed TELEGRAM_TDLIB_DOWNLOAD_THRESHOLD_MB instead of failing the whole download. - Doc/.env updates for the new key and the readiness-timeout fallback. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5b3405ba9b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| files_directory=str(session_dir), | ||
| ) | ||
| await client.start() | ||
| await start_client_ready(client) |
There was a problem hiding this comment.
Останавливайте TDLib-клиент при таймауте готовности
Если TDLib-сессия не авторизована или отозвана, start_client_ready() теперь поднимает TdlibDownloadError после таймаута, но этот вызов находится до try/finally, который делает await client.stop(). В этом сценарии уже запущенный pytdbot.Client не останавливается перед fallback на Telethon, поэтому повторные большие скачивания со stale-сессией могут оставлять фоновые TDLib-клиенты/открытую базу, хотя документация обещает только ограниченную задержку; оберните старт и последующую работу одним try/finally после создания клиента.
Useful? React with 👍 / 👎.
- Guard against the TDLib path resolving a different message than Telethon: getMessageLinkInfo re-resolves the raw t.me URL, which for ?comment=/thread links can land on another message. assert_expected_size compares downloaded bytes to the size Telethon saw and raises TdlibDownloadError on mismatch so the caller falls back to the correctly-resolved Telethon download. - Surface real progress on large TDLib downloads: when not quiet, download asynchronously and poll getFile, feeding the same _make_progress callback the Telethon path uses, instead of blocking silently for minutes on multi-gig files. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Summary
Closes all 7 ultrareview findings on the TDLib large-media download path.
First commit (
5b3405b) addressed:TELEGRAM_TDLIB_DB_ENCRYPTION_KEYinstead of a hardcoded constant.try_acquire_with_timeoutis now async (asyncio.sleep) so lock polling no longer blocks the event loop shared with the live Telethon client.deleteFilethe TDLib-side copy (best-effort) sofiles_directorydoesn't grow unbounded.messageAnimationsupported inextract_file_id_from_messageso large GIFs routed asvideo/*don't dead-end into a wasted start + fallback.TELEGRAM_TDLIB_DOWNLOAD_THRESHOLD_MBinstead of failing the whole download.start_client_readybounds the readiness wait so a revoked/absent session falls back instead of blocking forever.Second commit (
7faf6b7) closes the remaining two:t.meURL viagetMessageLinkInfo, which for?comment=/thread links can land on a different message than the Telethon path (which resolves by parsedchat/message_id). Newassert_expected_size()compares the downloaded byte count to the size Telethon saw and raisesTdlibDownloadErroron mismatch, so the caller safely falls back to the correctly-resolved Telethon download instead of silently saving the wrong file.synchronous=Trueand stayed silent for minutes. When notquiet, downloads now run asynchronously and pollgetFile, feeding the same_make_progresscallback the Telethon path already uses.quietmode still downloads synchronously and silently.Verification
PYTHONPATH=src .venv/bin/python -m unittest tests.test_tdlib_download tests.test_download_postFull
mcp/testssuite (unittest discover -s tests -p 'test_*.py'):Ran 412 tests — failures=1, errors=9Ran 416 tests — failures=1, errors=9The 1 failure + 9 errors are pre-existing, in
test_runtime.py(pydanticSettingsrequiresapi_id/api_hash, absent from this worktree's env; reproduces on clean HEAD). No new failures; the 4 added tests (AssertExpectedSizeTests,DownloadWithProgressTests) pass.compileallandgit diff --check(whitespace) clean.🤖 Generated with Claude Code