Python 3.11 subprocess is not ending on Windows

on Windows using Python 3.11 when I launch a subprocess it is almost as if the process is blocked from exiting itself.

This same bit of code works perfectly fine on Linux and macOS

    def launch_game(self):
        self.hide_progress_bar()
        # launch the game
        if sys.platform in ["win32", "win64"]:
            game = subprocess.Popen('Game', creationflags=134217728)
        elif sys.platform == "darwin":
            modes = os.stat('Game').st_mode
            if not modes & stat.S_IXUSR:
                os.chmod('Game', modes | stat.S_IXUSR)
            game = subprocess.Popen('Game')
        elif sys.platform in ["linux", "linux2"]:
            # set WINEDEBUG to -all to hide the annoying wine debug messages
            os.environ["WINEDEBUG"] = "-all"
            game = subprocess.Popen(['wine', 'Game.exe'])
        else:
            logging.warning("Unsupported platform")
            return
        time.sleep(1.5)

        # hide the window
        self.showMinimized()
        self.setWindowFlags(self.windowFlags() & Qt.CustomizeWindowHint)
        self.setWindowFlags(self.windowFlags() & ~Qt.WindowMinMaxButtonsHint)
        while game.poll() is None:
            time.sleep(1.5)
        if game.returncode == 0:
            self.restore_window()
            self.update_status("Thanks for playing!")
        else:
            self.restore_window()
            self.update_status_error("An unexpected error occurred.")

I’ve tried using subprocess.run, using pywin32, etc; it all results in the same issue only on Windows, where the process is unable to exit/terminate itself when closed.
If the app is closed, the Game process closes, but if I manually close the Game process in Task Manager, the code in the app continues as expected when using subprocess.run.
If I use subprocess.Popen the app completely deadlocks, not even closing the process will allow the code to continue. I’ve also tried with and without the creationflags so I am lost at this point.
The Game I am trying to run is a Python 2.4 compiled 32-bit executable if this helps with a solution at all.

  • 1

    Consider using QProcess instead, so that you won’t need to use blocking loops or functions, which should always be avoided in UI environments.

    – 

  • I’ll try this and report back. But I think I’ve tried this in the beginning, just don’t quite remember.

    – 




  • It appears using QProcess has the same effect as Popen where the process won’t end, and closing the process doesn’t allow the MainWindow to continue executing code. game = QProcess() game.setProgram(“Game.exe”) game.startDetached() This is what I tried, I also tried to use game.start() but it destroys the thread while it is still running. QProcess: Destroyed while process (“Game.exe”) is still running. Game return code: 62097

    – 




  • If you don’t keep a reference to the process, it will obviously get garbage collected (thus, “destroyed”). If the target program doesn’t terminate its own process when you attempt to close it, then there’s probably something wrong with that program (but you are not explaining how you’re actually trying to close it).

    – 

  • I’ve looked at some original code, and it just looks like it is doing sys.exit when closing the Game. I’ve tried closing it from the X button and from the Quit button within the game and it remains running regardless. If I use subprocess.run however, and I close the process in the Task Manager; the code continues as it should. The best way I can describe it is the Game is open even when it has already closed. When the game closes, its log file reports: :11-26-2023 11:02:44 TaskManager: TaskManager.destroy() :11-26-2023 11:02:44 :display: Closing wdxGraphicsWindow8

    – 

Use subprocess.run for Simplicity:

Instead of using subprocess.Popen directly, consider using the higher-level subprocess.run function, which is designed to be simpler and can handle the process lifecycle more gracefully

Leave a Comment