Skip to content

Propagate cancellation causes through task trees#461

Merged
ioquatix merged 4 commits into
socketry:mainfrom
infiton:fix/cancel-cause-propagation
Jul 17, 2026
Merged

Propagate cancellation causes through task trees#461
ioquatix merged 4 commits into
socketry:mainfrom
infiton:fix/cancel-cause-propagation

Conversation

@infiton

@infiton infiton commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Propagate cancellation causes through Node, Task, and Scheduler cancellation traversal so bulk cancellation reuses the originating Async::Cancel::Cause instead of regenerating a fresh cause/backtrace for each descendant task.

This reduces redundant Cancel::Cause.for / backtrace capture work during scheduler-driven bulk cancellation and shutdown. Descendant tasks now observe the originating cancellation cause rather than a per-node synthetic cause generated during traversal.

A benchmark was added in benchmark/cancel_cause.rb to demonstrate the improvement against main:

# TASKS=1365
main:   cause_for_calls=1365 allocated_objects=26009 duration_ms=7.42
branch: cause_for_calls=1    allocated_objects=5545  duration_ms=5.79

# TASKS=5461
main:   cause_for_calls=5461 allocated_objects=103833 duration_ms=35.03
branch: cause_for_calls=1    allocated_objects=21929  duration_ms=23.94

Types of Changes

  • Performance improvement.

Contribution

Comment thread lib/async/node.rb Outdated
@infiton
infiton force-pushed the fix/cancel-cause-propagation branch from aa1134c to ca5184e Compare July 10, 2026 13:10
@infiton
infiton requested a review from tavianator July 10, 2026 13:10
Assisted-By: devx/62617253-44ec-4b16-92f0-5121779fd985
@infiton
infiton force-pushed the fix/cancel-cause-propagation branch from ca5184e to 7dd0607 Compare July 13, 2026 14:35
@ioquatix
ioquatix merged commit f7d3446 into socketry:main Jul 17, 2026
37 of 39 checks passed
@ioquatix

ioquatix commented Jul 17, 2026

Copy link
Copy Markdown
Member

Thanks for your contribution. The only change I made was the state transition methods (ending with bangs) take positional arguments to be consistent with other state transition methods.

FYI, from my point of view, it's good to do less allocations during cancellation so this is a good idea. In general, error handling (which usually involves cancellation) should avoid doing unnecessary work. Otherwise you can have cascading failures (e.g. run out of memory).

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.

3 participants