Skip to content

Fix duplicate hooks from promoted embedded methods#616

Merged
alecthomas merged 1 commit into
alecthomas:masterfrom
sjh9714:fix-embedded-hook-duplicates
Jul 5, 2026
Merged

Fix duplicate hooks from promoted embedded methods#616
alecthomas merged 1 commit into
alecthomas:masterfrom
sjh9714:fix-embedded-hook-duplicates

Conversation

@sjh9714

@sjh9714 sjh9714 commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #536.

Kong walks embedded structs to discover hook methods. When a hook is promoted through multiple embedded structs, Go reflection exposes compiler-generated wrappers on each embedding level, so the same leaf AfterApply can be invoked more than once. This keeps ordinary method lookup unchanged, but callback collection skips those promoted wrappers because the embedded value itself is visited separately.

Changes

  • Skip promoted embedded method wrappers when collecting hook callbacks.
  • Add a regression test for an exported embedded chain with a value-receiver AfterApply, matching the reported issue shape.

Testing

  • Red check on a temporary master worktree: go test ./... -run TestPromotedEmbeddedAfterApplyCalledOnce -count=1 failed with expected 1, actual 3.
  • go test ./... -run 'TestPromotedEmbeddedAfterApplyCalledOnce|TestEmbeddedCallbacks|TestHooks|TestApplyCalledOnce|TestAfterApplyFiresForEnvOnlyFlag' -count=1
  • go test ./...
  • ./bin/hermit env -r >/tmp/kong-hermit-env && . /tmp/kong-hermit-env && command -v golangci-lint && golangci-lint --version && golangci-lint run
  • . /tmp/kong-hermit-env && golangci-lint run
  • git diff --check

Note: I used Codex while preparing this change, reviewed the final diff, and ran the listed checks locally.

@alecthomas alecthomas left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Nice.

@alecthomas alecthomas merged commit ff16446 into alecthomas:master Jul 5, 2026
5 checks passed
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.

Hooks unexpectedly invoked multiple times when struct embedding is used

2 participants