Skip to content

Commit

Permalink
unblock Unix Domain Socket on Dispose on macOS (#101753)
Browse files Browse the repository at this point in the history
* unblock Unix Domain Socket on dispose on macOS

* feedback

* feedback

* windows

* feedback
  • Loading branch information
wfurt authored May 22, 2024
1 parent acb7155 commit 258ae79
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,56 @@ public async Task ReceiveFrom_EndPoints_Correct(bool useAsync)
}
}

[ConditionalFact(typeof(Socket), nameof(Socket.OSSupportsUnixDomainSockets))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/52124", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
public async Task UnixDomainSocket_Receive_GetsCanceledByDispose()
{
string path = GetRandomNonExistingFilePath();
var endPoint = new UnixDomainSocketEndPoint(path);

using (var server = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified))
using (var client = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified))
{
server.Bind(endPoint);
server.Listen(1);

client.Connect(endPoint);
int msDelay = 100;
bool readFailed = false;
byte[] buffer = new byte[100];
using (Socket accepted = server.Accept())
{
while (msDelay < 10_000)
{
Task disposeTask = Task.Run(() =>
{
Thread.Sleep(msDelay);
client.Dispose();
});

try
{
client.Receive(buffer);
}
catch (SocketException)
{
await disposeTask;
readFailed = true;
break;
}
catch (ObjectDisposedException)
{
await disposeTask;
// Dispose happened before the operation, retry.
msDelay *= 2;
continue;
}
}
}
Assert.True(readFailed);
}
}

private static string GetRandomNonExistingFilePath()
{
string result;
Expand Down
5 changes: 5 additions & 0 deletions src/native/libs/System.Native/pal_networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -3143,6 +3143,11 @@ int32_t SystemNative_Disconnect(intptr_t socket)
#elif HAVE_DISCONNECTX
// disconnectx causes a FIN close on OSX. It's the best we can do.
err = disconnectx(fd, SAE_ASSOCID_ANY, SAE_CONNID_ANY);
if (err != 0)
{
// This happens on Unix Domain Sockets as disconnectx is only supported on AF_INET and AF_INET6
err = shutdown(fd, SHUT_RDWR);
}
#else
// best-effort, this may cause a FIN close.
err = shutdown(fd, SHUT_RDWR);
Expand Down

0 comments on commit 258ae79

Please sign in to comment.