Skip to content

Commit

Permalink
Fix program exit when n is passed (#6819)
Browse files Browse the repository at this point in the history
* fix program exit when `n` is passed

fix #6818

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
mhils and autofix-ci[bot] committed Apr 24, 2024
1 parent b2298d7 commit b5574fb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions mitmproxy/addons/readfile.py
Expand Up @@ -71,16 +71,14 @@ async def doread(self, rfile: str) -> None:
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:
self._read_task = asyncio.create_task(self.doread(ctx.options.rfile))

@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):
Expand Down
20 changes: 14 additions & 6 deletions test/mitmproxy/addons/test_readfile.py
Expand Up @@ -54,13 +54,21 @@ async def test_read(self, tmpdir, data, corrupt_data, caplog_async):

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))
Expand Down

0 comments on commit b5574fb

Please sign in to comment.