mirror of
https://github.com/vee1e/mitmproxy.git
synced 2026-09-03 19:27:14 +00:00
Merge pull request #5318 from nneonneo/fix-exit-shutdown
Fix terminal breakage if an error occurs at startup
This commit is contained in:
commit
c91685efeb
2 changed files with 12 additions and 8 deletions
|
|
@ -1,6 +1,5 @@
|
|||
import asyncio
|
||||
import sys
|
||||
from typing import Optional
|
||||
|
||||
from mitmproxy import log
|
||||
|
||||
|
|
@ -9,12 +8,12 @@ class ErrorCheck:
|
|||
"""Monitor startup for error log entries, and terminate immediately if there are some."""
|
||||
|
||||
def __init__(self, log_to_stderr: bool = False):
|
||||
self.has_errored: Optional[str] = None
|
||||
self.has_errored: list[str] = []
|
||||
self.log_to_stderr = log_to_stderr
|
||||
|
||||
def add_log(self, e: log.LogEntry):
|
||||
if not self.has_errored and e.level == "error":
|
||||
self.has_errored = e.msg
|
||||
if e.level == "error":
|
||||
self.has_errored.append(e.msg)
|
||||
|
||||
async def running(self):
|
||||
# don't run immediately, wait for all logging tasks to finish.
|
||||
|
|
@ -23,5 +22,8 @@ class ErrorCheck:
|
|||
async def _shutdown_if_errored(self):
|
||||
if self.has_errored:
|
||||
if self.log_to_stderr:
|
||||
print(f"Error on startup: {self.has_errored}", file=sys.stderr)
|
||||
plural = "s" if len(self.has_errored) > 1 else ""
|
||||
msg = "\n".join(self.has_errored)
|
||||
print(f"Error{plural} on startup: {msg}", file=sys.stderr)
|
||||
|
||||
sys.exit(1)
|
||||
|
|
|
|||
|
|
@ -46,9 +46,11 @@ class Master:
|
|||
# Handle scheduled tasks (configure()) first.
|
||||
await asyncio.sleep(0)
|
||||
await self.running()
|
||||
await self.should_exit.wait()
|
||||
|
||||
await self.done()
|
||||
try:
|
||||
await self.should_exit.wait()
|
||||
finally:
|
||||
# .wait might be cancelled (e.g. by sys.exit)
|
||||
await self.done()
|
||||
finally:
|
||||
self.event_loop.set_exception_handler(old_handler)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue