Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incomplete Dispose of WatsonTCPServer #303

Open
sancheolz opened this issue Nov 14, 2024 · 2 comments
Open

Incomplete Dispose of WatsonTCPServer #303

sancheolz opened this issue Nov 14, 2024 · 2 comments

Comments

@sancheolz
Copy link

sancheolz commented Nov 14, 2024

This test fails starting from 6.0 version of WatsonTCP:

var server = new WatsonTcpServer("127.0.0.1", 9000);
server.Events.MessageReceived += (_, _) => { Console.WriteLine("srecv"); };
server.Start();
var client = new WatsonTcpClient("127.0.0.1", 9000);
client.Events.MessageReceived += (_, _) => { Console.WriteLine("crecv"); };
client.Connect();
Task.Run(() => { client.SendAsync("aaa"); });
server.Dispose();

It throws

System.AggregateException : One or more errors occurred. (A task was canceled.)
  ----> System.Threading.Tasks.TaskCanceledException : A task was canceled.
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at WatsonTcp.WatsonTcpServer.Dispose(Boolean disposing) in WatsonTcpServer.cs:line 621
@ShayanFiroozi
Copy link
Contributor

Hi ,

Task.Run(() => { client.SendAsync("aaa"); });

You are executing the SendAsync in the Fire And Forget Task !

Which means the server.Dispose() will be called Before the completion of SendAsync.

Try this two options :

client.SendAsync("aaa").GetAwaiter.GetResult(); ( without Task.Run) and see what happens.

Or

await client.SendAsync("aaa");

Hope this helps.👍

@sancheolz
Copy link
Author

@ShayanFiroozi Hi. The client can be on another machine and send messages independently from the server. This example simulates such a scenario. So waiting doesn't make sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants