C
C#16mo ago
iskander

❔ ✅ (SOLVED) TcpListener.AcceptTcpClientAsync does not get cancelled

CancellationTokenSource cancel = new(400);

TcpClient JoinedClient = await Server.AcceptTcpClientAsync(cancel.Token);
CancellationTokenSource cancel = new(400);

TcpClient JoinedClient = await Server.AcceptTcpClientAsync(cancel.Token);
so i expect this code to accept any tcp clients and if a 400ms period pass it will cancel but for whatever reason AcceptTcpClientAsync keeps halting my execution! i could probably run my own wait timer and if the timer finishes then i will call TcpListener.Stop() but i can see how that workaround would cause issues for in the future. and one odd thing is when a tcp client successfully connects to the server the function doesnt finish execution oddly
10 Replies
cap5lut
cap5lut16mo ago
are u handling the cancellation exception?
MODiX
MODiX16mo ago
cap5lut
REPL Result: Success
using System.Diagnostics;
using System.Net.Sockets;

var listener = new TcpListener(System.Net.IPAddress.Any, 55555);
listener.Start();

var cts = new CancellationTokenSource(400);
var sw = Stopwatch.StartNew();
try
{
await listener.AcceptTcpClientAsync(cts.Token);
}
catch(Exception e)
{
Console.WriteLine(e);
}
finally
{
sw.Stop();
Console.WriteLine(sw.Elapsed);
}

listener.Stop();
using System.Diagnostics;
using System.Net.Sockets;

var listener = new TcpListener(System.Net.IPAddress.Any, 55555);
listener.Start();

var cts = new CancellationTokenSource(400);
var sw = Stopwatch.StartNew();
try
{
await listener.AcceptTcpClientAsync(cts.Token);
}
catch(Exception e)
{
Console.WriteLine(e);
}
finally
{
sw.Stop();
Console.WriteLine(sw.Elapsed);
}

listener.Stop();
Console Output
System.OperationCanceledException: The operation was canceled.
at System.Threading.CancellationToken.ThrowOperationCanceledException()
at System.Threading.CancellationToken.ThrowIfCancellationRequested()
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource<System.Net.Sockets.Socket>.GetResult(Int16 token)
at System.Net.Sockets.TcpListener.<AcceptTcpClientAsync>g__WaitAndWrap|31_0(ValueTask`1 task)
at Submission#0.<<Initialize>>d__0.MoveNext()
00:00:00.4022679
System.OperationCanceledException: The operation was canceled.
at System.Threading.CancellationToken.ThrowOperationCanceledException()
at System.Threading.CancellationToken.ThrowIfCancellationRequested()
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource<System.Net.Sockets.Socket>.GetResult(Int16 token)
at System.Net.Sockets.TcpListener.<AcceptTcpClientAsync>g__WaitAndWrap|31_0(ValueTask`1 task)
at Submission#0.<<Initialize>>d__0.MoveNext()
00:00:00.4022679
Compile: 680.802ms | Execution: 506.433ms | React with ❌ to remove this embed.
iskander
iskanderOP16mo ago
im handling An expectation 😅 ok i gonna try to catch but shouldn't VS break execution when an exception is thrown? yep that did it! thanks a lot @cap5lut
cap5lut
cap5lut16mo ago
in some situations it doesnt, for example if u do not await a task where its thrown in this is the way to $close the thread:
MODiX
MODiX16mo ago
Use the /close command to mark a forum thread as answered
iskander
iskanderOP16mo ago
yeah but i did await the task ig im missing something...good excuse to research some more about C# heh
cap5lut
cap5lut16mo ago
well, dunno. maybe the code u where u await is already in an unawaited task this for example doesnt make VS pause execution
_ = Task.Run(async () =>
{
var listener = new TcpListener(System.Net.IPAddress.Any, 55555);
listener.Start();

var cts = new CancellationTokenSource(400);
var sw = Stopwatch.StartNew();
await listener.AcceptTcpClientAsync(cts.Token);
sw.Stop();
Console.WriteLine(sw.Elapsed);

listener.Stop();
});
_ = Task.Run(async () =>
{
var listener = new TcpListener(System.Net.IPAddress.Any, 55555);
listener.Start();

var cts = new CancellationTokenSource(400);
var sw = Stopwatch.StartNew();
await listener.AcceptTcpClientAsync(cts.Token);
sw.Stop();
Console.WriteLine(sw.Elapsed);

listener.Stop();
});
using await Task.Run(...); instead makes it pause
iskander
iskanderOP16mo ago
umm yeah that could be it. i called the task while i was doing some other operations thanks a lot cap! you've been extremely helpful 😊
cap5lut
cap5lut16mo ago
glad i could help o7
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server