1.9 KiB
1.9 KiB
StringSession Conversion
The Problem
SQLite-based Telethon sessions deadlock when accessed in parallel. The error:
sqlite3.OperationalError: database is locked
This happens because SQLite uses file-level locking. When multiple accounts try to connect simultaneously, the .session files lock each other out.
The Fix: StringSession
Convert ALL sessions to in-memory StringSession format stored in a single JSON file. No file locking, no database contention.
Converter Script
Located at: C:\Users\User\Desktop\_PROJECTS\рассылка\session_converter.py
How It Works
# For each .session file:
client = TelegramClient(session_path, API_ID, API_HASH)
await client.connect()
ss = client.session.save() # Returns StringSession string
string_sessions[phone] = ss
await client.disconnect()
Output
generated/sessions_strings.json:
{
"12894827641": "1AgAOMTQ5...",
"14066141705": "1BQANOTE3...",
...
}
Usage in Join Script
# Load
with open("generated/sessions_strings.json") as f:
string_sessions = json.load(f)
# Use
ss = string_sessions.get(phone) or string_sessions.get(name)
if not ss:
return {"status": "no_stringsession"}
client = TelegramClient(StringSession(ss), API_ID, API_HASH)
await client.connect()
Conversion Results (2026-07-08)
- 222 sessions converted
- 137 new StringSessions created
- 9 failures (corrupted sessions)
Why NOT SQLite Retry
- WAL mode doesn't fix parallel access
- Retry with exponential backoff adds latency
- Timeout-based retry still leaves windows for deadlock
- StringSession is simpler, faster, and zero-lock
Pitfalls
- StringSession strings are long (~350 chars) — the JSON file can be large
- Invalid/corrupted .session files will fail conversion silently
- Some accounts may have both .session and StringSession — prefer StringSession
- The
generated/sessions_strings.jsonfile should be backed up before mass operations