Repository navigation
Fix SendTo to throw ArgumentException when remoteEP address family mismatches (#47772) - #135587
Sonder-Traveller wants to merge 2 commits into
Conversation
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
@dotnet-policy-service agree |
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
|
This matches the fix proposed in #47772 (comment), which is still waiting for an area owner's decision. It is a breaking change: these calls fail with a |
|
Thanks for the careful analysis — my change is indeed the same
Agreed this is a breaking change ( |
Issue
Fixes #47772. When the
remoteEPaddress family does not match the socket's address family, theReceive(Message)Fromvariants throw anArgumentException, but theSendTovariants let the underlying PAL fail instead, surfacing aSocketError.InvalidArgument(aSocketException), andSendToAsyncfails silently asynchronously. This is inconsistent.Problem
Socket.SendTo(EndPoint)andSocket.SendToAsync(SocketAsyncEventArgs)do not validate the endpoint's address family before calling into the OS PAL, unlikeReceiveFrom/ReceiveMessageFromwhich guard viaValidateReceiveFromEndpointAndState.Solution
Add a symmetric
ValidateSendToEndpointAndStateguard to theSendTofamily that rejects anendPointwhoseAddressFamilyis incompatible with the socket, throwing the sameArgumentException(SR.net_InvalidEndPointAddressFamily) that the receive variants already throw. The check reuses the existingCanTryAddressFamilylogic so it respects dual-mode sockets consistently.DnsEndPointis also rejected up front, mirroring the receive path.Behavior change
Socket.SendTonow throwsArgumentExceptioninstead of aSocketException/silent async failure when the endpoint's address family mismatches the socket. This alignsSendTowith the already-documentedReceiveFrombehavior.Tests
Added
MismatchedRemoteAddressFamily_Throws_ArgumentExceptiontoSendTo.Tests, asserting both the syncbyte[]/span overloads and the async path throwArgumentException. The pre-existing inconsistency was reproduced on a local SDK build before this change (receive →ArgumentException, send →SocketException/InvalidArgument).