Skip to content

Commit

Permalink
fix: Do not use KeyboardInterrupt for flow control
Browse files Browse the repository at this point in the history
As with Austin TUI, the code has been refactored to remove flow control
via KeyboardInterrupt.
  • Loading branch information
P403n1x87 committed Jan 6, 2021
1 parent fc17c8e commit 301185b
Show file tree
Hide file tree
Showing 4 changed files with 482 additions and 402 deletions.
28 changes: 15 additions & 13 deletions austin_web/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ async def start(self, args: List[str]) -> None:
except AustinTerminated:
pass
except AustinError as e:
raise KeyboardInterrupt("Failed to start Austin") from e
asyncio.get_event_loop().stop()
raise e

def run(self) -> None:
"""Run Austin Web."""
Expand All @@ -251,16 +252,20 @@ def run(self) -> None:
loop = asyncio.get_event_loop()

try:
loop.create_task(self.start(AustinWebArgumentParser.to_list(self._args)))
austin_task = loop.create_task(
self.start(AustinWebArgumentParser.to_list(self._args))
)
loop.run_forever()
except KeyboardInterrupt as e:
if e.__cause__:
print(
"❌ Austin failed to start. Please ensure that the Austin binary\n"
"is on the PATH environment variable and that the command line\n"
"arguments are correct."
)
pass
if not austin_task.done():
austin_task.cancel()
austin_task.result()
except AustinError as e:
(message,) = e.args
if message[0] == "(":
_, _, message = message.partition(") ")
print(message)
except KeyboardInterrupt:
print()
finally:
self.shutdown()

Expand Down Expand Up @@ -292,9 +297,6 @@ def shutdown(self) -> None:
except (AustinError, asyncio.CancelledError):
pass

if self._global_stats:
print(self._global_stats)


def _main(cls: Type[AustinWeb], args: List[str]) -> None:
cls(args).run()
Expand Down
Loading

0 comments on commit 301185b

Please sign in to comment.