Skip to content

add ctx.deferCleanup#360

Merged
jackyzha0 merged 4 commits intomainfrom
jackyzha0/ctx-defer-cleanup
Feb 25, 2026
Merged

add ctx.deferCleanup#360
jackyzha0 merged 4 commits intomainfrom
jackyzha0/ctx-defer-cleanup

Conversation

@jackyzha0
Copy link
Member

Why

ctx.signal.addEventListener('abort', ...) runs after the procedure span so procedures that do things like hold a lock for the duration of the proc drop the lock before cleanup runs

also, abort signal order is FIFO when instead cleanup should generally be LIFO (a la Go defer)

ultimately this reveals a bigger issue which is that we are conflating cancellation with cleanup

What changed

add ctx.deferCleanup

Versioning

  • Breaking protocol change
  • Breaking ts/js API change

@jackyzha0 jackyzha0 requested a review from a team as a code owner February 23, 2026 23:46
@jackyzha0 jackyzha0 requested review from Monkatraz and removed request for a team February 23, 2026 23:46
@masad-frost
Copy link
Member

I would also add to the description that async work in an abort signal callback can have coroutine-like behavior, as in, they can interlace work instead of waiting for the first cleanup task to completely finish.

Copy link
Member

@masad-frost masad-frost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has issues


const deferCleanup = (fn: () => void | Promise<void>) => {
if (cleanupsHaveRun) {
void runCleanupSafe(fn);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these are gonna be outside of traces

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i was debating adding a separate span for this but decided against it

@masad-frost
Copy link
Member

Yayy

@jackyzha0 jackyzha0 merged commit 0a90804 into main Feb 25, 2026
8 checks passed
@jackyzha0 jackyzha0 deleted the jackyzha0/ctx-defer-cleanup branch February 25, 2026 02:00
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.

2 participants