Skip to content

Fix deadline cleanup and handling of multiple expired deadlines - #114

Merged
maffoo merged 1 commit into
mainfrom
dev-maffoo/deadlines
Nov 20, 2026
Merged

maffoo merged 1 commit into
mainfrom
dev-maffoo/deadlines

Conversation

@maffoo

@maffoo maffoo commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Previously, child tasks spawned inside a deadline scope pushed an inherited DeadlineEntry in Task.init that was never invalidated in Task.close(), leaving valid entries for finished tasks in the scheduler's deadline queue. Additionally, when a deadline expired, Scheduler.tick() only popped entries up to that single oldest timestamp and then blocked unconditionally in get_all(None), even if interrupting those entries produced no ready tasks (e.g. for completed or uninterruptible tasks), which stalled later deadlines until external wakeups drained the queue.

This addresses those issues along with related deadline edge cases:

  • Pop and invalidate all remaining deadlines in Task.close(), and move awaitable.await() before push_deadline in Task.init.
  • Clear the task reference in DeadlineEntry.invalidate() (deriving valid from _task is not None) so invalidated entries do not pin finished tasks, and lazily compact the scheduler heap when invalid entries exceed half its size.
  • Loop in Scheduler._get_ready_tasks() to handle all deadlines <= now via handle_expired_deadlines(), continue waiting for the next deadline if no tasks become ready, avoid starving expired deadlines behind already- ready tasks, and handle interrupt signals without prematurely firing future deadlines.

Previously, child tasks spawned inside a deadline scope pushed an
inherited DeadlineEntry in Task.__init__ that was never invalidated in
Task.close(), leaving valid entries for finished tasks in the scheduler's
deadline queue. Additionally, when a deadline expired, Scheduler.tick()
only popped entries up to that single oldest timestamp and then blocked
unconditionally in get_all(None), even if interrupting those entries
produced no ready tasks (e.g. for completed or uninterruptible tasks),
which stalled later deadlines until external wakeups drained the queue.

This addresses those issues along with related deadline edge cases:

- Pop and invalidate all remaining deadlines in Task.close(), and move
  awaitable.__await__() before push_deadline in Task.__init__.
- Clear the task reference in DeadlineEntry.invalidate() (deriving valid
  from _task is not None) so invalidated entries do not pin finished
  tasks, and lazily compact the scheduler heap when invalid entries
  exceed half its size.
- Loop in Scheduler._get_ready_tasks() to handle all deadlines <= now via
  handle_expired_deadlines(), continue waiting for the next deadline if
  no tasks become ready, avoid starving expired deadlines behind already-
  ready tasks, and handle interrupt signals without prematurely firing
  future deadlines.
@maffoo
maffoo merged commit 1e1f890 into main Nov 20, 2026
30 checks passed
@maffoo
maffoo deleted the dev-maffoo/deadlines branch November 20, 2026 05:05
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.

1 participant