From 8cf0cba1cb6e87f1cf48789e90526b75caa5436d Mon Sep 17 00:00:00 2001 From: Changsin Date: Wed, 3 Apr 2024 23:17:56 +0900 Subject: [PATCH] Setting SIG_IGN for SIGPIPE errors (#6764) * Setting SIG_IGN for SIGPIPE errors The issue was reported in https://github.com/mitmproxy/mitmproxy/issues/6744 Problem description: When there is a sudden surge of requests, mitmproxy will hit SIGPIPE (broken pipe) errors because it was trying to write to a closed socket. The stacktrace is: File "asyncio/runners.py", line 44, in run File "asyncio/base_events.py", line 636, in run_until_complete File "asyncio/base_events.py", line 603, in run_forever File "asyncio/base_events.py", line 1909, in _run_once File "asyncio/events.py", line 80, in _run File "mitmproxy/proxy/server.py", line 294, in handle_connection File "mitmproxy/proxy/server.py", line 407, in server_event File "asyncio/streams.py", line 325, in write File "asyncio/selector_events.py", line 924, in write When this happens, the process terminates unless handled The fix will allow the process to continue to run. * add changelog entry * [autofix.ci] apply automated fixes * Handling SIGPIPE only in non-Windows platforms * [autofix.ci] apply automated fixes * nit: make check platform-agnostic --------- Co-authored-by: changsin Co-authored-by: Maximilian Hils Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- CHANGELOG.md | 6 +++++- mitmproxy/tools/main.py | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d708834a7..aa70c89ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,8 +21,12 @@ ([#6747](https://github.com/mitmproxy/mitmproxy/pull/6747), @jlaine) * Fix a bug where async `client_connected` handlers would crash mitmproxy. ([#6749](https://github.com/mitmproxy/mitmproxy/pull/6749), @mhils) - * Add button to close flow details panel +* Add button to close flow details panel ([#6734](https://github.com/mitmproxy/mitmproxy/pull/6734), @lups2000) +* Ignore SIGPIPE signals when there is lots of traffic. + Socket errors are handled directly and do not require extra signals + that generate noise. + ([#6764](https://github.com/mitmproxy/mitmproxy/pull/6764), @changsin) * Add primitive websocket interception and modification ([#6766](https://github.com/mitmproxy/mitmproxy/pull/6766), @errorxyz) diff --git a/mitmproxy/tools/main.py b/mitmproxy/tools/main.py index cc3c9ddee..bc62362da 100644 --- a/mitmproxy/tools/main.py +++ b/mitmproxy/tools/main.py @@ -120,6 +120,10 @@ def run( # but signal.signal just works fine for our purposes. signal.signal(signal.SIGINT, _sigint) signal.signal(signal.SIGTERM, _sigterm) + # to fix the issue mentioned https://github.com/mitmproxy/mitmproxy/issues/6744 + # by setting SIGPIPE to SIG_IGN, the process will not terminate and continue to run + if hasattr(signal, "SIGPIPE"): + signal.signal(signal.SIGPIPE, signal.SIG_IGN) await master.run() return master