Skip to content

fix: invoke throttle callbacks outside the state lock - #1063

Open
dailyun wants to merge 1 commit into
samber:masterfrom
dailyun:codex/throttle-callback-reset
Open

dailyun wants to merge 1 commit into
samber:masterfrom
dailyun:codex/throttle-callback-reset

Conversation

@dailyun

@dailyun dailyun commented Nov 2, 2026

Copy link
Copy Markdown

Fixes #1057.

Throttle callbacks currently run while holding the same mutex that reset acquires, so a callback that resets its throttle deadlocks. The same lock also blocks an unrelated key behind a slow callback.

Reserve the per-key count and start the window timer under the state lock, then release the lock before invoking callbacks. Calls remain synchronous, and callbacks within one call keep their order. As documented in the updated comment, the window now starts before the first callback and accepted concurrent calls can overlap; callbacks are no longer serialized by the internal state mutex.

Regression coverage checks callback reset with count limits of 1 and 3, same-key callback reentry, another key progressing while a callback is blocked, and 100 concurrent calls respecting the count limit. The final tests fail on unchanged main for reset/reentry/cross-key deadlocks and pass with the change.

Verified on macOS arm64 / Go 1.27.3:

  • Throttle tests with the race detector, repeated 5 times.
  • go test -race ./... (all root-module packages, including iterator/mutable/parallel packages).
  • go vet ./....
  • Changed-file formatting and golangci-lint run --new-from-rev 6f5a63a: 0 new issues.
  • git diff --check.

Full golangci-lint 2.15.0 reports 64 existing unused //nolint:revive directives in benchmark files; an unchanged base worktree reports the same 64 findings. No lint configuration was changed. The separate experimental SIMD module and other operating systems were not tested.

Prepared with OpenAI Codex assistance; reproduction, implementation, and local checks were performed in this session.

This branch has not been deployed

No deployments
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.

NewThrottle deadlocks when a callback calls its reset function

1 participant