diff --git a/CHANGELOG.md b/CHANGELOG.md index 0592cefe9..253169312 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ * Release tags are now prefixed with `v` again to follow SemVer convention. ([#6810](https://github.com/mitmproxy/mitmproxy/pull/6810), @mhils) +* Fix a bug where mitmproxy would not exit when `-n` is passed. + ([#6819](https://github.com/mitmproxy/mitmproxy/pull/6819), @mhils) ## 17 April 2024: mitmproxy 10.3.0 diff --git a/mitmproxy/addons/readfile.py b/mitmproxy/addons/readfile.py index ff010d41f..1a702a91f 100644 --- a/mitmproxy/addons/readfile.py +++ b/mitmproxy/addons/readfile.py @@ -71,8 +71,6 @@ class ReadFile: await self.load_flows_from_path(rfile) except exceptions.FlowReadException as e: logger.exception(f"Failed to read {ctx.options.rfile}: {e}") - finally: - self._read_task = None def running(self): if ctx.options.rfile: @@ -80,7 +78,7 @@ class ReadFile: @command.command("readfile.reading") def reading(self) -> bool: - return bool(self._read_task) + return bool(self._read_task and not self._read_task.done()) class ReadFileStdin(ReadFile): diff --git a/test/mitmproxy/addons/test_readfile.py b/test/mitmproxy/addons/test_readfile.py index 78513d586..bc1dd383a 100644 --- a/test/mitmproxy/addons/test_readfile.py +++ b/test/mitmproxy/addons/test_readfile.py @@ -54,13 +54,21 @@ class TestReadFile: tf = tmpdir.join("tfile") - with mock.patch("mitmproxy.master.Master.load_flow") as mck: - tf.write(data.getvalue()) - tctx.configure(rf, rfile=str(tf), readfile_filter=".*") - mck.assert_not_awaited() - rf.running() + load_called = asyncio.Event() + + async def load_flow(*_, **__): + load_called.set() + + tctx.master.load_flow = load_flow + + tf.write(data.getvalue()) + tctx.configure(rf, rfile=str(tf), readfile_filter=".*") + assert not load_called.is_set() + rf.running() + await load_called.wait() + + while rf.reading(): await asyncio.sleep(0) - mck.assert_awaited() tf.write(corrupt_data.getvalue()) tctx.configure(rf, rfile=str(tf))