question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WatsonTcpServer.ListClients() is empty if called "too early"

See original GitHub issue

Hey!

server.Start();
client.Connect();

Console.WriteLine(server.ListClients().Count());
await Task.Delay(1); // Thread.Sleep(1) also works
Console.WriteLine(server.ListClients().Count());

This will output:

0
1

This is very problematic as one might not expect server.ListClients() to return an empty enumerable, especially if they’re not aware of this issue.

I’m not sure why a 1ms delay seems to fix it. Are some async events (registering the client on server side) not awaited/fired on time or something?

Why does this happen and could it be fixed?

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Laiteuxcommented, Nov 28, 2022

Yeah, very understandable.

You can label this issue if you wish and keep it for later or whatever, I guess it’ll be fine for now. Especially if I’m the first one reporting about it.

In a real server<->client scenario, this probably won’t be important anyway because of the fact there is already ping between everything.

This was found because I ran some tests locally, but I can hardly think of a situation where you’d need to call ListClients() less than a few milliseconds after a client connects.

0reactions
jchristncommented, Nov 28, 2022

Yeah, not sure how I’m going to be able to fix that. There’s very little code between the server accepting the connection and the client metadata being added to the client list. This is a biproduct of the fact that the client is returning faster from BeginConnect() (and everything else it does within Connect() than the server does from the first few steps of AcceptConnections().

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · jchristn/WatsonTcp
WatsonTcp is the easiest way to build TCP-based clients and servers in C#. ... WatsonTcpServer.ListClients() is empty if called "too early" help wanted....
Read more >
C# Socket keep receiving empty message
Reason for receiving empty messages is that your client socket remains connected. You can wait to receive more messages with your client, if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found