Session not restored when launching via SDK with cdp=True (Telegram Web logs out on every restart)
Environment:
• OS: Ubuntu 22.04 x64, headless server with Xvfb :99 • shardx (Python SDK) 2.1.2, engine Chromium 156 (bundled cache) • Launched via SDK: sdk.launch(profile, cdp=True, proxy="socks5://...") — profile created with create_profile("win-gtx1060"), saved profile under profiles_dir • Proxy: SOCKS5, static exit IP (verified identical across runs)
Problem: A persistent (saved) profile does not restore its session when relaunched with cdp=True. Repro:
- Launch profile with cdp=True, open https://web.telegram.org/k/, log in with phone code → chat list renders fine (≈3 s).
- Stop the session (BrowserSession.stop() → SIGTERM → wait 5 s → kill).
- Relaunch the same profile with cdp=True and navigate to https://web.telegram.org/k/.
- Expected: session restored, chat list appears. Actual: Telegram Web shows the QR / "LOG IN BY PHONE NUMBER" screen every time.
Evidence that state IS on disk: after step 2, Local Storage/leveldb/*.log contains xt_instance, number_of_accounts, account1..4, dc2_auth_key, dc4_auth_key; IndexedDB contains tweb, tweb-account-1..4. So auth data persists, but is not picked up on the next cold load.
Root cause I found in the SDK source (shardx/browser.py, line ~176):
if not headless and not cdp: argv += ["--restore-last-session", "--hide-crash-restore-bubble"]
The restore flag is only added when cdp=False. Since automation always launches with cdp=True, Chromium never restores the last session — so every automated launch is a cold start, and Telegram Web K (which keeps its auth in IndexedDB with deferred writes) either restores the account or falls back to QR depending on whether the IDB write landed before stop().
Questions:
- Is there a supported way to get session restore when cdp=True? Would passing --restore-last-session via extra_args= be safe, or is it intentionally excluded for CDP sessions?
- BrowserSession.stop() does SIGTERM → 5 s → SIGKILL. Is there a graceful shutdown path (e.g. CDP Browser.close) so IndexedDB/localStorage flush before the process dies? A hard kill within 5 s of an IDB write is a plausible cause of the "half-session" state.
- Recommended pattern for long-lived logged-in sessions (Telegram Web / similar IDB-heavy apps) in server-side SDK automation: keep the profile running, or full stop/start cycles?
Addendum — two findings after further testing:
-
--restore-last-session via extra_args does NOT solve it. It restores tabs (URL list), not the in-page auth state; the profile still lands on the QR screen on every automated relaunch.
-
The failure is intermittent, not absolute. Right after onboard (login + canary message sent in the same browser session) the very first relaunch restored the session in ~3.2 s and the chat list rendered. Every relaunch after that lands on QR. So the profile can restore — it just stops doing so after one clean cycle.
-
Explicit ask: is there a supported graceful-shutdown path so IndexedDB/localStorage flush before the process dies? BrowserSession.stop() currently does SIGTERM → wait(5) → SIGKILL, and Telegram Web K writes its auth state to IndexedDB with deferred commits. A hard kill 5 s after the last IDB write would produce exactly this "half-session" signature.
-
Second explicit ask: is there a recommended pattern for long-lived logged-in sessions in server-side SDK automation — keep the profile running between jobs, or is full stop/start cycles supported for IDB-heavy apps?
in v2.1.3