Skip to content

Commit e85a40c

Browse files
Fix event loop leak when running tests (mitmproxy#7982)
* Fix event loop leak when running tests Close event loop in `__exit__` method to avoid "OSError: [Errno 24] Too many open files" * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 7654286 commit e85a40c

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
([#7971](https://github.com/mitmproxy/mitmproxy/pull/7971), @sleeyax, @mhils)
3232
- Fix URL of mitmweb when --web-host is an IPv6 address.
3333
([#7963](https://github.com/mitmproxy/mitmproxy/pull/7963), @Julien00859)
34+
- Fix event loop leak when running tests
35+
([#7982](https://github.com/mitmproxy/mitmproxy/pull/7982), @DNEGEL3125)
3436

3537
## 15 October 2025: mitmproxy 12.2.0
3638

mitmproxy/test/taddons.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ class context:
1717
"""
1818

1919
def __init__(self, *addons, options=None, loadcore=True):
20+
self.owns_loop = False
2021
try:
2122
loop = asyncio.get_running_loop()
2223
except RuntimeError:
24+
self.owns_loop = True
2325
loop = asyncio.new_event_loop()
2426

2527
options = options or mitmproxy.options.Options()
@@ -36,6 +38,9 @@ def __enter__(self):
3638
return self
3739

3840
def __exit__(self, exc_type, exc_value, traceback):
41+
if self.owns_loop and not self.master.event_loop.is_closed():
42+
# Close the loop if we created it
43+
self.master.event_loop.close()
3944
return False
4045

4146
async def cycle(self, addon, f):

0 commit comments

Comments
 (0)