Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove System.Net.Connections and related features #41648

Merged
merged 4 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/libraries/NetCoreAppLibrary.props
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
System.IO.FileSystem.Watcher;
System.IO.IsolatedStorage;
System.IO.MemoryMappedFiles;
System.IO.Pipelines;
Copy link
Member

Choose a reason for hiding this comment

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

Is removing pipelines reference here going to require a reaction in aspnetcore? Did we make changes to not reference it ourselves? @halter73 @Tratcher @JunTaoLuo

System.IO.Pipes;
System.IO.Pipes.AccessControl;
System.IO.UnmanagedMemoryStream;
Expand All @@ -57,7 +56,6 @@
System.Linq.Parallel;
System.Linq.Queryable;
System.Memory;
System.Net.Connections;
System.Net.Http;
System.Net.Http.Json;
System.Net.HttpListener;
Expand Down
21 changes: 21 additions & 0 deletions src/libraries/System.Net.Connections/ref/System.Net.Connections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,24 @@ public SocketsConnectionFactory(System.Net.Sockets.SocketType socketType, System
protected virtual System.Net.Sockets.Socket CreateSocket(System.Net.Sockets.AddressFamily addressFamily, System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType, System.Net.EndPoint? endPoint, System.Net.Connections.IConnectionProperties? options) { throw null; }
}
}
namespace System.Net
Copy link
Member

Choose a reason for hiding this comment

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

How does it work if we remove System.Net.Connections from shared framework but still depend on it? Or is it still present? Do we still expose it to the user?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not exposed to the user. It's still part of the tree, but not dropped.

