Skip to content

Commit 1305aac

Browse files
committed
Fix the app freeze when closing the console window
The app was freezing for a few seconds when closing the console window of a script where the main Python code has finished. This used to happen during the removal of the custom menus, particularly during the PyGILState_Ensure call to decref the menu callbacks.
1 parent db733ff commit 1305aac

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

ahkpy/Python.ahk

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

1010
global HPYTHON_DLL := NULL
1111
global PYTHON_DLL_PROCS := {}
12+
global EMERGENCY_EXIT := false
1213

1314
; Windows constants
1415
global LOAD_WITH_ALTERED_SEARCH_PATH := 0x8
@@ -261,6 +262,7 @@ SetArgs() {
261262
HandleCtrlEvent(signal) {
262263
if (signal == CTRL_CLOSE_EVENT) {
263264
; Exit when the console window is closed.
265+
EMERGENCY_EXIT := true
264266
ExitApp, 0
265267
}
266268
; Let the other handlers do the work.
@@ -433,6 +435,13 @@ class WrappedPythonCallable {
433435

434436
__Delete() {
435437
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+
}
436445
gstate := PyGILState_Ensure()
437446
try {
438447
Py_DecRef(this.pyFunc)

0 commit comments

Comments
 (0)