wails icon indicating copy to clipboard operation
wails copied to clipboard

Terminating a Wails App with Ctrl+C Causes Premature Application Termination

Open caesaneer opened this issue 2 years ago • 1 comments

Description

When terminating a Wails application using Ctrl+C, the application shuts down prematurely, not allowing the shutdown handler to complete its execution. This behavior is inconsistent with how the application handles window closure, where the shutdown process completes as expected.

To Reproduce

  1. Implement a shutdown handler in a Wails application that utilizes a sync.WaitGroup.
    
  2. The handler should wait for goroutines to finish (e.g., WaitGroup.Wait()).
    
  3. Close the application window and observe that the shutdown handler waits as expected.
    
  4. Restart the application and terminate it using Ctrl+C.
    
  5. Notice that the application terminates prematurely, without waiting for the shutdown handler to complete its execution.
    

Expected behaviour

When terminating the application using Ctrl+C, the shutdown handler should be allowed to complete its execution, similar to the behavior observed when closing the application window. The application should wait for all goroutines to finish (as managed by the sync.WaitGroup) before shutting down.

Actual Behavior Upon pressing Ctrl+C, the application shuts down immediately, not respecting the wait conditions defined in the shutdown handler. This results in incomplete execution of shutdown procedures.

Screenshots

No response

Attempted Fixes

No response

System Details

Version         | v2.6.0
Package Manager | apt   

# System
┌─────────────────────────┐
| OS           | Ubuntu   |
| Version      | 22.04    |
| ID           | ubuntu   |
| Go Version   | go1.21.4 |
| Platform     | linux    |
| Architecture | amd64    |
└─────────────────────────┘

# Dependencies
┌──────────────────────────────────────────────────────────────────────────┐
| Dependency | Package Name          | Status    | Version                 |
| *docker    | docker.io             | Available | 24.0.5-0ubuntu1~22.04.1 |
| gcc        | build-essential       | Installed | 12.9ubuntu3             |
| libgtk-3   | libgtk-3-dev          | Installed | 3.24.33-1ubuntu2        |
| libwebkit  | libwebkit2gtk-4.0-dev | Installed | 2.42.2-0ubuntu0.22.04.1 |
| npm        | npm                   | Installed | 10.2.0                  |
| *nsis      | nsis                  | Available | 3.08-2                  |
| pkg-config | pkg-config            | Installed | 0.29.2-1ubuntu3         |
└──────────────────────── * - Optional Dependency ─────────────────────────┘

Additional context

  1. This issue was observed in V2.
    
  2. The application's shutdown logic works correctly when the application window is closed, indicating that the issue is specific to handling Ctrl+C termination.
    
  3. The inconsistency in behavior between window closure and Ctrl+C termination suggests a potential issue in how Wails handles OS signals for application termination.
    

caesaneer avatar Nov 24 '23 21:11 caesaneer

Upon further investigation, I've observed a behavior in Wails that might be contributing to this issue. The handling of the Ctrl+C signal appears to be implemented in a separate goroutine within the Wails framework, specifically in the Start function of the signal handling code (signal.go). This goroutine is responsible for invoking the shutdown callbacks. However, it does not seem to block the main application thread. As a result, when Ctrl+C is used to terminate the application, it exits prematurely, not allowing the shutdown handler to complete its execution fully.

In contrast, when the application window is closed, the shutdown process appears to be invoked in a manner that blocks the main application thread, thereby allowing the shutdown procedure to complete properly. This difference in the shutdown behavior between window closure and Ctrl+C termination could be a key factor in the issue observed.

This observation suggests that the way Wails handles OS signals for termination, particularly Ctrl+C, might need adjustment to ensure a consistent and complete shutdown process across different termination methods.

caesaneer avatar Nov 24 '23 21:11 caesaneer