dotnet/runtime

UnixDomainSocketEndPoint no longer constructable on iOS (.NET 8)

Open

#96,143 opened on Dec 18, 2023

View on GitHub
 (6 comments) (0 reactions) (0 assignees)C# (5,445 forks)batch import
area-System.Netgood first issueos-ios

Repository metrics

Stars
 (17,886 stars)
PR merge metrics
 (Avg merge 12d 11h) (661 merged PRs in 30d)

Description

Description

I work on an iOS app (currently Xamarin iOS/.NET 4.8) that uses unix domain sockets. Trying to port to .NET 8, I noticed that the constructor of UnixDomainSocketEndPoint now throws a PlatformNotSupportedException.

Reproduction Steps

            var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
            fullPath = Path.Join(
                Environment.GetFolderPath(Environment.SpecialFolder.Personal),
                "my.sock");

            if (File.Exists(fullPath))
                File.Delete(fullPath);

            var endpoint = new UnixDomainSocketEndPoint(fullPath);  // throws PlatformNotSupportedException on .NET 8
            socket.Bind(endpoint);
            socket.Listen(10);

Expected behavior

Expected to get an instance of UnixDomainSocketEndPoint (as with the older framework)

Actual behavior

Thrown PlatformNotSupportedException

Regression?

Works on .NET 4.8/Xamarin.iOS

Known Workarounds

Pull a copy of the UnixDomainSocketEndPoint code into the project, then comment out the platform check (along with some other minor changes to make it compile) Verified that this works within my iOS app.

Configuration

net8.0-ios

Other information

SocketProtocolSupportPal.Unix.cs contains a comment about AF_UNIX sockets not working on iOS, then explicitly denies support:

    internal static partial class SocketProtocolSupportPal
    {
        private const int DgramSocketType = 2;

        private static unsafe bool IsSupported(AddressFamily af)
        {
            // Check for AF_UNIX on iOS/tvOS. The OS claims to support this, but returns EPERM on bind.
            // We should explicitly set the return here to false, to avoid giving a false impression.
            if (af == AddressFamily.Unix && (OperatingSystem.IsTvOS() || (OperatingSystem.IsIOS() && !OperatingSystem.IsMacCatalyst())))
            {
                return false;
            }

That claim is not consistent with my experience.

Contributor guide