Skip to content

Commit 9584365

Browse files
committed
Give up on real CTRL_C_EVENT testing on Windows
1 parent aba3839 commit 9584365

4 files changed

Lines changed: 87 additions & 119 deletions

File tree

‎appveyor.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ test_script:
2222
- "mkdir empty"
2323
- "cd empty"
2424
- "python -c \"import os, trio; print(os.path.dirname(trio.__file__))\""
25-
- "python ../ci/run-in-new-console.py \"python -m pytest -ra --pyargs trio -v -s --cov=trio --cov-config=../.coveragerc\""
25+
# -u makes sure we prompt output
26+
- "python -u -m pytest -ra --pyargs trio -v -s --cov=trio --cov-config=../.coveragerc"
2627
- "codecov"

‎ci/run-in-new-console.py‎

Lines changed: 0 additions & 63 deletions
This file was deleted.

‎ci/run-in-new-process-group.py‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎trio/_core/tests/test_ki.py‎

Lines changed: 85 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,98 @@
33
import os
44
import signal
55
import threading
6-
import time
76

87
from ... import _core
98
from ...testing import wait_run_loop_idle
109

1110
if os.name == "nt":
12-
from .._windows_cffi import ffi, kernel32
13-
# Make sure that we're not ignoring CTRL_C_EVENT. (This is important b/c
14-
# on appveyor we run the testsuite with CREATE_NEW_PROCESS_GROUP, which
15-
# sets the CTRL_C_EVENT ignore flag to TRUE in children as a side-effect.)
16-
kernel32.SetConsoleCtrlHandler(ffi.NULL, 0)
17-
11+
# I looked at this pretty hard and I'm pretty sure there isn't any way to
12+
# deliver a real CTRL_C_EVENT to the test process that works reliably on
13+
# Appveyor etc.
14+
#
15+
# You can make a synthetic CTRL_C_EVENT using GenerateConsoleCtrlEvent:
16+
#
17+
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms683155(v=vs.85).aspx
18+
#
19+
# However, this isn't directed at a *process*. Your options are:
20+
#
21+
# 1) send it to process 0, which means "everyone attached to this
22+
# console".
23+
#
24+
# 2) If a process is a group leader, then you can pass in its PID, and
25+
# which case it might send it to just the processes that are in that group
26+
# and also share a console with the caller. Or maybe not -- the page above
27+
# claims that this doesn't work for CTRL_C_EVENT, only
28+
# CTRL_BREAK_EVENT. But it's clearly wrong, because it did work for me in
29+
# some cases?
30+
#
31+
# 3) If a process is *not* a group leader and you pass in its PID, then
32+
# the results are not documented. But empirically it seems like it might
33+
# act like you passed 0:
34+
#
35+
# https://stackoverflow.com/questions/42180468
36+
#
37+
# So our problem is that we want to deliver a CTRL_C_EVENT to the current
38+
# process, without causing side-effects like freezing Appveyor.
39+
#
40+
# There are two strategies that suggest themselves: either run pytest as a
41+
# new process group, so we're a process group leader, or else run pytest
42+
# on a new console.
43+
#
44+
# There are some annoyances here that I do know how to overcome. If
45+
# creating a new process group: (a) we need to spawn Python directly,
46+
# e.g. "python -m pytest". If we use a entry-point script like "pytest",
47+
# or a launcher like "py", then it doesn't work -- probably because then
48+
# the launching process ends up as group leader, so the actual test
49+
# process isn't, and that breaks things as per above. (b)
50+
# CREATE_NEW_PROCESS_GROUP also sets an internal flag that makes it so the
51+
# spawned process ignores CTRL_C_EVENT. We have to flip that flag back by
52+
# hand, using SetConsoleCtrlHandler(NULL, FALSE).
53+
#
54+
# If creating a new console: we need to hide the new console window
55+
# (shell=True in subprocess.Popen), and pass in a stdout/stderr PIPE and
56+
# pump the output back by hand. (Otherwise the output goes to the new
57+
# console window, which on Appveyor is obviously useless regardless of
58+
# whether the new window is hidden.) AFAICT there's no way to directly
59+
# pass in the original console's output handles as handles for the child
60+
# process, probably because console handles are weird virtual handle
61+
# things.
62+
#
63+
# The docs claim that if you try to do both at once (CREATE_NEW_CONSOLE |
64+
# CREATE_NEW_PROCESS_GROUP) then that's equivalent to CREATE_NEW_CONSOLE
65+
# alone. I haven't tried to check this.
66+
#
67+
# There is also some subtlety required when using GenerateConsoleCtrlEvent
68+
# to make sure that the event is actually delivered at the right time --
69+
# for the tests we want the Python-level signal handler to run before we
70+
# exit ki_self, but this is tricky because the CTRL_C_EVENT handler runs
71+
# in a new thread that isn't synchronized to anything.
72+
#
73+
# Anyway. Having done all the work to deal with this issues, both of these
74+
# approaches work... kind of. They work locally. And I was able to get
75+
# them to work seemingly-reliably on Appveyor (like, passing 8/8 runs),
76+
# BUT this required weird tweaking -- in particular, the working runs were
77+
# like
78+
#
79+
# python -u -m pytest -v -s
80+
#
81+
# Yes, it is critically important that we run python with unbuffered stdio
82+
# (-u). Without this then there are freezes on a regular (but not 100%
83+
# deterministic) basis. Maybe there is something where you have to
84+
# actually interact with the console in order to get CTRL_C_EVENT
85+
# delivered? It's totally baffling to me.
86+
#
87+
# So anyway, I just don't trust this. I'd much rather have a test suite
88+
# that runs 100% reliably and tests 99% of the stuff than a test suite
89+
# that runs 99% reliably and tests 100% of the stuff. So we fake it.
1890
def ki_self():
19-
# On Windows, GenerateConsoleCtrlEvent spawns a thread to run the C
20-
# handler. We want the event to happen right *here*, so we manually
21-
# rendezvous with the handler before continuing. (Windows allows
22-
# multiple handlers for the same event, so this isn't too hard.)
23-
ev = threading.Event()
24-
@ffi.callback("BOOL WINAPI(DWORD)")
25-
def cb(dwCtrlType):
26-
print("ConsoleCtrlHandler callback!", dwCtrlType)
27-
ev.set()
28-
# 0 = FALSE = keep running handlers after this
29-
return 0
30-
try:
31-
kernel32.SetConsoleCtrlHandler(cb, 1)
32-
# os.kill has a special case where if you pass it CTRL_C_EVENT on
33-
# Windows then it calls GenerateConsoleCtrlEvent. Passing SIGINT
34-
# is totally different -- that just calls TerminateProcess. So
35-
# this raises a CTRL_C_EVENT in the current process
36-
# group. Hopefully that's just us...
37-
os.kill(0, signal.CTRL_C_EVENT)
38-
ev.wait()
39-
finally:
40-
kernel32.SetConsoleCtrlHandler(cb, 0)
41-
# Even this isn't quite enough because it only guarantees that the
42-
# first handler has run; it doesn't guarantee that the later ones
43-
# (like CPython's!) have, or that CPython has noticed and run the
44-
# Python-level handler. So we also do a short sleep.
45-
#
46-
# It this turns out to be unreliable, another approach would be to
47-
# rendezvous by registering our own Python-level signal handler that
48-
# calls the previously registered one and then signals the event. I'm
49-
# a little hesitant to mess with the Python-level signal handler here
50-
# since it's what we're trying to test, but it'd probably be okay...
51-
time.sleep(0.1)
91+
assert threading.current_thread() == threading.main_thread()
92+
handler = signal.getsignal(signal.SIGINT)
93+
handler(signal.SIGINT, sys._getframe())
5294
else:
53-
# On Unix, kill invokes the C handler synchronously, and then os.kill
54-
# immediately checks for this and runs the Python handler before
55-
# returning. So... that's easy.
95+
# On Unix, kill invokes the C handler synchronously, in this process only,
96+
# and then os.kill immediately checks for this and runs the Python handler
97+
# before returning. So... that's easy.
5698
def ki_self():
5799
os.kill(os.getpid(), signal.SIGINT)
58100

0 commit comments

Comments
 (0)