Skip to content

Commit

Permalink
handle SIGINT and SIGTERM (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
karmacoma-eth authored Oct 24, 2023
1 parent 4620b71 commit 6666a32
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/halmos/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import uuid
import json
import re
import signal
import traceback

from argparse import Namespace
Expand Down Expand Up @@ -1325,15 +1326,38 @@ def _main(_args=None) -> MainResult:

timer.create_subtimer("tests")

#
# run
#

total_passed = 0
total_failed = 0
total_found = 0
test_results_map = {}

#
# exit and signal handlers to avoid dropping json output
#

def on_exit(exitcode: int) -> MainResult:
result = MainResult(exitcode, test_results_map)

if args.json_output:
with open(args.json_output, "w") as json_file:
json.dump(asdict(result), json_file, indent=4)

return result

def on_signal(signum, frame):
if args.debug:
debug(f"Signal {signum} received. Dumping {test_results_map}...")
exitcode = 128 + signum
on_exit(exitcode)
sys.exit(exitcode)

for signum in [signal.SIGINT, signal.SIGTERM]:
signal.signal(signum, on_signal)

#
# run
#

for build_out_map, filename, contract_name in build_output_iterator(build_out):
if args.contract and args.contract != contract_name:
continue
Expand Down Expand Up @@ -1404,13 +1428,7 @@ def _main(_args=None) -> MainResult:
return MainResult(1)

exitcode = 0 if total_failed == 0 else 1
result = MainResult(exitcode, test_results_map)

if args.json_output:
with open(args.json_output, "w") as json_file:
json.dump(asdict(result), json_file, indent=4)

return result
return on_exit(exitcode)


# entrypoint for the `halmos` script
Expand Down
8 changes: 8 additions & 0 deletions src/halmos/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ def color_info(text: str) -> str:
return cyan(text)


def color_debug(text: str) -> str:
return magenta(text)


def error(text: str) -> None:
print(color_error(text))

Expand All @@ -381,6 +385,10 @@ def info(text: str) -> None:
print(color_info(text))


def debug(text: str) -> None:
print(color_debug(text))


def indent_text(text: str, n: int = 4) -> str:
return "\n".join(" " * n + line for line in text.splitlines())

Expand Down

0 comments on commit 6666a32

Please sign in to comment.