Skip to content

Commit

Permalink
Add handling to detect abrupt parent Python process termination for h…
Browse files Browse the repository at this point in the history
…otkeys process
  • Loading branch information
spyoungtech committed Feb 6, 2024
1 parent 859161b commit 03f065c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
31 changes: 23 additions & 8 deletions ahk/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2806,6 +2806,7 @@

HOTKEYS_SCRIPT_TEMPLATE = r"""#Requires AutoHotkey v1.1.17+
#Persistent
#Warn
{% for directive in directives %}
{% if directive.apply_to_hotkeys_process %}
Expand All @@ -2817,6 +2818,7 @@
OnClipboardChange("ClipChanged")
{% endif %}
KEEPALIVE := Chr(57344)
stdin := FileOpen("*", "r `n", "UTF-8")
SetTimer, keepalive, 1000
Crypt32 := DllCall("LoadLibrary", "Str", "Crypt32.dll", "Ptr")
Expand Down Expand Up @@ -2902,8 +2904,14 @@
keepalive:
global KEEPALIVE
FileAppend, %KEEPALIVE%`n, *, UTF-8
global KEEPALIVE
global stdin
FileAppend, %KEEPALIVE%`n, *, UTF-8
alivesignal := RTrim(stdin.ReadLine(), "`n")
if (alivesignal = "") {
ExitApp
}
return
"""

Expand Down Expand Up @@ -5805,9 +5813,9 @@
KEEPALIVE := Chr(57344)
;SetTimer, keepalive, 1000
stdout := FileOpen("*", "w", "UTF-8")
stdin := FileOpen("*", "r `n", "UTF-8")
WriteStdout(s) {
global stdout
Expand Down Expand Up @@ -5902,10 +5910,17 @@
OnClipboardChange(ClipChanged)
{% endif %}
;keepalive:
;global KEEPALIVE
;FileAppend, %KEEPALIVE%`n, *, UTF-8
SetTimer KeepAliveFunc, 1000
KeepAliveFunc() {
global stdin
global KEEPALIVE
WriteStdout(Format("{}`n", KEEPALIVE))
alivesignal := RTrim(stdin.ReadLine(), "`n")
if (alivesignal = "") {
ExitApp
}
return
}
"""
5 changes: 4 additions & 1 deletion ahk/_hotkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,14 @@ def listener(self) -> None:
stderr=subprocess.STDOUT,
)
atexit.register(kill, self._proc)
assert self._proc.stdout is not None
assert self._proc.stdin is not None
while self._running:
assert self._proc.stdout is not None
line = self._proc.stdout.readline()
if line.rstrip(b'\n') == _KEEPALIVE_SENTINEL:
logging.debug('keepalive received')
self._proc.stdin.write(b'alive\n')
self._proc.stdin.flush()
continue
if not line.strip():
logging.debug('Listener: Process probably died, exiting')
Expand Down
19 changes: 13 additions & 6 deletions ahk/templates/hotkeys-v2.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@


KEEPALIVE := Chr(57344)
;SetTimer, keepalive, 1000

stdout := FileOpen("*", "w", "UTF-8")
stdin := FileOpen("*", "r `n", "UTF-8")

WriteStdout(s) {
global stdout
Expand Down Expand Up @@ -105,8 +105,15 @@ ClipChanged(Type) {
OnClipboardChange(ClipChanged)

{% endif %}


;keepalive:
;global KEEPALIVE
;FileAppend, %KEEPALIVE%`n, *, UTF-8
SetTimer KeepAliveFunc, 1000

KeepAliveFunc() {
global stdin
global KEEPALIVE
WriteStdout(Format("{}`n", KEEPALIVE))
alivesignal := RTrim(stdin.ReadLine(), "`n")
if (alivesignal = "") {
ExitApp
}
return
}
12 changes: 10 additions & 2 deletions ahk/templates/hotkeys.ahk
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Requires AutoHotkey v1.1.17+
#Persistent
#Warn
{% for directive in directives %}
{% if directive.apply_to_hotkeys_process %}

Expand All @@ -11,6 +12,7 @@
OnClipboardChange("ClipChanged")
{% endif %}
KEEPALIVE := Chr(57344)
stdin := FileOpen("*", "r `n", "UTF-8")
SetTimer, keepalive, 1000

Crypt32 := DllCall("LoadLibrary", "Str", "Crypt32.dll", "Ptr")
Expand Down Expand Up @@ -96,5 +98,11 @@ ClipChanged(Type) {


keepalive:
global KEEPALIVE
FileAppend, %KEEPALIVE%`n, *, UTF-8
global KEEPALIVE
global stdin
FileAppend, %KEEPALIVE%`n, *, UTF-8
alivesignal := RTrim(stdin.ReadLine(), "`n")
if (alivesignal = "") {
ExitApp
}
return

0 comments on commit 03f065c

Please sign in to comment.