Skip to content

perf: PerformanceMonitor runs blocking sqlite3 I/O on the event loop #1195

Description

@groupthinking

Problem

PerformanceMonitor (src/youtube_extension/backend/services/performance_monitor.py) uses the fully synchronous sqlite3 driver directly inside six async def methods:

Line Method
274 _store_metric
357 _store_alert
504 _basic_cleanup
566 get_current_performance_summary
662 _get_recent_metrics_summary
802 _store_benchmark_result

Each performs sqlite3.connect(...) → statement → commit()close(). commit() fsyncs. All of it runs on the event loop, so no other coroutine can be scheduled while the write is in flight.

Reachability evidence

Not speculative — this is on a live request path:

  • youtube_extension.backend.services.performance_monitor is in the transitive import closure of the production entrypoint youtube_extension.main:app (root Dockerfile:93).
  • src/youtube_extension/backend/api/v1/router.py:69from ...services.performance_monitor import PerformanceMonitor
  • router.py:1165 and router.py:1183await performance_monitor.record_metric(...)
  • record_metric (L231) calls await self._store_metric(metric) (L261) unconditionally — there is no sampling or enable flag.

Additional live callers: memory_manager.py:472/475, load_balancer.py:381, database_optimizer.py:49.

Proposed fix

Move each method's database work into a nested synchronous function dispatched via await asyncio.to_thread(...). SQL text, transaction boundaries, return values and error handling stay identical.

Leave _init_database (L127) alone — it is synchronous and called from __init__, so it never runs on the loop.

Acceptance criteria

  • All six async def sqlite call sites dispatched off-loop
  • All 113 existing tests/unit/test_performance_monitor.py tests pass with zero test edits
  • New tests prove the loop stays responsive during database work
  • Non-vacuity proven by behavioural mutation, with targeted (not uniform) failures
  • ruff parity with main

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions