Files
Mak5erandClaude Fable 5 caffc343af refactor: apply full-project audit — bug fixes, performance, deduplication
Full-codebase audit (149 raw findings, 57 verified and applied).

Fixes:
- download resume: recompute Range header per retry attempt; never resume
  preallocated multipart .part files (silent media corruption)
- inline sends: canonical error tail (rate-limit/queue-busy/timeout/failure)
  for all platforms; YouTube inline no longer strands tokens in
  'processing' for 6h
- admin mailing: exclude banned users; fix Telegram error-text matching
- middlewares: instantiate once and share across message/callback/inline
  so antiflood/ban state no longer fragments
- event loop: move ZIP compression, JSON state persistence, admin FS
  walks, PIL/ffmpeg/ffprobe work off the loop
- download queue: prune idle per-user scopes; cancelled leader releases
  joined duplicate requests
- worker subprocess: disable log sinks (stderr error protocol and log
  rotation no longer race the parent)
- third-party loggers route through app handlers instead of lastResort
- tiktok: fix mojibake reaction emoji; per-user settings in doc callback;
  per-URL locks for short-URL expansion; parallel photo-set downloads
- ci: fix broken yt-dlp auto-bump workflow baseline
- db: drop redundant analytics_events indexes (migration 000010)

Refactoring:
- services/inline/send_flow.py: shared inline delivery flow for all five
  platforms (~640 duplicated lines removed)
- services/media/audio_flow.py: shared MP3 pipeline (was duplicated 6x
  across youtube/youtube_inline/soundcloud/spotify)
- CobaltMediaService base + shared Cobalt response parsing
- services/platforms/ytdlp_helpers.py: shared yt-dlp scaffolding
- register_inline_send_handlers: one chosen_inline_result/callback
  registration factory for 8 handlers
- merge pinterest/threads single-media and media-group flows
- logging decorator factory; remove ~11 dead repository methods

Tests: 470 -> 518 passing, coverage 77.3% -> 80.9% (CI gate 74%).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-26 16:25:59 +03:00

88 lines
3.0 KiB
Python

import asyncio
from typing import Optional
import aiohttp
from services.logger import logger as logging
_session: Optional[aiohttp.ClientSession] = None
_session_loop: Optional[asyncio.AbstractEventLoop] = None
_lock = asyncio.Lock()
_lock_loop: Optional[asyncio.AbstractEventLoop] = None
# Slightly higher limits to speed up bursty handler traffic while keeping sockets bounded
_CLIENT_TIMEOUT = aiohttp.ClientTimeout(total=20, connect=5, sock_read=20)
_CONNECTOR_LIMIT = 128
def _get_loop_bound_lock() -> asyncio.Lock:
"""Return the module lock, recreating it when the running loop changes.
An asyncio.Lock binds to the loop that first awaits it, so reusing the old
instance after a loop change would raise RuntimeError and permanently break
the session-recreation path this module implements.
"""
global _lock, _lock_loop
loop = asyncio.get_running_loop()
if _lock_loop is not loop:
_lock = asyncio.Lock()
_lock_loop = loop
return _lock
async def _close_session_from_previous_loop(session: aiohttp.ClientSession) -> None:
"""Best-effort cleanup for a session created on a different event loop.
Awaiting ``session.close()`` would touch the previous loop's connector, so
detach the session and close the connector directly instead.
"""
try:
connector = session.connector
session.detach()
if connector is not None:
await connector.close()
except Exception as exc:
logging.debug("Discarding HTTP session from a previous event loop: error=%s", exc)
async def get_http_session() -> aiohttp.ClientSession:
"""Return a lazily initialised shared aiohttp session."""
global _session, _session_loop
loop = asyncio.get_running_loop()
if _session and not _session.closed and _session_loop is loop:
return _session
async with _get_loop_bound_lock():
if _session and not _session.closed and _session_loop is loop:
return _session
previous_session = _session
connector = aiohttp.TCPConnector(
limit=_CONNECTOR_LIMIT,
limit_per_host=32,
ttl_dns_cache=300,
enable_cleanup_closed=True,
)
_session = aiohttp.ClientSession(timeout=_CLIENT_TIMEOUT, connector=connector)
_session_loop = loop
if previous_session and not previous_session.closed:
# An open previous session here means the loop changed, so awaiting
# its close() would run on the wrong loop.
await _close_session_from_previous_loop(previous_session)
return _session
async def close_http_session() -> None:
"""Close the shared session if it was created."""
global _session, _session_loop
async with _get_loop_bound_lock():
session = _session
session_loop = _session_loop
_session = None
_session_loop = None
if session and not session.closed:
if session_loop is asyncio.get_running_loop():
await session.close()
else:
await _close_session_from_previous_loop(session)