Skip to content

[v3] app quit on message#3990

Merged
leaanthony merged 3 commits into
wailsapp:v3-alphafrom
mmalcek:v3-alpha
Jan 11, 2025
Merged

[v3] app quit on message#3990
leaanthony merged 3 commits into
wailsapp:v3-alphafrom
mmalcek:v3-alpha

Conversation

@mmalcek

@mmalcek mmalcek commented Jan 8, 2025

Copy link
Copy Markdown
Contributor

In some cases it's necessary close the application by message.
w32.WM_CLOSE is implemented on Window level but not on Application level.
If systray exists and application.WindowsOptions{DisableQuitOnLastWindowClosed: true} systray can exists without any window. But there is no handle for w32.WM_CLOSE.
So if other app (in my case Inno setup on upgrade) wants to close the application by message, it's not possible.

Discord discussion about this issue
https://discord.com/channels/1042734330029547630/1147312415948668971/threads/1323034810188566539

Summary by CodeRabbit

  • Bug Fixes
    • Improved application shutdown handling on Windows.
    • Enhanced system-level event management for graceful application termination.
    • Fixed application closure handling on WM_CLOSE message when no windows are open or only the system tray is active.

@coderabbitai

coderabbitai Bot commented Jan 8, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The changes introduce enhanced Windows application termination handling in the wndProc method of the application_windows.go file. The modification focuses on responding to system-level window closure events such as WM_ENDSESSION, WM_DESTROY, and WM_CLOSE for the main thread window. When these messages are received, the application triggers its quit mechanism, ensuring a robust shutdown process aligned with Windows operating system lifecycle management. Additionally, documentation has been updated to reflect improvements in handling the WM_CLOSE message when no windows are open.

Changes

File Change Summary
v3/pkg/application/application_windows.go Added logic in wndProc to handle application termination messages for the main thread window.
docs/src/content/docs/changelog.mdx Added fix for handling application closure on WM_CLOSE message when no windows are open or only the system tray is active.

Poem

🐰 In the Windows realm, a message we greet,
When it's time to close, we won't miss a beat.
With a hop and a skip, our app says goodbye,
Responding to signals, like clouds in the sky.
A gentle farewell, as we end the day! 🖥️


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7326abd and fa6c48e.

📒 Files selected for processing (1)
  • docs/src/content/docs/changelog.mdx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • docs/src/content/docs/changelog.mdx

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
v3/pkg/application/application_windows.go (1)

208-214: Consider documenting the Windows message handling behavior.

The implementation successfully adds application-level quit message handling, which is a significant architectural change in how the application responds to system events. Consider adding documentation that explains:

  1. The difference between window-level and application-level message handling
  2. The behavior when WindowsOptions{DisableQuitOnLastWindowClosed: true} is set
  3. The interaction with system tray applications

This will help maintainers understand the message handling hierarchy and system integration points.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b6b8c18 and 7c591a9.

📒 Files selected for processing (1)
  • v3/pkg/application/application_windows.go (1 hunks)
🔇 Additional comments (1)
v3/pkg/application/application_windows.go (1)

208-214: Verify message handling order and potential race conditions.

The implementation correctly adds application-level quit message handling, but there are a few considerations:

  1. The order of message handling might be important. Consider whether WM_ENDSESSION should be handled differently as it's a system shutdown event that requires immediate response.
  2. There could be potential race conditions if globalApplication.Quit() is called concurrently from multiple message handlers.

Let's verify the message handling implementation in other parts of the codebase:

Consider adding these improvements:

  1. Handle WM_ENDSESSION separately as it requires immediate response during system shutdown:
 if hwnd == m.mainThreadWindowHWND {
-    if msg == w32.WM_ENDSESSION || msg == w32.WM_DESTROY || msg == w32.WM_CLOSE {
+    if msg == w32.WM_ENDSESSION {
+        // Respond to system shutdown immediately
+        globalApplication.Quit()
+        return 0
+    }
+    if msg == w32.WM_DESTROY || msg == w32.WM_CLOSE {
         globalApplication.Quit()
     }
  1. Add synchronization to prevent race conditions:
 if hwnd == m.mainThreadWindowHWND {
     if msg == w32.WM_ENDSESSION || msg == w32.WM_DESTROY || msg == w32.WM_CLOSE {
+        // Use sync.Once to prevent multiple quit calls
+        globalApplication.QuitOnce.Do(func() {
             globalApplication.Quit()
+        })
     }
✅ Verification successful

The implementation is correct and thread-safe.

The codebase already implements proper synchronization and cleanup mechanisms:

  • Quit() is designed to handle concurrent calls safely through sync primitives
  • Shutdown tasks are executed synchronously via InvokeSync
  • The message handling order is appropriate for all termination scenarios
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for other WM_ENDSESSION handlers that might conflict
rg "WM_ENDSESSION|WM_DESTROY|WM_CLOSE" --type go

# Check for other uses of globalApplication.Quit()
rg "globalApplication.Quit" --type go

Length of output: 3075


Script:

#!/bin/bash
# Check the implementation of globalApplication and Quit()
ast-grep --pattern 'func ($app $_) Quit() { $$$ }'

# Look for any sync primitives or thread safety mechanisms
rg "sync\." --type go v3/pkg/application/application.go

Length of output: 427


Script:

#!/bin/bash
# Find Quit implementation with a more specific pattern
ast-grep --pattern 'func ($app *Application) Quit() { $$$ }'

# Look for shutdown-related code
rg "func.*Quit|shutdown|cleanup" --type go v3/pkg/application/application.go

Length of output: 467

@leaanthony

Copy link
Copy Markdown
Member

Thanks 🙏 Please could you add an entry to the changelog located at docs/src/content/docs/changelog.mdx? Thanks!

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
docs/src/content/docs/changelog.mdx (1)

55-55: LGTM with a minor formatting suggestion.

The changelog entry is well-structured and provides clear context about the fix. For consistency with other entries in the changelog, consider adding a period at the end of the line.

-Fix Close application on WM_CLOSE message when no windows are open/systray only in [#3990](https://github.com/wailsapp/wails/pull/3990) 
+Fix Close application on WM_CLOSE message when no windows are open/systray only in [#3990](https://github.com/wailsapp/wails/pull/3990).
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7c591a9 and 7326abd.

📒 Files selected for processing (1)
  • docs/src/content/docs/changelog.mdx (1 hunks)

@mmalcek

mmalcek commented Jan 10, 2025

Copy link
Copy Markdown
Contributor Author

Thanks 🙏 Please could you add an entry to the changelog located at docs/src/content/docs/changelog.mdx? Thanks!

done

@leaanthony leaanthony added this to the v3.0.0-alpha.9 milestone Jan 10, 2025
@leaanthony leaanthony merged commit 20e050b into wailsapp:v3-alpha Jan 11, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Jan 18, 2025
16 tasks
@leaanthony leaanthony removed this from v3 Backlog Feb 1, 2026
Grantmartin2002 pushed a commit to Grantmartin2002/wails that referenced this pull request Apr 29, 2026
* app quit on message

* update changelog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants