| 52 | mitmproxy_ctx.options = self.options |
| 53 | |
| 54 | async def run(self) -> None: |
| 55 | with ( |
| 56 | asyncio_utils.install_exception_handler(self._asyncio_exception_handler), |
| 57 | asyncio_utils.set_eager_task_factory(), |
| 58 | ): |
| 59 | self.should_exit.clear() |
| 60 | |
| 61 | # Can we exit before even bringing up servers? |
| 62 | if ec := self.addons.get("errorcheck"): |
| 63 | await ec.shutdown_if_errored() |
| 64 | if ps := self.addons.get("proxyserver"): |
| 65 | # This may block for some proxy modes, so we also monitor should_exit. |
| 66 | await asyncio.wait( |
| 67 | [ |
| 68 | asyncio_utils.create_task( |
| 69 | ps.setup_servers(), name="setup_servers", keep_ref=False |
| 70 | ), |
| 71 | asyncio_utils.create_task( |
| 72 | self.should_exit.wait(), name="should_exit", keep_ref=False |
| 73 | ), |
| 74 | ], |
| 75 | return_when=asyncio.FIRST_COMPLETED, |
| 76 | ) |
| 77 | if self.should_exit.is_set(): |
| 78 | return |
| 79 | # Did bringing up servers fail? |
| 80 | if ec := self.addons.get("errorcheck"): |
| 81 | await ec.shutdown_if_errored() |
| 82 | |
| 83 | try: |
| 84 | await self.running() |
| 85 | # Any errors in the final part of startup? |
| 86 | if ec := self.addons.get("errorcheck"): |
| 87 | await ec.shutdown_if_errored() |
| 88 | ec.finish() |
| 89 | |
| 90 | await self.should_exit.wait() |
| 91 | finally: |
| 92 | # if running() was called, we also always want to call done(). |
| 93 | # .wait might be cancelled (e.g. by sys.exit), so this needs to be in a finally block. |
| 94 | await self.done() |
| 95 | |
| 96 | def shutdown(self): |
| 97 | """ |