Fix Telegram client startup under uvicorn in Docker#6
Conversation
Defer Pyrogram Client creation until the FastAPI lifespan runs so clients bind to uvicorn's event loop instead of failing with "Future attached to a different loop". Register bot handlers after clients are built, resolve stale tg_client imports in auth/streaming routers, and mount the session volume at /app/session so rebuilds are not masked by a persisted /app tree. Fixes subinps#5 Co-authored-by: Cursor <cursoragent@cursor.com>
📝 WalkthroughWalkthroughTelegram client pool construction is moved from module-import time into a new Lazy Telegram Client Initialization
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@backend/app/routers/streaming.py`:
- Around line 153-156: The thumbnail fallback in the streaming route is
swallowing all `download_media(...)` exceptions, which turns real
client/session/API errors into a misleading later “Thumbnail not found”
response. Update the exception handling in the thumbnail retrieval logic in
`streaming.py` to only suppress the expected not-found case, and add logging for
unexpected failures instead of using a bare `pass`. Keep the fix localized
around the `telegram.tg_client.download_media(...)` fallback path and the
surrounding response handling.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 8cea2854-c397-4f8f-999d-53ccae561bf4
📒 Files selected for processing (5)
backend/app/main.pybackend/app/routers/auth.pybackend/app/routers/streaming.pybackend/app/telegram.pydocker-compose.yml
💤 Files with no reviewable changes (1)
- backend/app/main.py
| thumb_bytes = await telegram.tg_client.download_media(file.thumbnail_file_id, in_memory=True) | ||
| return Response(content=thumb_bytes.getvalue(), media_type="image/jpeg") | ||
| except Exception: | ||
| pass |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Don’t turn fallback download failures into a fake 404.
Lines 153-156 swallow every download_media(...) failure, so client/session/API problems are silently converted into the later “Thumbnail not found” path. Please narrow this to the expected not-found case and log unexpected failures instead of pass.
🧰 Tools
🪛 Ruff (0.15.18)
[error] 155-156: try-except-pass detected, consider logging the exception
(S110)
[warning] 155-155: Do not catch blind exception: Exception
(BLE001)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@backend/app/routers/streaming.py` around lines 153 - 156, The thumbnail
fallback in the streaming route is swallowing all `download_media(...)`
exceptions, which turns real client/session/API errors into a misleading later
“Thumbnail not found” response. Update the exception handling in the thumbnail
retrieval logic in `streaming.py` to only suppress the expected not-found case,
and add logging for unexpected failures instead of using a bare `pass`. Keep the
fix localized around the `telegram.tg_client.download_media(...)` fallback path
and the surrounding response handling.
Source: Linters/SAST tools
Summary
Fixes #5 — backend bot client fails on Docker startup with
Future attached to a different loop.Clientcreation until FastAPI lifespan runs (binds to uvicorn's event loop)bothandlers after clients are builttelegram.tg_clientin auth/streaming routers to avoid staleNoneimports/app/sessioninstead of/appso image rebuilds are not maskedTest plan
docker compose up -d --buildon clean volumeGET /api/auth/bot/inforeturns bot username/startand file upload (requires storage channel admin setup)Made with Cursor
Summary by CodeRabbit
Bug Fixes
Chores