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

example/gui-linux does not exit when vm.Stop() is used #150

Closed
cfergeau opened this issue Nov 10, 2023 · 6 comments · Fixed by #173
Closed

example/gui-linux does not exit when vm.Stop() is used #150

cfergeau opened this issue Nov 10, 2023 · 6 comments · Fixed by #173
Labels
bug Something isn't working

Comments

@cfergeau
Copy link
Contributor

cfergeau commented Nov 10, 2023

Describe the bug
The GUI code in virtualization_view.m is notified when there was a guest initiated shutdown ('guestDidStopVirtualMachine') and when there was a virtualization error ('didStopWithError').

When calling Stop(), the VM is forcefully stopped (similar to pulling the plug on real hardware). This action is neither a guest initiated shutdown, nor a virtualization error, so the GUI code does not catch it. This means after calling vm.Stop(), the GUI main loop will keep running, and the application using Code-Hex/vz will never exit.

The branch https://github.com/cfergeau/vz/tree/hardstop-gui has a fix for this bug. It adds an observer for VM state changes, and calls 'terminate' when the VM state becomes 'stopped' or 'error'.

I can file a PR if this approach is acceptable to you, or I can explore a different fix if needed.

This was initially reported as crc-org/vfkit#67

To Reproduce
Steps to reproduce the behavior:

  1. Apply this patch
diff --git a/example/gui-linux/main.go b/example/gui-linux/main.go
index 5db3f99..b98bdd7 100644
--- a/example/gui-linux/main.go
+++ b/example/gui-linux/main.go
@@ -77,6 +77,17 @@ func run(ctx context.Context) error {
                }
        }()
 
+       go func() {
+               if !vm.CanStop() {
+                       log.Println("cannot stop vm forcefully")
+                       return
+               }
+               time.Sleep(10 * time.Second)
+               log.Println("calling vm.Stop()")
+
+               vm.Stop()
+       }()
+
        // cleanup is this function is useful when finished graphic application.
        cleanup := func() {
                for i := 1; vm.CanRequestStop(); i++ {

(this change is made in my branch https://github.com/cfergeau/vz/tree/hardstop-gui)

  1. Start gui-linux

Expected behavior

The process stops after 10 seconds.

Environment that you use to compile (please complete the following information):

  • macOS Version: [sw_vers | pbcopy]
ProductVersion:		13.6.1
BuildVersion:		22G313
  • mac architecture: [uname -m | pbcopy] arm64
  • Go Version: [go version | pbcopy] go version go1.20.5 darwin/arm64
@cfergeau cfergeau added the bug Something isn't working label Nov 10, 2023
cfergeau added a commit to cfergeau/vz that referenced this issue Nov 10, 2023
The GUI code in virtualization_view.m is notified when there was a guest
initiated shutdown ('guestDidStopVirtualMachine') and when there was a
virtualization error ('didStopWithError').

When calling Stop(), the VM is forcefully stopped (similar to pulling
the plug on real hardware). This action is neither a guest initiated
shutdown, nor a virtualization error, so the GUI code does not catch
it. This means after calling vm.Stop(), the GUI main loop will keep
running, and the application using Code-Hex/vz will never exit.

This commit fixes this by adding an observer for VM state changes, and
by calling 'terminate' when the VM state becomes 'stopped' or 'error'.

This fixes Code-Hex#150
cfergeau added a commit to cfergeau/vz that referenced this issue Jan 23, 2024
The GUI code in virtualization_view.m is notified when there was a guest
initiated shutdown ('guestDidStopVirtualMachine') and when there was a
virtualization error ('didStopWithError').

When calling Stop(), the VM is forcefully stopped (similar to pulling
the plug on real hardware). This action is neither a guest initiated
shutdown, nor a virtualization error, so the GUI code does not catch
it. This means after calling vm.Stop(), the GUI main loop will keep
running, and the application using Code-Hex/vz will never exit.

This commit fixes this by adding an observer for VM state changes, and
by calling 'terminate' when the VM state becomes 'stopped' or 'error'.

This fixes Code-Hex#150
cfergeau added a commit to cfergeau/vz that referenced this issue Mar 29, 2024
The GUI code in virtualization_view.m is notified when there was a guest
initiated shutdown ('guestDidStopVirtualMachine') and when there was a
virtualization error ('didStopWithError').

When calling Stop(), the VM is forcefully stopped (similar to pulling
the plug on real hardware). This action is neither a guest initiated
shutdown, nor a virtualization error, so the GUI code does not catch
it. This means after calling vm.Stop(), the GUI main loop will keep
running, and the application using Code-Hex/vz will never exit.

This commit fixes this by adding an observer for VM state changes, and
by calling 'terminate' when the VM state becomes 'stopped' or 'error'.

This fixes Code-Hex#150
@Code-Hex
Copy link
Owner

@cfergeau Sorry, what was the problem with this issue?
Is the problem that calling stop does not stop the GUI event loop? I assume that clicking close exits the application, but is that not good enough?

cfergeau added a commit to cfergeau/vz that referenced this issue Nov 19, 2024
The GUI code in virtualization_view.m is notified when there was a guest
initiated shutdown ('guestDidStopVirtualMachine') and when there was a
virtualization error ('didStopWithError').

When calling Stop(), the VM is forcefully stopped (similar to pulling
the plug on real hardware). This action is neither a guest initiated
shutdown, nor a virtualization error, so the GUI code does not catch
it. This means after calling vm.Stop(), the GUI main loop will keep
running, and the application using Code-Hex/vz will never exit.

This commit fixes this by adding an observer for VM state changes, and
by calling 'terminate' when the VM state becomes 'stopped' or 'error'.

This fixes Code-Hex#150
@cfergeau
Copy link
Contributor Author

@cfergeau Sorry, what was the problem with this issue? Is the problem that calling stop does not stop the GUI event loop? I assume that clicking close exits the application, but is that not good enough?

Yes, calling vm.Stop() does not exit the event loop. This is different behaviour from what happens if it is the guest which shuts down (ie vm.RequestStop()). Clicking close exits the application, but I want to do close the window programmatically. The window is only used to show a debug console for display purpose, it's unexpected that in some cases the user has to close this debug console to be able to exit the command line application.

@Code-Hex
Copy link
Owner

@cfergeau Thank you for your explanation. I'll fix it in the PR I'm currently opening.

Clicking close exits the application, but I want to do close the window programmatically.

@Code-Hex Code-Hex linked a pull request Nov 19, 2024 that will close this issue
@Code-Hex
Copy link
Owner

@cfergeau Could you check this comment please 🙏
#173 (comment)

@lstocchi
Copy link
Contributor

@Code-Hex i saw it is already merged but i wanted to confirm that the fix works fine. @cfergeau was busy on other tasks and asked me to verify. Thank you!!

@Code-Hex
Copy link
Owner

@lstocchi Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants