Skip to content

feature(fiber): add Cancel.make_child for linked cancellation#15364

Open
rgrinberg wants to merge 1 commit into
mainfrom
fiber-cancel-make-child
Open

feature(fiber): add Cancel.make_child for linked cancellation#15364
rgrinberg wants to merge 1 commit into
mainfrom
fiber-cancel-make-child

Conversation

@rgrinberg

Copy link
Copy Markdown
Member

Summary

Adds Fiber.Cancel.make_child, a way to link cancellations together:

val make_child : t -> t

make_child parent returns a new cancellation that is fired whenever parent is
fired (but not the other way around). This makes it possible to build trees of
cancellations where cancelling a parent propagates to all of its descendants.

Details

  • A Not_cancelled cancellation now tracks its children and its parent.
  • fire and fire' walk the child tree, so firing a node fires its whole
    subtree. The order in which a parent's and its children's handlers run is
    unspecified.
  • When a node fires it detaches itself from its parent, so short-lived children
    attached to a long-lived parent do not accumulate. (A child is otherwise
    retained until it or its parent is fired, or the parent is collected.)
  • with_handler's behaviour is unchanged.

Tests

src/fiber/test/cancel_tests.ml gains three expect tests covering:

  • cancelling a parent triggers a child's cancellation handler,
  • a child created from an already-fired parent is born cancelled,
  • cancelling a child does not cancel its parent.

[Cancel.make_child parent] returns a new cancellation that is fired whenever [parent] is fired (but not the other way around). A parent tracks its children and [fire]/[fire'] propagate cancellation down the tree; a child detaches itself from its parent when it fires so that short-lived children attached to a long-lived parent do not leak.

Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <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.

1 participant