Skip to content

fix(v3/events): read event hooks under hookLock in Emit - #6276

Merged
leaanthony merged 2 commits into
wailsapp:masterfrom
DevLumuz:fix/v3-event-hooks-race
Oct 27, 2026
Merged

leaanthony merged 2 commits into
wailsapp:masterfrom
DevLumuz:fix/v3-event-hooks-race

Conversation

@DevLumuz

@DevLumuz DevLumuz commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Description

EventProcessor.Emit reads e.hooks without taking hookLock, while RegisterHook appends to the hook slice under the lock and the function it returns rewrites that slice in place with slices.DeleteFunc (which also zeroes the tail since Go 1.22). An Emit running concurrently with registering or removing a hook therefore races with both, and can observe a cleared entry.

Emit now copies the event's hooks under hookLock.RLock() and runs the copy, the same snapshot approach EventManager already uses for application event listeners. Hooks still run synchronously and in registration order, and a hook can still register or remove hooks without deadlocking because the lock is released before any hook runs.

Found while working on #6275 (Go listener ordering, follow-up to #5757); kept separate because it is an independent bug.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • WEP (proposal only; no implementation)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

TestEventProcessorRegisterHookWhileEmitting emits in a loop while registering and removing hooks on another goroutine.

  • Before the fix, go test -race ./pkg/application/ -run TestEventProcessorRegisterHookWhileEmitting reports DATA RACE between events.go:157 (Emit) and events.go:219/228 (RegisterHook and its remover).

  • After the fix: clean with -race -count=20, and the whole ./pkg/application/ suite passes with -race.

  • Windows

  • macOS

  • Linux

Linux: Fedora 43, Go 1.26.7 (the change is platform-independent Go).

Test Configuration

Platform-independent change; tested with go test -race on Linux (Fedora 43, x86_64, Go 1.26.7).

Checklist:

  • My code follows the general coding style of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a race that could occur when events were emitted while hooks were registered or removed. Hook callbacks continue to run in order and can stop event emission.

@github-actions github-actions Bot added Bug Something isn't working v3 Documentation Improvements or additions to documentation labels Oct 16, 2026
@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration
Commits

Reviewing files that changed from the base of the PR and between 5a50640 and fad7239.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review.

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: wailsapp/wails/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8e5111c4-c460-4bb4-ab4a-9696d6079e80
📥 Commits

Reviewing files that changed from the base of the PR and between 7e126e2 and ad9714c.

📒 Files selected for processing: 1
  • v3/UNRELEASED_CHANGELOG.md
🚧 Files skipped from review as they are similar to previous changes: 1
  • v3/UNRELEASED_CHANGELOG.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

EventProcessor.Emit now snapshots hooks under a read lock and invokes them after releasing the lock. A new test exercises concurrent event emission and hook registration or removal. The changelog describes the data race.

Changes

Event hook synchronization

Layer / File(s) Summary
Snapshot hooks during emission
v3/pkg/application/events.go, v3/pkg/application/events_test.go, v3/UNRELEASED_CHANGELOG.md
Emit copies the event’s hooks under a read lock, then invokes the copy. The test emits events while registering and removing a hook. The changelog describes the data race.

Priority: Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: leaanthony

Merge Risk: Minimal · previous review ad971

This change fixes a data race in event hook emission. No actionable merge-blocking risk was found in the supplied changes.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. (1 skipped: 1 … Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary fix: protecting event hook reads in Emit with hookLock.
Description check Passed The description explains the race condition, implementation, motivation, testing, platform, and checklist status. It identifies related issue #6275 and provides sufficient test configuration, although…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description explains the race condition, implementation, motivation, testing, platform, and checklist status. It identifies related issue #6275 and provides sufficient test configuration, although it does not include wails doctor output.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit watched the hooks go by,
While emitters hopped beneath the sky.
A locked snapshot kept their place,
Then callbacks ran at their own pace.
The changelog noted what had been,
And rabbits cheered the threads serene.
With ears held high, they closed the scene.

Comment @coderabbitai help to get the list of available commands.

@taliesin-ai

Copy link
Copy Markdown
Collaborator

Automated GA-readiness test result for head 7e126e27900fbe331102ff6ce593db31efa8d757 (taliesin-ai)

Platform CLI build Unit tests (./pkg/... ./internal/...) Example build (examples/window)
macOS (mac-node1) ✅ ❌ 1 pkg: pkg/updater TestWorkflowReproDownloadFailureLeavesTempArtifacts ✅
Windows (win-node1) ✅ ❌ 2 pkgs: same updater test + internal/webview2/pkg/edge TestLive* x4 (known, needs interactive desktop) ✅
Linux not run (node unreachable)

Neither failure comes from this PR. The branch is based on 91a74c4, and the updater test failure there was fixed on master by #6278 (master ba77efe passes it on both nodes). The pkg/application event tests pass on both platforms. A rebase onto master should clear the updater failure. App launch wasn't exercised (no GUI session over SSH).

@DevLumuz
DevLumuz force-pushed the fix/v3-event-hooks-race branch from 7e126e2 to ad9714c Compare October 17, 2026 00:58
@taliesin-ai

Copy link
Copy Markdown
Collaborator

Automated GA-readiness test result for head ad9714c1fca40c04a7615277b6d58f03473d6c93 (taliesin-ai)

Platform CLI build Unit tests (./pkg/... ./internal/...) Example build (examples/window)
macOS (mac-node1) ✅ ✅ 51/51 pkgs ✅
Windows (win-node1) ✅ ❌ 1 pkg: internal/webview2/pkg/edge TestLive* x4 (known, needs interactive desktop) ✅
Linux not run (node unreachable)

This matches the master baseline (d8f3336) on both nodes: master shows the same Windows-only TestLive* failures, which come from the SSH session-0 environment, not from this PR. The earlier updater test failure is gone now that the branch includes #6278. App launch wasn't exercised (no GUI session over SSH).

Emit read e.hooks without taking hookLock, while RegisterHook appends to
the slice and its remover rewrites it in place with slices.DeleteFunc
(which also zeroes the tail). A concurrent Emit could race with both and
observe a cleared entry.

Emit now copies the event's hooks under a read lock and runs the copy.
@DevLumuz
DevLumuz force-pushed the fix/v3-event-hooks-race branch from ad9714c to 5a50640 Compare October 27, 2026 01:26
@leaanthony
leaanthony merged commit d746fdf into wailsapp:master Oct 27, 2026
17 of 19 checks passed
leaanthony pushed a commit that referenced this pull request Oct 27, 2026
taliesin-ai added a commit to DevLumuz/wails that referenced this pull request Oct 27, 2026
Resolves the conflict in v3/pkg/application/events_test.go with wailsapp#6276,
which added TestEventProcessorRegisterHookWhileEmitting where this branch
added its ordering tests: both are kept. events.go merged cleanly (wailsapp#6276's
hook copy under hookLock in Emit; this branch's per-listener delivery in
dispatchEventToListeners).

Checked locally on go1.26.10 (the toolchain v3/go.mod pins): go vet and
go test of ./pkg/application/... and ./internal/mailbox/..., and the event
and mailbox tests three times under -race.
@DevLumuz
DevLumuz deleted the fix/v3-event-hooks-race branch October 27, 2026 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working Documentation Improvements or additions to documentation v3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants