Skip to content

Replace UnixEndPoint with UnixDomainSocketEndPoint #1160

@mguinness

Description

@mguinness

While looking into https://stackoverflow.com/questions/71742985 I noticed that OpenUnixSocketAsync() uses the UnixEndPoint class that was added with PR #119 back in 2016.

private async Task<bool> OpenUnixSocketAsync(ConnectionSettings cs, IOBehavior ioBehavior, CancellationToken cancellationToken)
{
m_logArguments[1] = cs.UnixSocket;
Log.Trace("Session{0} connecting to UNIX Socket '{1}'", m_logArguments);
var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
var unixEp = new UnixEndPoint(cs.UnixSocket!);
try
{
using (cancellationToken.Register(() => socket.Dispose()))
{
try
{
if (ioBehavior == IOBehavior.Asynchronous)
{
await Task.Factory.FromAsync(socket.BeginConnect, socket.EndConnect, unixEp, null).ConfigureAwait(false);
}
else
{
socket.Connect(unixEp);
}
}
catch (ObjectDisposedException) when (cancellationToken.IsCancellationRequested)
{
Log.Info("Session{0} connect timeout expired connecting to UNIX Socket '{1}'", m_logArguments);
throw new MySqlException(MySqlErrorCode.UnableToConnectToHost, "Connect Timeout expired.");
}
}
}
catch (SocketException)
{
socket.Dispose();
}
if (socket.Connected)
{
m_socket = socket;
m_stream = new NetworkStream(socket);
m_activityTags.Add(ActivitySourceHelper.NetTransportTagName, ActivitySourceHelper.NetTransportUnixValue);
m_activityTags.Add(ActivitySourceHelper.NetPeerNameTagName, cs.UnixSocket);
lock (m_lock)
m_state = State.Connected;
return true;
}
return false;
}

Since .NET Standard 2.1 the UnixDomainSocketEndPoint class is available which should provide the same functionality as UnixEndPoint that was copied from Mono.

It probably doesn't offer any benefits other than code maintainability, but if the code implementing Unix Domain Sockets can easily switch to the new class that would be worthwhile. The Postgres driver also made this switch in npgsql/npgsql#3314,

The question as to why the uid option is needed in the connection string for the unix socket authentication plugin to work is another issue which I asked the OP to submit.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions