mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-30 03:14:22 +00:00
Merge pull request #3064 from cortesi/mexit
asyncio: fix exit behaviour for console
This commit is contained in:
commit
633023701b
@ -1,3 +1,5 @@
|
|||||||
|
import sys
|
||||||
|
import traceback
|
||||||
import threading
|
import threading
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
@ -85,18 +87,40 @@ class Master:
|
|||||||
self.addons.trigger("tick")
|
self.addons.trigger("tick")
|
||||||
await asyncio.sleep(0.1)
|
await asyncio.sleep(0.1)
|
||||||
|
|
||||||
def run(self):
|
def run_loop(self, loop):
|
||||||
self.start()
|
self.start()
|
||||||
asyncio.ensure_future(self.tick())
|
asyncio.ensure_future(self.tick())
|
||||||
|
|
||||||
|
exc = None
|
||||||
|
try:
|
||||||
|
loop()
|
||||||
|
except Exception as e:
|
||||||
|
exc = traceback.format_exc()
|
||||||
|
finally:
|
||||||
|
if not self.should_exit.is_set():
|
||||||
|
self.shutdown()
|
||||||
|
pending = asyncio.Task.all_tasks()
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
try:
|
try:
|
||||||
loop.run_forever()
|
|
||||||
finally:
|
|
||||||
pending = asyncio.Task.all_tasks()
|
|
||||||
loop.run_until_complete(asyncio.gather(*pending))
|
loop.run_until_complete(asyncio.gather(*pending))
|
||||||
|
except Exception as e:
|
||||||
|
# When we exit with an error, shutdown might not happen cleanly,
|
||||||
|
# and we can get exceptions here caused by pending Futures.
|
||||||
|
pass
|
||||||
loop.close()
|
loop.close()
|
||||||
|
|
||||||
|
if exc: # pragma: no cover
|
||||||
|
print(exc, file=sys.stderr)
|
||||||
|
print("mitmproxy has crashed!", file=sys.stderr)
|
||||||
|
print("Please lodge a bug report at:", file=sys.stderr)
|
||||||
|
print("\thttps://github.com/mitmproxy/mitmproxy", file=sys.stderr)
|
||||||
|
|
||||||
self.addons.trigger("done")
|
self.addons.trigger("done")
|
||||||
|
|
||||||
|
def run(self, func=None):
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
self.run_loop(loop.run_forever)
|
||||||
|
|
||||||
async def _shutdown(self):
|
async def _shutdown(self):
|
||||||
if self.server:
|
if self.server:
|
||||||
self.server.shutdown()
|
self.server.shutdown()
|
||||||
|
@ -9,7 +9,6 @@ import stat
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import traceback
|
|
||||||
import typing # noqa
|
import typing # noqa
|
||||||
import contextlib
|
import contextlib
|
||||||
|
|
||||||
@ -205,7 +204,6 @@ class ConsoleMaster(master.Master):
|
|||||||
screen = self.ui,
|
screen = self.ui,
|
||||||
handle_mouse = self.options.console_mouse,
|
handle_mouse = self.options.console_mouse,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.window = window.Window(self)
|
self.window = window.Window(self)
|
||||||
self.loop.widget = self.window
|
self.loop.widget = self.window
|
||||||
self.window.refresh()
|
self.window.refresh()
|
||||||
@ -216,24 +214,7 @@ class ConsoleMaster(master.Master):
|
|||||||
self.start_err = None
|
self.start_err = None
|
||||||
self.loop.set_alarm_in(0.01, display_err)
|
self.loop.set_alarm_in(0.01, display_err)
|
||||||
|
|
||||||
self.start()
|
super().run_loop(self.loop.run)
|
||||||
try:
|
|
||||||
self.loop.run()
|
|
||||||
except Exception:
|
|
||||||
self.loop.stop()
|
|
||||||
sys.stdout.flush()
|
|
||||||
print(traceback.format_exc(), file=sys.stderr)
|
|
||||||
print("mitmproxy has crashed!", file=sys.stderr)
|
|
||||||
print("Please lodge a bug report at:", file=sys.stderr)
|
|
||||||
print("\thttps://github.com/mitmproxy/mitmproxy", file=sys.stderr)
|
|
||||||
print("Shutting down...", file=sys.stderr)
|
|
||||||
finally:
|
|
||||||
sys.stderr.flush()
|
|
||||||
super().shutdown()
|
|
||||||
self.addons.trigger("done")
|
|
||||||
|
|
||||||
def shutdown(self):
|
|
||||||
raise urwid.ExitMainLoop
|
|
||||||
|
|
||||||
def overlay(self, widget, **kwargs):
|
def overlay(self, widget, **kwargs):
|
||||||
self.window.set_overlay(widget, **kwargs)
|
self.window.set_overlay(widget, **kwargs)
|
||||||
|
Loading…
Reference in New Issue
Block a user