Stop artificially rooting SocketAsyncEventArgs (dotnet/corefx#28767)
authorStephen Toub <stoub@microsoft.com>
Tue, 3 Apr 2018 16:57:29 +0000 (09:57 -0700)
committerGitHub <noreply@github.com>
Tue, 3 Apr 2018 16:57:29 +0000 (09:57 -0700)
commit4cce62862fd6a42d7b97ac48e703ebda859aadfd
tree26330150e95c00e7ae8a581efeb9a8c8f21de024
parentfcb85a56d7b1e011ff635a362b8655cbeee8cdb7
Stop artificially rooting SocketAsyncEventArgs (dotnet/corefx#28767)

SocketAsyncEventArgs creates an overlapped object that references the SAEA.  That overlapped object creates an async pinning handle in the runtime, which roots the SocketAsyncEventArgs.  The cycle from SAEA->Overlapped->handle->SAEA involving the root means that a dropped SAEA that's not Dispose'd will end up leaking.

This commit fixes that by adding a level of indirection between the handle and the SAEA.  Rather than wrapping the SAEA directly, the handle is given an intermediate object that references the SAEA, and the SAEA then stores itself as a reference into that object only while an active operation is in progress.  Once the operation completes, the reference in that object is nulled out, and the SAEA will no longer be kept alive by the pinning handle.

Commit migrated from https://github.com/dotnet/corefx/commit/4d6521c572c44c5872281c125d8e341af054eec3
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs
src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketAsyncEventArgsTest.netcoreapp.cs