diff --git a/test/openjd/sessions_v0/support_files/app_20s_run.py b/test/openjd/sessions_v0/support_files/app_20s_run.py index e384616d..e7005c78 100644 --- a/test/openjd/sessions_v0/support_files/app_20s_run.py +++ b/test/openjd/sessions_v0/support_files/app_20s_run.py @@ -7,14 +7,21 @@ # Hook SIGTERM (posix) or CTRL_BREAK_EVENT (windows) and print "Trapped" # and exit if we get the signal +import os import signal import sys import time def hook(handle, frame): - print("Trapped") - sys.stdout.flush() + # os.write(2) rather than print() + flush(): a signal handler can interrupt + # the main loop *inside* sys.stdout.flush(), and re-entering the same + # BufferedWriter raises + # RuntimeError: reentrant call inside <_io.BufferedWriter name=''> + # which loses this message and exits non-zero. write(2) is unbuffered, so it + # cannot re-enter. Every print here is flushed immediately, so nothing is + # sitting in the buffer for this to appear out of order with. + os.write(1, b"Trapped\n") sys.exit(1) diff --git a/test/openjd/sessions_v0/support_files/app_20s_run_ignore_signal.py b/test/openjd/sessions_v0/support_files/app_20s_run_ignore_signal.py index 111421e6..f263cb85 100644 --- a/test/openjd/sessions_v0/support_files/app_20s_run_ignore_signal.py +++ b/test/openjd/sessions_v0/support_files/app_20s_run_ignore_signal.py @@ -2,14 +2,21 @@ # As app_20s_run.py except it does not exit when it gets a SIGTERM/SIGBREAK +import os import signal import sys import time def hook(handle, frame): - print("Trapped") - sys.stdout.flush() + # os.write(2) rather than print() + flush(): a signal handler can interrupt + # the main loop *inside* sys.stdout.flush(), and re-entering the same + # BufferedWriter raises + # RuntimeError: reentrant call inside <_io.BufferedWriter name=''> + # which loses this message and exits non-zero. write(2) is unbuffered, so it + # cannot re-enter. Every print here is flushed immediately, so nothing is + # sitting in the buffer for this to appear out of order with. + os.write(1, b"Trapped\n") if sys.platform.startswith("win"): diff --git a/test/openjd/sessions_v1/support_files/app_20s_run.py b/test/openjd/sessions_v1/support_files/app_20s_run.py index e384616d..e7005c78 100644 --- a/test/openjd/sessions_v1/support_files/app_20s_run.py +++ b/test/openjd/sessions_v1/support_files/app_20s_run.py @@ -7,14 +7,21 @@ # Hook SIGTERM (posix) or CTRL_BREAK_EVENT (windows) and print "Trapped" # and exit if we get the signal +import os import signal import sys import time def hook(handle, frame): - print("Trapped") - sys.stdout.flush() + # os.write(2) rather than print() + flush(): a signal handler can interrupt + # the main loop *inside* sys.stdout.flush(), and re-entering the same + # BufferedWriter raises + # RuntimeError: reentrant call inside <_io.BufferedWriter name=''> + # which loses this message and exits non-zero. write(2) is unbuffered, so it + # cannot re-enter. Every print here is flushed immediately, so nothing is + # sitting in the buffer for this to appear out of order with. + os.write(1, b"Trapped\n") sys.exit(1) diff --git a/test/openjd/sessions_v1/support_files/app_20s_run_ignore_signal.py b/test/openjd/sessions_v1/support_files/app_20s_run_ignore_signal.py index 111421e6..f263cb85 100644 --- a/test/openjd/sessions_v1/support_files/app_20s_run_ignore_signal.py +++ b/test/openjd/sessions_v1/support_files/app_20s_run_ignore_signal.py @@ -2,14 +2,21 @@ # As app_20s_run.py except it does not exit when it gets a SIGTERM/SIGBREAK +import os import signal import sys import time def hook(handle, frame): - print("Trapped") - sys.stdout.flush() + # os.write(2) rather than print() + flush(): a signal handler can interrupt + # the main loop *inside* sys.stdout.flush(), and re-entering the same + # BufferedWriter raises + # RuntimeError: reentrant call inside <_io.BufferedWriter name=''> + # which loses this message and exits non-zero. write(2) is unbuffered, so it + # cannot re-enter. Every print here is flushed immediately, so nothing is + # sitting in the buffer for this to appear out of order with. + os.write(1, b"Trapped\n") if sys.platform.startswith("win"):