diff --git a/CHANGELOG.md b/CHANGELOG.md index 435dcf9f1..a07653b40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ * Remove workarounds for old pyOpenSSL versions (#4831, @KarlParkinson) * Add fonts to asset filter (~a) (#4928, @elespike) * Fix bug that crashed when using `view.flows.resolve` (#4916, @rbdixon) +* Fix a bug where `running()` is invoked twice on startup (#3584, @mhils) ## 28 September 2021: mitmproxy 7.0.4 diff --git a/mitmproxy/addons/script.py b/mitmproxy/addons/script.py index 4e146d67d..5907476c7 100644 --- a/mitmproxy/addons/script.py +++ b/mitmproxy/addons/script.py @@ -75,6 +75,7 @@ class Script: path.strip("'\" ") ) self.ns = None + self.is_running = False if not os.path.isfile(self.fullpath): raise exceptions.OptionsError('No such script') @@ -85,6 +86,9 @@ class Script: else: self.loadscript() + def running(self): + self.is_running = True + def done(self): if self.reloadtask: self.reloadtask.cancel() @@ -105,7 +109,8 @@ class Script: if self.ns: # We're already running, so we have to explicitly register and # configure the addon - ctx.master.addons.invoke_addon(self.ns, hooks.RunningHook()) + if self.is_running: + ctx.master.addons.invoke_addon(self.ns, hooks.RunningHook()) try: ctx.master.addons.invoke_addon( self.ns, diff --git a/test/mitmproxy/addons/test_script.py b/test/mitmproxy/addons/test_script.py index 12ee070c6..f815f1655 100644 --- a/test/mitmproxy/addons/test_script.py +++ b/test/mitmproxy/addons/test_script.py @@ -1,5 +1,6 @@ import asyncio import os +import re import sys import traceback @@ -11,6 +12,7 @@ from mitmproxy.addons import script from mitmproxy.proxy.layers.http import HttpRequestHook from mitmproxy.test import taddons from mitmproxy.test import tflow +from mitmproxy.tools import main # We want this to be speedy for testing @@ -90,7 +92,7 @@ class TestScript: ) with taddons.context(sc) as tctx: tctx.configure(sc) - await tctx.master.await_log("recorder running") + await tctx.master.await_log("recorder configure") rec = tctx.master.addons.get("recorder") assert rec.call_log[0][0:2] == ("recorder", "load") @@ -128,7 +130,7 @@ class TestScript: True, ) tctx.master.addons.add(sc) - await tctx.master.await_log("error running") + await tctx.master.await_log("error load") tctx.configure(sc) f = tflow.tflow(resp=True) @@ -331,3 +333,21 @@ class TestScriptLoader: 'e running', 'e configure', ] + + +def test_order(event_loop, tdata, capsys): + """Integration test: Make sure that the runtime hooks are triggered on startup in the correct order.""" + asyncio.set_event_loop(event_loop) + main.mitmdump([ + "-n", + "-s", tdata.path("mitmproxy/data/addonscripts/recorder/recorder.py"), + "-s", tdata.path("mitmproxy/data/addonscripts/shutdown.py"), + ]) + assert re.match( + r"Loading script.+recorder.py\n" + r"\('recorder', 'load', .+\n" + r"\('recorder', 'configure', .+\n" + r"Loading script.+shutdown.py\n" + r"\('recorder', 'running', .+\n$", + capsys.readouterr().out, + ) diff --git a/test/mitmproxy/data/addonscripts/error.py b/test/mitmproxy/data/addonscripts/error.py index 2f0c1755c..8dcd404d8 100644 --- a/test/mitmproxy/data/addonscripts/error.py +++ b/test/mitmproxy/data/addonscripts/error.py @@ -1,8 +1,8 @@ from mitmproxy import ctx -def running(): - ctx.log.info("error running") +def load(loader): + ctx.log.info("error load") def request(flow):