{
public enum NetworkError : int
{
Other = 0,
EndPointInUse,
HostNotFound,
TimedOut,
ConnectionRefused,
OperationAborted,
ConnectionAborted,
ConnectionReset,
}
public class NetworkException : System.IO.IOException
{
public NetworkException(NetworkError error, Exception? innerException = null) { }
public NetworkException(string message, NetworkError error, Exception? innerException = null) { }
protected NetworkException(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext) { }
public NetworkError NetworkError { get { throw null; } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<ItemGroup>
<Compile Include="$(CommonPath)System\Net\NetworkErrorHelper.cs" Link="Common\System\Net\NetworkErrorHelper.cs" />
<Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs" Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="System\Net\NetworkError.cs" />
<Compile Include="System\Net\NetworkException.cs" />
<Compile Include="System\Net\Connections\ConnectionBase.cs" />
<Compile Include="System\Net\Connections\ConnectionCloseMethod.cs" />
<Compile Include="System\Net\Connections\ConnectionExtensions.cs" />
Expand All @@ -20,13 +22,13 @@
<Compile Include="System\Net\Connections\Sockets\SocketsConnectionFactory.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\System.IO.Pipelines\ref\System.IO.Pipelines.csproj" />
<Reference Include="System.Runtime" />
<Reference Include="System.Memory" />
<Reference Include="System.Net.Primitives" />
<Reference Include="System.Net.Sockets" />
<Reference Include="System.Threading" />
<Reference Include="System.Threading.Tasks" />
<Reference Include="System.IO.Pipelines" />
<Reference Include="Microsoft.Win32.Primitives" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,7 @@ public override void GetObjectData(SerializationInfo serializationInfo, Streamin
/// <summary>Returns the specific kind of error.</summary>
public NetworkError NetworkError { get; }

private static string GetExceptionMessage(NetworkError error) => error switch
{
NetworkError.EndPointInUse => SR.networkerror_addressinuse,
NetworkError.TimedOut => SR.networkerror_timedout,
NetworkError.HostNotFound => SR.networkerror_hostnotfound,
NetworkError.ConnectionRefused => SR.networkerror_connectionrefused,
NetworkError.ConnectionAborted => SR.networkerror_connectionaborted,
NetworkError.ConnectionReset => SR.networkerror_connectionreset,
NetworkError.OperationAborted => SR.networkerror_operationaborted,
_ => SR.networkerror_other
};
// TODO: Better exception messages
safern marked this conversation as resolved.
Show resolved Hide resolved
private static string GetExceptionMessage(NetworkError error) => $"A network error occurred: {error}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,36 +225,6 @@ public async Task Connection_Pipe_ReadWrite_Success(EndPoint endPoint, SocketTyp
Assert.True(rr.Buffer.FirstSpan.SequenceEqual(sendData));
}

[Fact]
public async Task Connection_Stream_FailingOperation_ThowsNetworkException()
{
using var server = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, IPAddress.Loopback);
using SocketsConnectionFactory factory = new SocketsConnectionFactory(SocketType.Stream, ProtocolType.Tcp);
using Connection connection = await factory.ConnectAsync(server.EndPoint);

connection.ConnectionProperties.TryGet(out Socket socket);
Stream stream = connection.Stream;
socket.Dispose();

Assert.Throws<NetworkException>(() => stream.Read(new byte[1], 0, 1));
Assert.Throws<NetworkException>(() => stream.Write(new byte[1], 0, 1));
}

[Fact]
public async Task Connection_Pipe_FailingOperation_ThowsNetworkException()
{
using var server = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, IPAddress.Loopback);
using SocketsConnectionFactory factory = new SocketsConnectionFactory(SocketType.Stream, ProtocolType.Tcp);
using Connection connection = await factory.ConnectAsync(server.EndPoint);

connection.ConnectionProperties.TryGet(out Socket socket);
IDuplexPipe pipe = connection.Pipe;
socket.Dispose();

await Assert.ThrowsAsync<NetworkException>(() => pipe.Input.ReadAsync().AsTask());
await Assert.ThrowsAsync<NetworkException>(() => pipe.Output.WriteAsync(new byte[1]).AsTask());
}
Comment on lines -229 to -256
Copy link
Member

Choose a reason for hiding this comment

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

If we are keeping the rest of the code anyways, can we preserve these tests by refactoring the assertions for IOException? I would prefer not to loose them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I can tell, the only reason these tests exist is to check for NetworkException specifically. So I'm not sure what we get by checking for IOException.

Copy link
Member

Choose a reason for hiding this comment

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

I agree that the assertion is less valuable in it's current form, but is it more likely that we are about to re-introduce NetworkException eventually? In that case keeping them around will ensure that we don't loose them by accident.

Copy link
Contributor

Choose a reason for hiding this comment

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

Depending on the direction we go, we will make a branch from master that reverts this PR and continue to iterate the API --so, I don't think we will lose anything.


[Theory]
[InlineData(false, false)]
[InlineData(false, true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@
<Compile Include="$(CommonTestPath)System\Net\Sockets\SocketImplementationType.cs" Link="SocketCommon\SocketImplementationType.cs" />
<Compile Include="$(CommonTestPath)System\Threading\Tasks\TaskTimeoutExtensions.cs" Link="Common\System\Threading\Tasks\TaskTimeoutExtensions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\System.IO.Pipelines\src\System.IO.Pipelines.csproj" />
<ProjectReference Include="..\..\..\System.Net.Connections\src\System.Net.Connections.csproj" />
</ItemGroup>

</Project>
2 changes: 0 additions & 2 deletions src/libraries/System.Net.Http/ref/System.Net.Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ public SocketsHttpHandler() { }
public static bool IsSupported { get { throw null; } }
public bool AllowAutoRedirect { get { throw null; } set { } }
public System.Net.DecompressionMethods AutomaticDecompression { get { throw null; } set { } }
public System.Net.Connections.ConnectionFactory? ConnectionFactory { get { throw null; } set { } }
public System.TimeSpan ConnectTimeout { get { throw null; } set { } }
[System.Diagnostics.CodeAnalysis.AllowNullAttribute]
public System.Net.CookieContainer CookieContainer { get { throw null; } set { } }
Expand All @@ -345,7 +344,6 @@ public SocketsHttpHandler() { }
public int MaxConnectionsPerServer { get { throw null; } set { } }
public int MaxResponseDrainSize { get { throw null; } set { } }
public int MaxResponseHeadersLength { get { throw null; } set { } }
public System.Func<System.Net.Http.HttpRequestMessage, System.Net.Connections.Connection, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask<System.Net.Connections.Connection>>? PlaintextFilter { get { throw null; } set { } }
public System.TimeSpan PooledConnectionIdleTimeout { get { throw null; } set { } }
public System.TimeSpan PooledConnectionLifetime { get { throw null; } set { } }
public bool PreAuthenticate { get { throw null; } set { } }
Expand Down
1 change: 0 additions & 1 deletion src/libraries/System.Net.Http/ref/System.Net.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" />
<ProjectReference Include="..\..\System.Net.Primitives\ref\System.Net.Primitives.csproj" />
<ProjectReference Include="..\..\System.Net.Sockets\ref\System.Net.Sockets.csproj" />
<ProjectReference Include="..\..\System.Net.Connections\ref\System.Net.Connections.csproj" />
<ProjectReference Include="..\..\System.Net.Security\ref\System.Net.Security.csproj" />
<ProjectReference Include="..\..\System.Security.Cryptography.X509Certificates\ref\System.Security.Cryptography.X509Certificates.csproj" />
<ProjectReference Include="..\..\System.Text.Encoding\ref\System.Text.Encoding.csproj" />
Expand Down
6 changes: 1 addition & 5 deletions src/libraries/System.Net.Http/src/System.Net.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@
Link="Common\System\IO\StreamHelpers.CopyValidation.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SslClientAuthenticationOptionsExtensions.cs"
Link="Common\System\Net\Security\SslClientAuthenticationOptionsExtensions.cs" />
<Compile Include="$(CommonPath)System\Net\NetworkErrorHelper.cs"
Link="Common\System\Net\NetworkErrorHelper.cs" />
<Compile Include="$(CommonPath)System\IO\DelegatingStream.cs"
Link="Common\System\IO\DelegatingStream.cs" />
<Compile Include="$(CommonPath)System\IO\ReadOnlyMemoryStream.cs"
Expand Down Expand Up @@ -175,9 +173,9 @@
<Compile Include="System\Net\Http\SocketsHttpHandler\MultiProxy.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\RawConnectionStream.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\RedirectHandler.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\SocketsConnectionFactory.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\SocketsHttpHandler.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\SystemProxyInfo.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\DnsEndPointWithProperties.cs" />
<Compile Include="$(CommonPath)System\Net\NTAuthentication.Common.cs"
Link="Common\System\Net\NTAuthentication.Common.cs" />
<Compile Include="$(CommonPath)System\Net\ContextFlagsPal.cs"
Expand Down Expand Up @@ -686,9 +684,7 @@
<Reference Include="System.Diagnostics.DiagnosticSource" />
<Reference Include="System.Diagnostics.Tracing" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Pipelines" />
<Reference Include="System.Memory" />
<Reference Include="System.Net.Connections" />
<Reference Include="System.Net.NameResolution" />
<Reference Include="System.Net.NetworkInformation" />
<Reference Include="System.Net.Primitives" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics.CodeAnalysis;
using System.Net.Connections;

namespace System.Net.Http
{
Expand Down Expand Up @@ -149,18 +148,6 @@ public HttpKeepAlivePingPolicy KeepAlivePingPolicy
set => throw new PlatformNotSupportedException();
}

public ConnectionFactory? ConnectionFactory
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public Func<HttpRequestMessage, Connection, CancellationToken, ValueTask<Connection>>? PlaintextFilter
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public IDictionary<string, object?> Properties => throw new PlatformNotSupportedException();

public HeaderEncodingSelector<HttpRequestMessage>? RequestHeaderEncodingSelector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@

using System.Diagnostics;
using System.IO;
using System.IO.Pipelines;
using System.Net.Connections;
using System.Net.Quic;
using System.Net.Security;
using System.Net.Sockets;
using System.Runtime.ExceptionServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -34,11 +31,11 @@ public CertificateCallbackMapper(Func<HttpRequestMessage, X509Certificate2?, X50
}
}

public static async ValueTask<Connection> ConnectAsync(ConnectionFactory factory, DnsEndPoint endPoint, IConnectionProperties? options, CancellationToken cancellationToken)
public static async ValueTask<Stream> ConnectAsync(SocketsConnectionFactory factory, DnsEndPoint endPoint, CancellationToken cancellationToken)
{
try
{
return await factory.ConnectAsync(endPoint, options, cancellationToken).ConfigureAwait(false);
return await factory.ConnectAsync(endPoint, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException ex) when (ex.CancellationToken == cancellationToken)
{
Expand All @@ -50,7 +47,7 @@ public static async ValueTask<Connection> ConnectAsync(ConnectionFactory factory
}
}

public static Connection Connect(string host, int port, CancellationToken cancellationToken)
public static Stream Connect(string host, int port, CancellationToken cancellationToken)
{
// For synchronous connections, we can just create a socket and make the connection.
cancellationToken.ThrowIfCancellationRequested();
Expand All @@ -63,17 +60,7 @@ public static Connection Connect(string host, int port, CancellationToken cancel
socket.Connect(new DnsEndPoint(host, port));
}

// Since we only do GracefulShutdown in SocketsHttpHandler code, Connection.FromStream() should match SocketConnection's behavior:
return Connection.FromStream(new NetworkStream(socket, ownsSocket: true), localEndPoint: socket.LocalEndPoint, remoteEndPoint: socket.RemoteEndPoint);
}
catch (SocketException se)
{
socket.Dispose();

// SocketConnectionFactory wraps SocketException in NetworkException. Do the same here.
NetworkException ne = NetworkErrorHelper.MapSocketException(se);

throw CreateWrappedException(ne, host, port, cancellationToken);
return new NetworkStream(socket, ownsSocket: true);
}
catch (Exception e)
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net.Connections;
using System.Net.Http.Headers;
using System.Net.Http.HPack;
using System.Net.Security;
Expand All @@ -24,7 +23,6 @@ internal sealed partial class Http2Connection : HttpConnectionBase, IDisposable
{
private readonly HttpConnectionPool _pool;
private readonly Stream _stream;
private readonly Connection _connection;

// NOTE: These are mutable structs; do not make these readonly.
private ArrayBuffer _incomingBuffer;
Expand Down Expand Up @@ -119,11 +117,10 @@ internal enum KeepAliveState
private long _keepAlivePingTimeoutTimestamp;
private volatile KeepAliveState _keepAliveState;

public Http2Connection(HttpConnectionPool pool, Connection connection)
public Http2Connection(HttpConnectionPool pool, Stream stream)
{
_pool = pool;
_stream = connection.Stream;
_connection = connection;
_stream = stream;
_incomingBuffer = new ArrayBuffer(InitialConnectionBufferSize);
_outgoingBuffer = new ArrayBuffer(InitialConnectionBufferSize);

Expand Down Expand Up @@ -1698,7 +1695,7 @@ private void CheckForShutdown()
GC.SuppressFinalize(this);

// Do shutdown.
_connection.Dispose();
_stream.Dispose();

_connectionWindow.Dispose();
_concurrentStreams.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Net.Http.Headers;
using System.Net.Security;
using System.Net.Sockets;
using System.Net.Connections;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -46,7 +45,6 @@ internal partial class HttpConnection : HttpConnectionBase, IDisposable
private readonly HttpConnectionPool _pool;
private readonly Socket? _socket; // used for polling; _stream should be used for all reading/writing. _stream owns disposal.
private readonly Stream _stream;
private readonly Connection _connection;
private readonly TransportContext? _transportContext;
private readonly WeakReference<HttpConnection> _weakThisRef;

Expand All @@ -73,16 +71,19 @@ internal partial class HttpConnection : HttpConnectionBase, IDisposable

public HttpConnection(
HttpConnectionPool pool,
Connection connection,
Stream stream,
TransportContext? transportContext)
{
Debug.Assert(pool != null);
Debug.Assert(connection != null);
Debug.Assert(stream != null);

_pool = pool;
connection.ConnectionProperties.TryGet(out _socket); // may be null in cases where we couldn't easily get the underlying socket
_stream = connection.Stream;
_connection = connection;
_stream = stream;
if (stream is NetworkStream networkStream)
{
_socket = networkStream.Socket;
}

_transportContext = transportContext;

_writeBuffer = new byte[InitialWriteBufferSize];
Expand Down Expand Up @@ -125,7 +126,7 @@ protected void Dispose(bool disposing)
if (disposing)
{
GC.SuppressFinalize(this);
_connection.Dispose();
_stream.Dispose();

// Eat any exceptions from the read-ahead task. We don't need to log, as we expect
// failures from this task due to closing the connection while a read is in progress.
Expand Down
Loading