Skip to content

fix(macos): close UDP ConnectionTask on redirector EOF - #326

Merged
mhils merged 4 commits into
mitmproxy:mainfrom
BetterAndBetterII:fix/macos-udp-eof-leak
Nov 14, 2026
Merged

mhils merged 4 commits into
mitmproxy:mainfrom
BetterAndBetterII:fix/macos-udp-eof-leak

Conversation

@BetterAndBetterII

Copy link
Copy Markdown
Contributor

Summary

In macOS local redirect mode, ConnectionTask::handle_udp used Some(packet) = stream.next(), so redirector EOF (None) disabled that select! arm. When a UDP flow ended before its first datagram, ConnectionEstablished was never sent, Python never sent CloseConnection, and the UnixStream leaked for the process lifetime (EMFILE under DNS-heavy traffic).

Treat stream EOF like CloseConnection: state.close(); break; so the task drops the socket.

Fixes mitmproxy/mitmproxy#8412 (UDP counterpart of the TCP leak class fixed in #260 / mitmproxy#7675).

Test plan

  • cargo test -p mitmproxy --lib packet_sources::macos::tests
    • udp_eof_before_first_datagram_completes (timed out before fix)
    • udp_eof_after_first_datagram_completes (timed out before fix)
  • macOS local-mode smoke: DNS-heavy traffic no longer climbs unix fds without bound

Some(packet) = stream.next() ignored None, so UDP flows closed before the
first datagram never got ConnectionEstablished/CloseConnection and leaked
the UnixStream forever (EMFILE under DNS-heavy local redirect).

Treat stream EOF like CloseConnection: state.close(); break.
Regression tests cover EOF before and after the first datagram.

Fixes mitmproxy/mitmproxy#8412

@mhils mhils left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the slow review, this somehow slipped through!

Comment thread src/packet_sources/mod.rs Outdated
#[cfg(target_os = "linux")]
pub mod linux;
#[cfg(target_os = "macos")]
#[cfg(any(target_os = "macos", all(unix, test)))]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why we want to run the macos tests on Linux? I'd be perfectly fine with keeping this module as macos-only, we run macos in CI after all.

@mhils

mhils commented Sep 30, 2026

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

mhils

This comment was marked as duplicate.

@mhils
mhils enabled auto-merge (squash) November 14, 2026 21:29
@mhils
mhils merged commit 8206408 into mitmproxy:main Nov 14, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macOS local mode: UDP flows closed by the redirector before their first datagram leak a file descriptor forever

2 participants