Skip to content

Harden TDLib large-media download path and address review follow-ups#18

Open
speech115 wants to merge 2 commits into
mainfrom
claude/magical-heyrovsky-e229ba
Open

Harden TDLib large-media download path and address review follow-ups#18
speech115 wants to merge 2 commits into
mainfrom
claude/magical-heyrovsky-e229ba

Conversation

@speech115

@speech115 speech115 commented Jul 2, 2026

Copy link
Copy Markdown
Owner

Summary

Closes all 7 ultrareview findings on the TDLib large-media download path.

First commit (5b3405b) addressed:

Second commit (7faf6b7) closes the remaining two:

  • Improve installation guide for a clean local setup #4 Correctness: the TDLib path re-resolves the raw t.me URL via getMessageLinkInfo, which for ?comment=/thread links can land on a different message than the Telethon path (which resolves by parsed chat/message_id). New assert_expected_size() compares the downloaded byte count to the size Telethon saw and raises TdlibDownloadError on mismatch, so the caller safely falls back to the correctly-resolved Telethon download instead of silently saving the wrong file.
  • docs: clarify first-run install steps #7 Maintainability: large TDLib downloads used synchronous=True and stayed silent for minutes. When not quiet, downloads now run asynchronously and poll getFile, feeding the same _make_progress callback the Telethon path already uses. quiet mode still downloads synchronously and silently.

Verification

PYTHONPATH=src .venv/bin/python -m unittest tests.test_tdlib_download tests.test_download_post

Ran 44 tests in 0.308s
OK

Full mcp/tests suite (unittest discover -s tests -p 'test_*.py'):

  • clean HEAD (before second commit): Ran 412 tests — failures=1, errors=9
  • this branch: Ran 416 tests — failures=1, errors=9

The 1 failure + 9 errors are pre-existing, in test_runtime.py (pydantic Settings requires api_id/api_hash, absent from this worktree's env; reproduces on clean HEAD). No new failures; the 4 added tests (AssertExpectedSizeTests, DownloadWithProgressTests) pass. compileall and git diff --check (whitespace) clean.

🤖 Generated with Claude Code

- 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>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Останавливайте 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>
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.

1 participant