TcpClient.Connect might throw `NullReferenceException` on macOS

Aperta
#136,744 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
csharp
Ambito
networking

Direzione di ricerca

Run the reproduction in tcpclient.cs on macOS, then trace the call through TcpClient.Connect(IPEndPoint). Compare the behavior with the reported Linux result; done means closing the client during connection produces a SocketException rather than a NullReferenceException.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

area-System.Net.Sockets untriaged
Description

When closing a TcpClient which is connecting, a NullReferenceException is thrown on macOS.

Reproduction Steps

tcpclient.cs

using System.Net;
using System.Net.Sockets;

using var tcpClient = new TcpClient();
using var cts = new CancellationTokenSource(500);
cts.Token.Register(tcpClient.Close);
tcpClient.Connect(new IPEndPoint(new IPAddress([1, 2, 3, 4]), 53));
dotnet run tcpclient.cs
Expected behavior

A SocketException is thrown.

Actual behavior

A NullReferenceException is thrown.

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
   at Program.<Main>$(String[] args) in ~/tcpclient.cs:line 7
Regression?

I don't think it's a regression, I just found about this when running the Lookup_QueryTimesOut_Tcp_Sync test from the DnsClient.NET project which is failing on macOS.

Known Workarounds

I don't know of any workaround.

Configuration
  • .NET 10.0.403
  • macOS 26.7.1
  • arm64

It's specific to macOS but probably not tied to a specific .NET version.

Other information

On Linux, running the same code throws a SocketException.

Unhandled exception. System.Net.Sockets.SocketException (88): Socket operation on non-socket [::ffff:1.2.3.4]:53
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
   at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
   at Program.<Main>$(String[] args) in ~/tcpclient.cs:line 7
Lingua principale
C#
Stelle
18.4k
Fork
5.8k
Merge medio
2g 12h
PR unite (30g)
600

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dotnet/runtime

Tutte le issue di dotnet/runtime

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.