Skip to content

Commit 67e07a1

Browse files
committed
Avoid running out of FDs in tests on MacOS
1 parent 2138d3b commit 67e07a1

6 files changed

Lines changed: 25 additions & 4 deletions

File tree

hippolyzer/apps/proxy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def start_proxy(session_manager: SessionManager, extra_addons: Optional[list] =
128128
if proxy_host is None:
129129
proxy_host = session_manager.settings.PROXY_BIND_ADDR
130130

131+
session_manager.flow_context = HTTPFlowContext()
131132
flow_context = session_manager.flow_context
132133
session_manager.name_cache.load_viewer_caches()
133134

hippolyzer/lib/proxy/http_flow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def from_state(cls, flow_state: Dict, session_manager: Optional[SessionManager])
140140
assert flow is not None
141141
cap_data_ser = flow.metadata.get("cap_data_ser")
142142
callback_queue = None
143-
if session_manager:
143+
if session_manager and session_manager.flow_context:
144144
callback_queue = session_manager.flow_context.to_proxy_queue
145145
if cap_data_ser is not None:
146146
flow.metadata["cap_data"] = CapData.deserialize(cap_data_ser, session_manager)

hippolyzer/lib/proxy/http_proxy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ def __init__(self):
102102
self.shutdown_signal = multiprocessing.Event()
103103
self.mitmproxy_ready = multiprocessing.Event()
104104

105+
def close(self):
106+
self.shutdown_signal.set()
107+
self.from_proxy_queue.close()
108+
self.to_proxy_queue.close()
109+
105110

106111
class IPCInterceptionAddon:
107112
"""

hippolyzer/lib/proxy/sessions.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import datetime
66
import functools
77
import logging
8-
import multiprocessing
98
from typing import *
109
from weakref import ref
1110

@@ -121,14 +120,17 @@ def __init__(self, settings: ProxySettings):
121120
BaseClientSessionManager.__init__(self)
122121
self.settings: ProxySettings = settings
123122
self.sessions: List[Session] = []
124-
self.shutdown_signal = multiprocessing.Event()
125-
self.flow_context = HTTPFlowContext()
123+
self.flow_context: HTTPFlowContext | None = None
126124
self.asset_repo = HTTPAssetRepo()
127125
self.message_logger: Optional[BaseMessageLogger] = None
128126
self.addon_ctx: Dict[str, Dict[str, Any]] = collections.defaultdict(dict)
129127
self.name_cache = ProxyNameCache()
130128
self.pending_leap_clients: List[LEAPClient] = []
131129

130+
def close(self):
131+
if self.flow_context is not None:
132+
self.flow_context.close()
133+
132134
def create_session(self, login_data) -> Session:
133135
session = Session.from_login_data(login_data, self)
134136
self.name_cache.create_subscriptions(

hippolyzer/lib/proxy/test_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ async def asyncSetUp(self) -> None:
3939

4040
def tearDown(self) -> None:
4141
self.protocol.close()
42+
self.session_manager.close()
4243

4344
async def _wait_drained(self):
4445
await asyncio.sleep(0.001)

tests/proxy/integration/test_http.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from hippolyzer.lib.proxy.addon_utils import BaseAddon
1717
from hippolyzer.lib.proxy.addons import AddonManager
1818
from hippolyzer.lib.proxy.http_event_manager import MITMProxyEventManager
19+
from hippolyzer.lib.proxy.http_proxy import HTTPFlowContext
1920
from hippolyzer.lib.proxy.http_flow import HippoHTTPFlow
2021
from hippolyzer.lib.proxy.caps import SerializedCapData
2122
from hippolyzer.lib.proxy.sessions import SessionManager
@@ -35,6 +36,7 @@ async def asyncSetUp(self) -> None:
3536
await super().asyncSetUp()
3637
self.addon = MockAddon()
3738
AddonManager.init([], self.session_manager, [self.addon])
39+
self.session_manager.flow_context = HTTPFlowContext()
3840
self.flow_context = self.session_manager.flow_context
3941
self.http_event_manager = MITMProxyEventManager(self.session_manager, self.flow_context)
4042
self._setup_default_circuit()
@@ -148,6 +150,7 @@ async def asyncSetUp(self) -> None:
148150
self.caps_client = self.session.main_region.caps_client
149151
proxy_port = 9905
150152
self.session_manager.settings.HTTP_PROXY_PORT = proxy_port
153+
self.session_manager.flow_context = HTTPFlowContext()
151154

152155
self.http_proc = multiprocessing.Process(
153156
target=run_http_proxy_process,
@@ -162,6 +165,15 @@ async def asyncSetUp(self) -> None:
162165
self.session_manager.flow_context
163166
)
164167

168+
def tearDown(self) -> None:
169+
self.session_manager.flow_context.shutdown_signal.set()
170+
self.http_proc.join(timeout=2.0)
171+
if self.http_proc.is_alive():
172+
self.http_proc.terminate()
173+
self.http_proc.join(timeout=1.0)
174+
self.http_proc.close()
175+
super().tearDown()
176+
165177
def test_mitmproxy_works(self):
166178
async def _request_example_com():
167179
# Pump callbacks from mitmproxy

0 commit comments

Comments
 (0)