Skip to content

Commit 48a99cb

Browse files
committed
Schedule the exit in the main thread when closing the console window
This is a different approach to fixing the problem from the one that was implemented in 1305aac.
1 parent 93d640c commit 48a99cb

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

ahkpy/Lib/Commands.ahk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ _EnvGet(EnvVarName) {
103103
return OutputVar
104104
}
105105

106+
_ExitApp(ExitCode=0) {
107+
ExitApp, %ExitCode%
108+
}
109+
106110
_FileCreateShortcut(Target,LinkFile,WorkingDir="",Args="",Description="",IconFile="",ShortcutKey="",IconNumber="",RunState="") {
107111
FileCreateShortcut %Target%,%LinkFile%,%WorkingDir%,%Args%,%Description%,%IconFile%,%ShortcutKey%,%IconNumber%,%RunState%
108112
}

ahkpy/Python.ahk

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ global EMPTY_STRING := ""
99

1010
global HPYTHON_DLL := NULL
1111
global PYTHON_DLL_PROCS := {}
12-
global EMERGENCY_EXIT := false
1312

1413
; Windows constants
1514
global LOAD_WITH_ALTERED_SEARCH_PATH := 0x8
@@ -262,8 +261,15 @@ SetArgs() {
262261
HandleCtrlEvent(signal) {
263262
if (signal == CTRL_CLOSE_EVENT) {
264263
; Exit when the console window is closed.
265-
EMERGENCY_EXIT := true
266-
ExitApp, 0
264+
;
265+
; The system creates a new thread in the process to execute the
266+
; HandleCtrlEvent function. Calling ExitApp here in this thread will
267+
; eventually try to acquire the GIL in order to clean up the menu
268+
; handlers. However, it won't succeed because the GIL will have been
269+
; acquired by the main thread. So instead, schedule the exit to be
270+
; executed in the main thread.
271+
SetTimer, _ExitApp, -1
272+
Sleep, 100
267273
}
268274
; Let the other handlers do the work.
269275
return false
@@ -435,13 +441,6 @@ class WrappedPythonCallable {
435441

436442
__Delete() {
437443
WRAPPED_PYTHON_CALLABLE.Delete(this.pyFunc)
438-
if (EMERGENCY_EXIT) {
439-
; For some reason, the app freezes trying to acquire the GIL when
440-
; the user closes the console window. So, freeing the memory here is
441-
; not as important, since the app is going to exit and free all the
442-
; memory anyway.
443-
return
444-
}
445444
gstate := PyGILState_Ensure()
446445
try {
447446
Py_DecRef(this.pyFunc)

0 commit comments

Comments
 (0)