Skip to content

fix(v3): guard dispatchWailsEvent against race condition on reload#4925

Merged
leaanthony merged 3 commits into
wailsapp:v3-alphafrom
ddmoney420:fix/4872-dispatch-event-race
Feb 2, 2026
Merged

fix(v3): guard dispatchWailsEvent against race condition on reload#4925
leaanthony merged 3 commits into
wailsapp:v3-alphafrom
ddmoney420:fix/4872-dispatch-event-race

Conversation

@ddmoney420

@ddmoney420 ddmoney420 commented Jan 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Guards window._wails.dispatchWailsEvent calls against race conditions during page reload.

Problem

When the page is reloaded, the WindowLoadFinished event can fire before the JavaScript runtime has mounted dispatchWailsEvent on window._wails. This causes:

TypeError: window._wails.dispatchWailsEvent is not a function

This is a classic race condition between:

  • Path A: Native WebKit firing WindowLoadFinished event immediately
  • Path B: JavaScript runtime modules loading and initializing

Solution

Added a guard to check if window._wails and dispatchWailsEvent exist before calling:

msg := fmt.Sprintf("if(window._wails&&window._wails.dispatchWailsEvent){window._wails.dispatchWailsEvent(%s);}", event.ToJSON())

If the runtime isn't ready, the event is silently skipped (correct behavior since there's no handler).

Test plan

  • Run wails3 dev on a fresh project
  • Right-click and click "Reload" on the frontend window
  • Verify no error appears in console

Fixes #4872

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Prevented errors occurring during page reloads when the runtime isn't fully initialized, improving stability when events fire before the runtime is ready.
    • Reduced race-condition impact on event dispatch to make reloads more reliable and less likely to trigger transient failures.

✏️ Tip: You can customize this high-level summary in your review settings.

When the page is reloaded, the WindowLoadFinished event can fire before
the JavaScript runtime has mounted dispatchWailsEvent on window._wails.
This causes a TypeError: window._wails.dispatchWailsEvent is not a function.

This fix adds a guard to check if window._wails and dispatchWailsEvent
exist before attempting to call the function. If the runtime isn't ready,
the event is silently skipped (which is correct since there's no handler).

Fixes wailsapp#4872

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jan 29, 2026

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

Walkthrough

Guards the runtime call to window._wails.dispatchWailsEvent by checking that window._wails and window._wails.dispatchWailsEvent exist before invoking, preventing errors when events fire before the Wails runtime is mounted (e.g., during page reload).

Changes

Cohort / File(s) Summary
Race Condition Guard
v3/pkg/application/webview_window.go
Wraps the JavaScript invocation of window._wails.dispatchWailsEvent with a runtime existence check and adds comments explaining the reload-time race condition. (+4/-1 lines)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

Bug, Documentation, v3-alpha, runtime, size:XS

Poem

🐇 I hopped to the window, ears full of cheer,
Events came too early and caused a small fear.
Now I peek before calling, I wait and I see—
If _wails is ready, then let the events be! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding a runtime guard to dispatchWailsEvent to fix a race condition on reload.
Description check ✅ Passed The description includes a clear summary, problem statement, solution, and test plan. However, the description checklist items are not checked off.
Linked Issues check ✅ Passed The PR directly addresses issue #4872 by implementing the runtime guard to prevent TypeError when WindowLoadFinished fires before JavaScript runtime initialization.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the race condition in issue #4872; no unrelated or out-of-scope modifications are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

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

@leaanthony

Copy link
Copy Markdown
Member

Thanks 🙏 Please could you add an entry to the changelog located at v3/UNRELEASED_CHANGELOG.md? Thanks!

leaanthony added a commit that referenced this pull request Feb 2, 2026
Fix race condition causing TypeError during page reload.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@leaanthony leaanthony merged commit 5d0dd30 into wailsapp:v3-alpha Feb 2, 2026
8 of 11 checks passed
@sonarqubecloud

sonarqubecloud Bot commented Feb 2, 2026

Copy link
Copy Markdown

Grantmartin2002 pushed a commit to Grantmartin2002/wails that referenced this pull request Apr 29, 2026
Fix race condition causing TypeError during page reload.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Grantmartin2002 pushed a commit to Grantmartin2002/wails that referenced this pull request Apr 29, 2026
…ailsapp#4925)

* fix(v3): guard dispatchWailsEvent against race condition on reload

When the page is reloaded, the WindowLoadFinished event can fire before
the JavaScript runtime has mounted dispatchWailsEvent on window._wails.
This causes a TypeError: window._wails.dispatchWailsEvent is not a function.

This fix adds a guard to check if window._wails and dispatchWailsEvent
exist before attempting to call the function. If the runtime isn't ready,
the event is silently skipped (which is correct since there's no handler).

Fixes wailsapp#4872

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* docs: Add changelog entry for wailsapp#4872

---------

Co-authored-by: ddmoney420 <ddmoney420@users.noreply.github.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
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