[英]Ping Tasks will not complete
我正在開發一個“心跳”應用程序,它每分鍾通過一個循環 ping 數百個 IP 地址。 IP 地址存儲在 class Machines
列表中。 我有一個循環,它為每個 IP 創建一個Task<MachinePingResults>
(其中MachinePingResults
基本上是一個 IP 和在線狀態的元組)並使用System.Net.NetworkInformation
調用 ping ZC1C425268E68385D1AB5074
我遇到的問題是,運行數小時(或數天)后,主程序的一個循環無法完成導致 memory 泄漏的Tasks
。 我無法確定為什么我的任務沒有完成(如果我在運行幾天后在運行時查看任務列表,有數百個任務顯示為“等待”)。 大多數時候,所有任務都完成並被處理; 他們沒有完成只是隨機的。 例如,過去 24 小時有一個問題,大約在 12 小時內有 148 個等待任務從未完成。 由於無法查看Ping
為何掛起的性質(因為它在 .NET 內部),我無法復制該問題以進行調試。
( 似乎.NET 中的Ping
調用可能會掛起,並且如果存在 DNS 問題,則內置超時會失敗,這就是我在其中構建額外超時的原因)
如果使用Task.Delay
和CancellationToken
在 15 秒內沒有返回 ping,我有辦法取消主循環。 然后在每個 Ping function 中我有一個Delay
,以防 Ping 調用本身掛起,這會強制 function 完成。 另請注意,我只 ping IPv4; 沒有 IPv6 或 URL。
主循環
pingcancel = new CancellationTokenSource();
List<Task<MachinePingResults>> results = new List<Task<MachinePingResults>>();
try
{
foreach (var m in localMachines.FindAll(m => !m.Online))
results.Add(Task.Run(() =>
PingMachine(m.ipAddress, 8000), pingcancel.Token
));
await Task.WhenAny(Task.WhenAll(results.ToArray()), Task.Delay(15000));
pingcancel.Cancel();
}
catch (Exception ex) { Console.WriteLine(ex); }
finally
{
results.Where(r => r.IsCompleted).ToList()
.ForEach(r =>
//modify the online machines);
results.Where(r => r.IsCompleted).ToList().ForEach(r => r.Dispose());
results.Clear();
}
平Function
static async Task<MachinePingResults> PingMachine(string ipaddress, int timeout)
{
try
{
using (Ping ping = new Ping())
{
var reply = ping.SendPingAsync(ipaddress, timeout);
await Task.WhenAny(Task.Delay(timeout), reply);
if (reply.IsCompleted && reply.Result.Status == IPStatus.Success)
{
return new MachinePingResults(ipaddress, true);
}
}
}
catch (Exception ex)
{
Debug.WriteLine("Error: " + ex.Message);
}
return new MachinePingResults(ipaddress, false);
}
如果 Ping 掛起,每個Task
都有延遲讓它繼續,我不知道是什么問題導致某些Task<MachinePingResults>
永遠無法完成。
如何確保使用 .NET Ping
結束的Task
?
發布的代碼中有很多空白,但我嘗試復制並最終進行了一些重構。
這個版本看起來相當健壯,對SendAsync
的實際調用包含在適配器 class 中。
我接受這不一定直接回答問題,但在無法准確復制您的問題的情況下,提供了另一種結構化代碼的方法,可以消除問題。
async Task Main()
{
var masterCts = new CancellationTokenSource(TimeSpan.FromSeconds(15)); // 15s overall timeout
var localMachines = new List<LocalMachine>
{
new LocalMachine("192.0.0.1", false), // Should be not known - TimedOut
new LocalMachine("192.168.86.88", false), // Should be not known - DestinationHostUnreachable (when timeout is 8000)
new LocalMachine("www.dfdfsdfdfdsgrdf.cdcc", false), // Should be not known - status Unknown because of PingException
new LocalMachine("192.168.86.87", false) // Known - my local IP
};
var results = new List<PingerResult>();
try
{
// Create the "hot" tasks
var tasks = localMachines.Where(m => !m.Online)
.Select(m => new Pinger().SendPingAsync(m.HostOrAddress, 8000, masterCts.Token))
.ToArray();
await Task.WhenAll(tasks);
results.AddRange(tasks.Select(t => t.Result));
}
finally
{
results.ForEach(r => localMachines.Single(m => m.HostOrAddress.Equals(r.HostOrAddress)).Online = r.Status == IPStatus.Success);
results.Dump(); // For LINQPad
localMachines.Dump(); // For LINQPad
results.Clear();
}
}
public class LocalMachine
{
public LocalMachine(string hostOrAddress, bool online)
{
HostOrAddress = hostOrAddress;
Online = online;
}
public string HostOrAddress { get; }
public bool Online { get; set; }
}
public class PingerResult
{
public string HostOrAddress {get;set;}
public IPStatus Status {get;set;}
}
public class Pinger
{
public async Task<PingerResult> SendPingAsync(string hostOrAddress, int timeout, CancellationToken token)
{
// Check if timeout has occurred
token.ThrowIfCancellationRequested();
IPStatus status = default;
try
{
var reply = await SendPingInternal(hostOrAddress, timeout, token);
status = reply.Status;
}
catch (PingException)
{
status = IPStatus.Unknown;
}
return new PingerResult
{
HostOrAddress = hostOrAddress,
Status = status
};
}
// Wrap the legacy EAP pattern offered by Ping.
private Task<PingReply> SendPingInternal(string hostOrAddress, int timeout, CancellationToken cancelToken)
{
var tcs = new TaskCompletionSource<PingReply>();
if (cancelToken.IsCancellationRequested)
{
tcs.TrySetCanceled();
}
else
{
using (var ping = new Ping())
{
ping.PingCompleted += (object sender, PingCompletedEventArgs e) =>
{
if (!cancelToken.IsCancellationRequested)
{
if (e.Cancelled)
{
tcs.TrySetCanceled();
}
else if (e.Error != null)
{
tcs.TrySetException(e.Error);
}
else
{
tcs.TrySetResult(e.Reply);
}
}
};
cancelToken.Register(() => { tcs.TrySetCanceled(); });
ping.SendAsync(hostOrAddress, timeout, new object());
}
};
return tcs.Task;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.