Skip to content

Commit 29cf38e

Browse files
committed
ported over interrupt fix from related library
1 parent f15c019 commit 29cf38e

1 file changed

Lines changed: 37 additions & 12 deletions

File tree

‎hackrfpy/tests/conftest.py‎

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,41 @@ def fixtures_dir():
6969
IDLE = {idle!r}
7070
EXIT_CODE = {exit_code!r}
7171
EMIT_BYTES = {emit_bytes!r}
72+
TAIL_ON_INTERRUPT = {tail_on_interrupt!r}
73+
IGNORE_INTERRUPT = {ignore_interrupt!r}
7274
7375
def _on_signal(signum, frame):
76+
# record WHICH signal arrived, so tests can distinguish the clean
77+
# interrupt (SIGINT / SIGBREAK from CTRL_BREAK_EVENT) from the
78+
# terminate() escalation -- "the child died" is not "the child was
79+
# interrupted cleanly"
7480
if MARKER:
7581
try:
76-
open(MARKER, "w").close()
77-
except OSError:
78-
pass
82+
with open(MARKER, "w") as fh:
83+
fh.write(signal.Signals(signum).name)
84+
except (OSError, ValueError):
85+
try:
86+
open(MARKER, "w").close()
87+
except OSError:
88+
pass
89+
if TAIL_ON_INTERRUPT:
90+
# the hackrf_transfer contract: flush buffered output BEFORE dying,
91+
# so the capture file is never truncated mid-sample-pair
92+
sys.stdout.write(TAIL_ON_INTERRUPT + "\\n")
93+
sys.stdout.flush()
7994
sys.exit(0)
8095
81-
signal.signal(signal.SIGINT, _on_signal)
82-
if hasattr(signal, "SIGBREAK"):
83-
signal.signal(signal.SIGBREAK, _on_signal)
84-
if hasattr(signal, "SIGTERM"):
85-
signal.signal(signal.SIGTERM, _on_signal)
96+
if IGNORE_INTERRUPT:
97+
# deaf child: exercises the stop() grace-timeout -> terminate() path
98+
signal.signal(signal.SIGINT, signal.SIG_IGN)
99+
if hasattr(signal, "SIGBREAK"):
100+
signal.signal(signal.SIGBREAK, signal.SIG_IGN)
101+
else:
102+
signal.signal(signal.SIGINT, _on_signal)
103+
if hasattr(signal, "SIGBREAK"):
104+
signal.signal(signal.SIGBREAK, _on_signal)
105+
if hasattr(signal, "SIGTERM"):
106+
signal.signal(signal.SIGTERM, _on_signal)
86107
87108
for line in STDERR_LINES:
88109
sys.stderr.write(line + "\\n")
@@ -101,8 +122,9 @@ def _on_signal(signum, frame):
101122
sys.stdout.flush()
102123
103124
if IDLE:
104-
while True:
105-
if STDERR_LINES:
125+
_idle_until = time.monotonic() + 30.0 # safety ceiling: never leak a
126+
while time.monotonic() < _idle_until: # stub child on CI, even one
127+
if STDERR_LINES: # that ignores interrupts
106128
sys.stderr.write(STDERR_LINES[-1] + "\\n")
107129
sys.stderr.flush()
108130
time.sleep(0.02)
@@ -113,13 +135,16 @@ def _on_signal(signum, frame):
113135

114136
def _write_stub(tools_dir, name, *, stdout_lines=(), stderr_lines=(),
115137
stderr_flood=0, idle=False, exit_code=0, marker=None,
116-
emit_bytes=None):
138+
emit_bytes=None, tail_on_interrupt=None,
139+
ignore_interrupt=False):
117140
py_path = os.path.join(tools_dir, name + ".py")
118141
body = _STUB_TEMPLATE.format(
119142
marker=marker, stdout_lines=list(stdout_lines),
120143
stderr_lines=list(stderr_lines), stderr_flood=stderr_flood,
121144
idle=idle, exit_code=exit_code,
122-
emit_bytes=list(emit_bytes) if emit_bytes else None)
145+
emit_bytes=list(emit_bytes) if emit_bytes else None,
146+
tail_on_interrupt=tail_on_interrupt,
147+
ignore_interrupt=ignore_interrupt)
123148
with open(py_path, "w") as f:
124149
f.write(body)
125150

0 commit comments

Comments
 (0)