Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Cancellation watcher polls the database every 100 ms per running task #22

Open

Description

@tercel

Summary

For every running task, TaskManager._watch_cancellation() opens a new database session and polls the cancellation flag every 100 ms for the entire executor run. A single one-hour task therefore issues roughly 36,000 cancellation queries, each with a session acquire/release.

Environment

  • apflow 0.24.3 (main @ 479d578)

Evidence

  • src/apflow/core/execution/task_manager.py:1765-1798 — while not event.is_set(): await asyncio.sleep(0.1) then a new Session/AsyncSession and get_task_by_id

Impact

  • SQLite write contention and disk I/O on a laptop-class machine for the whole render.
  • With multiple ready tasks, polling multiplies (N tasks × 10 queries/s).
  • Contributes to the session-expiry problem in a busy pool.

Proposed fix

  • Make the poll interval configurable and use adaptive backoff (e.g. 100 ms → 1 s → 5 s) for long runs.
  • Prefer a notification channel where available (in-process asyncio.Event, DB notification/LISTEN, or an explicit cancel_task() call that sets the event directly).
  • Reuse a dedicated lightweight session for cancellation checks instead of opening one per tick.

Acceptance criteria

  • An idle one-hour task issues O(1) cancellation queries per minute, not ~36,000.
  • Cancellation still takes effect within a documented bound (e.g. ≤ 2 s).
  • Tests cover the interval/backoff behavior.

Workaround

Pass a shared cancellation event and call cancel_task() in-process; the poller still runs, but nothing can be done from the caller side today.

Activity

  1. added
    bug
    on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions