fix: stop log streams from blocking worker event loops (#11)#13
Open
LeastAction-Labs wants to merge 2 commits into
Open
fix: stop log streams from blocking worker event loops (#11)#13LeastAction-Labs wants to merge 2 commits into
LeastAction-Labs wants to merge 2 commits into
Conversation
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
reviewed
Jun 17, 2026
| None, _run_duckdb_query, request.sql, request.category, request.date | ||
| ) | ||
| async with LOG_WORK_SEMAPHORE: | ||
| return await asyncio.to_thread( |
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
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.