Skip to content

fix: stop log streams from blocking worker event loops (#11)#13

Open
LeastAction-Labs wants to merge 2 commits into
mainfrom
fix/log-streams-thread-blocking
Open

fix: stop log streams from blocking worker event loops (#11)#13
LeastAction-Labs wants to merge 2 commits into
mainfrom
fix/log-streams-thread-blocking

Conversation

@LeastAction-Labs

Copy link
Copy Markdown
Owner

Description

Log endpoints did directory walks, recursive globs, full-file reads, and JSON parsing/sorting directly on the worker event loop. With one event loop per uvicorn worker, a few heavy log tabs could freeze an entire worker, making the service unusable.

  • Offload all blocking/CPU-bound log work to threads via asyncio.to_thread
  • Add a per-process semaphore (LOG_WORK_SEMAPHORE) bounding concurrent heavy log ops so a burst can't exhaust the shared thread pool and starve other endpoints; apply it to query_logs (DuckDB) too
  • Page file tails by seeking backward from EOF instead of reading the whole file into memory

SSE contract unchanged (frontend reads only content/has_more). Tail-paging math validated to match the prior whole-file logic across skip/limit ranges, including multi-block reads and empty files.

Log endpoints did directory walks, recursive globs, full-file reads, and
JSON parsing/sorting directly on the worker event loop. With one event loop
per uvicorn worker, a few heavy log tabs could freeze an entire worker,
making the service unusable.

- Offload all blocking/CPU-bound log work to threads via asyncio.to_thread
- Add a per-process semaphore (LOG_WORK_SEMAPHORE) bounding concurrent heavy
  log ops so a burst can't exhaust the shared thread pool and starve other
  endpoints; apply it to query_logs (DuckDB) too
- Page file tails by seeking backward from EOF instead of reading the whole
  file into memory

SSE contract unchanged (frontend reads only content/has_more). Tail-paging
math validated to match the prior whole-file logic across skip/limit ranges,
including multi-block reads and empty files.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@amoghar29 amoghar29 linked an issue Jun 17, 2026 that may be closed by this pull request
Comment thread backend/src/core/api/routes/logs.py Outdated
None, _run_duckdb_query, request.sql, request.category, request.date
)
async with LOG_WORK_SEMAPHORE:
return await asyncio.to_thread(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add time out

A stuck disk/mount or pathological glob could hold a LOG_WORK_SEMAPHORE slot
forever; three such ops would permanently lock out every log endpoint.

- Add run_log_op(): acquire semaphore + asyncio.to_thread + asyncio.wait_for,
  so the slot is released on timeout and capacity recovers. Raises TimeoutError
  with a clear message (bare asyncio.TimeoutError stringifies to '').
- Route the heavy methods and query_logs through it; query_logs uses a larger
  LOG_QUERY_TIMEOUT and returns a graceful {"error": ...} on timeout.
- iter_file_chunks keeps its manual semaphore (held across the stream) and
  bounds each blocking read with wait_for instead.

Note: wait_for cancels the awaiting coroutine but cannot kill the worker
thread, so timeouts are generous (30s ops, 120s queries) and only fire on
genuine pathology. Verified the slot is released and capacity recovers even
when all slots time out simultaneously.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Log streams should not hold threads

2 participants