Skip to content

Commit

Permalink
fix: Dont dispose IPC service on error
Browse files Browse the repository at this point in the history
  • Loading branch information
fipso authored Jan 24, 2025
1 parent bf64dc3 commit 4812948
Showing 1 changed file with 42 additions and 50 deletions.
92 changes: 42 additions & 50 deletions src/Nethermind/Nethermind.Runner/JsonRpc/JsonRpcIpcRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,57 +58,49 @@ public void Start(CancellationToken cancellationToken)
}
}

private async Task StartServer(string path, CancellationToken cancellationToken)
private async Task StartServer(string path, CancellationToken cancellationToken)
{
try
{
DeleteSocketFileIfExists(path);

_server = new(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
_server.Bind(new UnixDomainSocketEndPoint(path));
_server.Listen(0);

while (true)
{
if (_logger.IsInfo) _logger.Info("Waiting for an IPC connection...");

Socket socket = await _server.AcceptAsync(cancellationToken);

socket.ReceiveTimeout = _jsonRpcConfig.Timeout;
socket.SendTimeout = _jsonRpcConfig.Timeout;

using JsonRpcSocketsClient<IpcSocketMessageStream>? socketsClient = new(
string.Empty,
new IpcSocketMessageStream(socket),
RpcEndpoint.IPC,
_jsonRpcProcessor,
_jsonRpcLocalStats,
_jsonSerializer,
maxBatchResponseBodySize: _jsonRpcConfig.MaxBatchResponseBodySize);

await socketsClient.ReceiveLoopAsync();
}
}
catch (IOException ex) when (ex.InnerException is SocketException { SocketErrorCode: SocketError.ConnectionReset })
{
_logger.Debug("IPC client disconnected.");
}
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.ConnectionReset || ex.ErrorCode == OperationCancelledError)
{
_logger.Debug("IPC client disconnected.");
}
catch (SocketException ex)
{
if (_logger.IsError) _logger.Error($"IPC server error {ex.ErrorCode}:", ex);
}
catch (Exception ex)
{
if (_logger.IsError) _logger.Error($"IPC server error:", ex);
}
finally
{
Dispose();
}
DeleteSocketFileIfExists(path);

_server = new(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
_server.Bind(new UnixDomainSocketEndPoint(path));
_server.Listen(0);

while (true) {
try {
if (_logger.IsInfo) _logger.Info("Waiting for an IPC connection...");

Socket socket = await _server.AcceptAsync(cancellationToken);

socket.ReceiveTimeout = _jsonRpcConfig.Timeout;
socket.SendTimeout = _jsonRpcConfig.Timeout;

using JsonRpcSocketsClient < IpcSocketMessageStream > ? socketsClient = new(
string.Empty,
new IpcSocketMessageStream(socket),
RpcEndpoint.IPC,
_jsonRpcProcessor,
_jsonRpcLocalStats,
_jsonSerializer,
maxBatchResponseBodySize: _jsonRpcConfig.MaxBatchResponseBodySize);

await socketsClient.ReceiveLoopAsync();

} catch (IOException ex) when(ex.InnerException is SocketException {
SocketErrorCode: SocketError.ConnectionReset
}) {
_logger.Debug("IPC client disconnected.");
}
catch (SocketException ex) when(ex.SocketErrorCode == SocketError.ConnectionReset || ex.ErrorCode == OperationCancelledError) {
_logger.Debug("IPC client disconnected.");
}
catch (SocketException ex) {
if (_logger.IsError) _logger.Error($"IPC server error {ex.ErrorCode}:", ex);
} catch (Exception ex) {
if (_logger.IsError) _logger.Error($"IPC server error:", ex);
}
}

}

private void DeleteSocketFileIfExists(string path)
Expand Down

0 comments on commit 4812948

Please sign in to comment.