Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3] app quit on message #3990

Open
wants to merge 1 commit into
base: v3-alpha
Choose a base branch
from
Open

Conversation

mmalcek
Copy link

@mmalcek mmalcek commented Jan 8, 2025

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

Copy link
Contributor

coderabbitai bot commented Jan 8, 2025

Walkthrough

The changes introduce enhanced Windows application termination handling in the wndProc method. The modification focuses on properly responding to system-level window closure events like WM_ENDSESSION, WM_DESTROY, and WM_CLOSE specifically for the main thread window. When these messages are received, the application will trigger its quit mechanism, ensuring a more robust shutdown process that aligns with Windows operating system lifecycle management.

Changes

File Change Summary
v3/pkg/application/application_windows.go Added logic in wndProc to handle application termination messages for the main thread window

Poem

🐰 A Windows whisper, a system's soft call
Shutdown messages dancing through the wall
With grace and precision, our app takes flight
Closing gently when the moment feels right
A rabbit's farewell, clean and neat! 🖥️

Finishing Touches

  • 📝 Generate Docstrings

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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

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

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.

1 participant