Skip to content

Commit b14107e

Browse files
committed
Catch SIGINT/Exit during rollbacks
Closes #133 Signed-off-by: Sylvain Hellegouarch <sh@defuze.org>
1 parent ddfafb5 commit b14107e

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
### Added
88

99
- Catch `InterruptExecution` during rollbacks so that the experiment terminates
10-
gracefully [#132][132]
10+
gracefully [#132][132]. The remaining rollbacks are not applied.
11+
- Catch `SIGINT` and `SystemExit` during rollbacks so that the experiment
12+
terminates gracefully [#133][133]. The remaining rollbacks are not applied.
1113

1214
[132]: https://github.com/chaostoolkit/chaostoolkit-lib/issues/132
15+
[133]: https://github.com/chaostoolkit/chaostoolkit-lib/issues/133
1316

1417
## [1.7.0][] - 2019-09-21
1518

chaoslib/experiment.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ def run_experiment(experiment: Experiment,
254254
except InterruptExecution as i:
255255
journal["status"] = "interrupted"
256256
logger.fatal(str(i))
257+
except (KeyboardInterrupt, SystemExit):
258+
journal["status"] = "interrupted"
259+
logger.warn(
260+
"Received an exit signal."
261+
"Terminating now without running the remaining rollbacks.")
257262

258263
journal["end"] = datetime.utcnow().isoformat()
259264
journal["duration"] = time.time() - started_at

tests/test_experiment.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,36 @@ def handler(signum, frame):
187187
pytest.fail("we should have swalled the InterruptExecution exception")
188188

189189

190+
def test_can_interrupt_rollbacks_on_SystemExit():
191+
def handler(signum, frame):
192+
raise SystemExit()
193+
194+
signal.signal(signal.SIGALRM, handler)
195+
signal.alarm(1)
196+
197+
try:
198+
journal = run_experiment(experiments.ExperimentWithRollbackLongPause)
199+
assert isinstance(journal, dict)
200+
assert journal["status"] == "interrupted"
201+
except SystemExit:
202+
pytest.fail("we should have swalled the SystemExit exception")
203+
204+
205+
def test_can_interrupt_rollbacks_on_SIGINT():
206+
def handler(signum, frame):
207+
raise KeyboardInterrupt()
208+
209+
signal.signal(signal.SIGALRM, handler)
210+
signal.alarm(1)
211+
212+
try:
213+
journal = run_experiment(experiments.ExperimentWithRollbackLongPause)
214+
assert isinstance(journal, dict)
215+
assert journal["status"] == "interrupted"
216+
except SystemExit:
217+
pytest.fail("we should have swalled the KeyboardInterrupt exception")
218+
219+
190220
def test_probes_can_reference_each_other():
191221
experiment = experiments.RefProbeExperiment.copy()
192222
experiment["dry"] = True

0 commit comments

Comments
 (0)