c# - TCP Socket 5 分钟后 CPU/内存使用率高
问题描述
我正在构建一个接受传入 TCP 连接的服务器应用程序。(大约 300 个独特的客户)。重要的是要注意我无法控制客户。
我发现一些正在连接的客户端在建立初始连接并发送第一个状态更新后保持空闲相当长的一段时间。当它们保持空闲超过 5 分钟时,应用程序的 CPU 使用率会跃升至 90% 以上并保持在那里。
为了解决这个问题,我构建了一个 4 分钟后触发的取消令牌。这使我可以终止连接。然后客户端检测到这一点并在大约一分钟后重新连接。这解决了高 CPU 使用率的问题,但有高内存使用率的副作用,似乎存在内存泄漏。我怀疑资源被前一个套接字对象持有。
我有一个客户端对象,其中包含套接字连接和有关已连接客户端的信息。它还管理传入的消息。还有一个接受传入连接的管理器类。然后它创建客户端对象,为其分配套接字并将客户端对象添加到并发字典中。每隔 10 秒,它会检查字典中是否存在已设置为 _closeConnection = true 的客户端并调用它们的 dispose 方法。
这是一些客户端对象代码:
public void StartCommunication()
{
Task.Run(async () =>
{
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[75]);
while (IsConnected)
{
try
{
// This is where I suspect the memory leak is originating - this call I suspect is not properly cleaned up when the object is diposed
var result = await SocketTaskExtensions.ReceiveAsync(ClientConnection.Client, buffer, SocketFlags.None).WithCancellation(cts.Token);
if (result > 0)
{
var message = new ClientMessage(buffer.Array, true);
if(message.IsValid)
HandleClientMessage(message);
}
}
catch (OperationCanceledException)
{
_closeConnection = true;
DisconnectReason = "Client has not reported in 4 mins";
}
catch (Exception e)
{
_closeConnection = true;
DisconnectReason = "Error during receive opperation";
}
}
});
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_closeConnection = true;
cts.Cancel();
// Explicitly kill the underlying socket
if (UnitConnection.Client != null)
{
UnitConnection.Client.Close();
}
UnitConnection.Close();
cts.Dispose();
}
}
任务扩展方法:
public static async Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<bool>();
using (cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
{
if (task != await Task.WhenAny(task, tcs.Task))
{
throw new OperationCanceledException(cancellationToken);
}
}
return task.Result;
}
经理代码:
public bool StartListener()
{
_listener = new TcpListenerEx(IPAddress.Any, Convert.ToInt32(_serverPort));
_listener.Start();
Task.Run(async () =>
{
while (_maintainConnection) // <--- boolean flag to exit loop
{
try
{
HandleClientConnection(await _listener.AcceptTcpClientAsync());
}
catch (Exception e)
{
//<snip>
}
}
});
return true;
}
private void HandleClientConnection(TcpClient client)
{
Task.Run(async () =>
{
try
{
// Create new Coms object
var client = new ClientComsAsync();
client.ClientConnection = client;
// Start client communication
client.StartCommunication();
//_clients is the ConcurrentDictionary
ClientComsAsync existingClient;
if (_clients.TryGetValue(client.ClientName, out existingClient) && existingClient != null)
{
if (existingClient.IsConnected)
existingClient.SendHeatbeat();
if (!existingClient.IsConnected)
{
// Call Dispose on existing client
CleanUpClient(existingClient, "Reconnected with new connection");
}
}
}
catch (Exception e)
{
//<snip>
}
finally
{
//<snip>
}
});
}
private void CleanUpClient(ClientComsAsync client, string reason)
{
ClientComsAsync _client;
_units.TryRemove(client.ClientName, out _client);
if (_client != null)
{
_client.Dispose();
}
}
解决方案
当它们保持空闲超过 5 分钟时,应用程序的 CPU 使用率会跃升至 90% 以上并保持在那里。
为了解决这个问题,我构建了一个 4 分钟后触发的取消令牌。
正确的反应是解决高 CPU 使用率问题。
在我看来它在这里:
while (IsConnected)
{
try
{
var result = await SocketTaskExtensions.ReceiveAsync(ClientConnection.Client, buffer, SocketFlags.None);
if (result > 0)
{
...
}
}
catch ...
{
...
}
}
套接字很奇怪,要正确处理原始 TCP/IP 套接字非常困难。顺便说一句,我总是鼓励开发人员使用更标准的东西,比如 HTTP 或 WebSockets,但在这种情况下,你不能控制客户端,所以这不是一个选择。
具体来说,您的代码没有处理result == 0
. 如果客户端设备优雅地关闭了它们的套接字,您会看到 a result
of 0
,立即循环并继续获得 a result
of 0
- 一个占用 CPU 的紧密循环。
当然,这是假设IsConnected
仍然存在true
。而这或许是可能的……
您没有显示IsConnected
代码中设置的位置,但我怀疑它在发送心跳消息后的错误处理中。因此,这就是为什么这可能无法按预期工作的原因...我怀疑客户端设备正在关闭其发送流(您的接收流),同时保持其接收流(您的发送流)打开。这是关闭套接字的一种方法,有时被认为“更有礼貌”,因为它允许另一端继续发送数据,即使该端已完成发送。(这是从客户端设备的角度来看,因此“另一端”是您的代码,“这一端”是客户端设备)。
这在套接字方面是完全合法的,因为每个连接的套接字都是两个流,而不是一个,每个流都可以独立关闭。如果发生这种情况,您的心跳仍将无错误地发送和接收(并且可能只是被客户端设备静默丢弃),IsConnected
将保持true
,并且读取循环将变得同步并占用您的 CPU。
要解决此问题,请在读取循环中添加检查result == 0
并清理客户端,就像发送心跳失败一样。
推荐阅读
- stanford-nlp - 斯坦福 NER 自定义模型精度测试
- css - 如何在 IE11 中使用 CSS 网格填充空间?
- go - Intellisense 不工作 golang 测试文件
- c - Is it safe to put control char in string?
- php - 将 Ajax 调用封装到一个类中,当我使用它上传文件时,它的工作很奇怪
- c# - 如何使用通用类型和 lambda 构造和编译此 C# 表达式
- javascript - Type Void is Not Assignable to Type Boolean (React/Typescript)
- ios - iOS Firebase -When Fanning -'InvalidPathValidation',原因:'(child:) 必须是非空字符串且不包含'。' '#' '$' '[' 或 ']''
- typescript - 为什么这个包装函数不拾取函数重载(打字稿)?
- c# - 为什么从 uint 中减去 int.MaxValue 与从 uint 中减去 int 类型变量不同?