Skip to content

Fix UdpClient.EnableBroadcast not preventing broadcast sends#124482

Open
MatanTsach wants to merge 2 commits intodotnet:mainfrom
MatanTsach:fix/118055-udpclient-enable-broadcast
Open

Fix UdpClient.EnableBroadcast not preventing broadcast sends#124482
MatanTsach wants to merge 2 commits intodotnet:mainfrom
MatanTsach:fix/118055-udpclient-enable-broadcast

Conversation

@MatanTsach
Copy link

Summary

Fixes #118055

UdpClient.CheckForBroadcast() unconditionally auto-enables the Broadcast socket option when sending to IPAddress.Broadcast, even when the user has explicitly set EnableBroadcast = false. This PR makes CheckForBroadcast() respect the user's explicit choice.

Changes

  • Added _isBroadcastSetByUser field to track whether EnableBroadcast has been explicitly set by the user
  • Modified CheckForBroadcast() to skip auto-enable when the user has explicitly set EnableBroadcast
  • Backward-compatible: auto-enable behavior is preserved when users have not explicitly set the property

Note: The pre-existing _isBroadcast flag is not reset when the Client socket is replaced via the property setter. The new _isBroadcastSetByUser flag follows this same pattern for consistency. Resetting state on socket replacement could be a follow-up improvement.

Testing

  • EnableBroadcast_ExplicitlyDisabled_NotAutoEnabled — verifies that explicitly disabling broadcast prevents auto-enable on broadcast send
  • EnableBroadcast_ExplicitlyEnabledThenDisabled_NotAutoEnabled — verifies the toggle case (enable → disable) also prevents auto-enable
  • EnableBroadcast_NotExplicitlySet_AutoEnabled — verifies backward compatibility: auto-enable still works when the user hasn't set the property

MatanTsach and others added 2 commits February 16, 2026 23:31
When a user explicitly sets EnableBroadcast to false, CheckForBroadcast()
should respect that choice and not auto-enable the broadcast socket option.

Fixes dotnet#118055

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Group related broadcast fields together, following the existing
convention in UDPClient.cs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Feb 16, 2026
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Net.Sockets community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regarding the issue of the UdpClient.EnableBroadcast property

1 participant