Skip to content

Fix cancellation handling in ErrSizedGroup and SizedGroup - #18

Merged
umputun merged 2 commits into
masterfrom
ctx-cancel-queued
Oct 3, 2026
Merged

umputun merged 2 commits into
masterfrom
ctx-cancel-queued

Conversation

@umputun

@umputun umputun commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

callbacks waiting for a semaphore permit in ErrSizedGroup still ran after the group's context was canceled, and Wait returned nil. Cancellation was checked only on entry to Go.

the check now also runs after the permit is acquired, in default and Preemptive modes, with or without TermOnErr. The context error is recorded once. Running callbacks are not interrupted and keep their own errors.

TestErrorSizedGroup_CancelWithActiveErrors relied on queued callbacks running after cancel, so it now waits for all callbacks to start before canceling.

second commit: SizedGroup with Preemptive leaked a permit on cancel. The permit is taken in Go before the goroutine starts, and the goroutine returned on a canceled context without unlocking, so each such call removed one permit from the group. The unlock is now deferred and covers the skip path too. The default-mode ordering in SizedGroup is unchanged.

Related to #17, which fixed the same ordering for TermOnErr.

Cancellation was checked only on entry to Go, so callbacks already waiting
for a semaphore permit still ran after the group's context was canceled and
Wait returned nil. The check now also runs after the permit is acquired, in
both locking modes and with or without TermOnErr. The context error is
recorded once, running callbacks finish and keep their own errors.

TestErrorSizedGroup_CancelWithActiveErrors used to rely on queued callbacks
running after cancel. It now waits for all callbacks to start before canceling.
Copilot AI balanced review requested due to automatic review settings October 3, 2026 17:19
@coveralls

coveralls commented Oct 3, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 37140236136

Coverage increased (+0.06%) to 98.93%

Details

  • Coverage increased (+0.06%) from the base build.
  • Patch coverage: 11 of 11 lines across 2 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 187
Covered Lines: 185
Line Coverage: 98.93%
Coverage Strength: 2082.84 hits per line

💛 - Coveralls

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The implementation correctly handles queued cancellation across supported modes with focused regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Ensures queued ErrSizedGroup callbacks are skipped after context cancellation while preserving errors from active callbacks.

Changes:

  • Rechecks cancellation after semaphore acquisition.
  • Adds coverage across locking and termination modes.
  • Clarifies cancellation behavior in API documentation.
File Description
README.md Documents cancellation semantics.
group_options.go Clarifies the Context option.
errsizedgroup.go Skips queued callbacks after cancellation.
errsizedgroup_test.go Adds regression coverage and updates active-error setup.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

…ive call

With Preemptive the permit is taken in Go before the goroutine starts. When
the goroutine then found the context canceled it returned without unlocking,
so each such call took one permit away from the group for good. The unlock is
now deferred and covers the skip path as well as the normal one.
@umputun umputun changed the title Skip queued callbacks in ErrSizedGroup after context cancellation Fix cancellation handling in ErrSizedGroup and SizedGroup Oct 3, 2026
@umputun
umputun merged commit 6069974 into master Oct 3, 2026
7 checks passed
@umputun
umputun deleted the ctx-cancel-queued branch October 3, 2026 17:27
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