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.

proper Cancellation?

See original GitHub issue

Currently the app freezes for seconds when clicking the red X of the console window – which is nearly the same as a task kill. How to stop Socket.AcceptAsync from “blocking” infinitely?

Further:

  • what about proper disposing of IDisposables?
  • async Main method does not contain a single await … why not await ProcessLinesAsync?
  • no .editorconfig so first contact with ctrl+k-d reformats everything

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
gfoidlcommented, Jul 11, 2018

Keep in mind that this is an introductionary sample, not one that can and should be used in production directly. It’s more or less a demo to get your feets wet with pipelines. If this demo would be productized, then it would be too big for an intro.

Currently the app freezes for seconds when clicking the red X of the console window

In this demo there is no unbind on the socket, etc. and nothing that stops the accept-loop https://github.com/davidfowl/TcpEcho/blob/07ad4df181151850974219b8ebf434eee37778a4/src/Program.cs#L22-L26 You could run this server in a generic host, and leverage cancellation as well as proper disposing. But, as said before, this would be too much for an intro…

async Main method does not contain a single await

Here it is: https://github.com/davidfowl/TcpEcho/blob/07ad4df181151850974219b8ebf434eee37778a4/src/Program.cs#L24

… why not await ProcessLinesAsync?

Imagine https://github.com/davidfowl/TcpEcho/blob/07ad4df181151850974219b8ebf434eee37778a4/src/Program.cs#L25 would be written as

await ProcessLinesAsync(socket);

what would happen?

  1. The accept-loop asynchronously awaits a client-connection https://github.com/davidfowl/TcpEcho/blob/07ad4df181151850974219b8ebf434eee37778a4/src/Program.cs#L24
  2. ProcessLinesAsync is “started” for that client, the accept-loop asynchronously awaits ProcessLinesAsync to be completed
  3. ProcessLinesAsync will complete only when reading and writing are done https://github.com/davidfowl/TcpEcho/blob/07ad4df181151850974219b8ebf434eee37778a4/src/Program.cs#L37
  4. goto point 1

So this would handle one client at a time and when the one is done, the next client is handled. Not very scalable.

With the implementation as is, the following happens:

  1. The accept-loop asynchronously awaits a client-connection
  2. ProcessLinesAsync is “started” for that client, the returning Task is thrown away, because we don’t want to await it
  3. goto point 1

So when ProcessLinesAsync is started – or to be more precise, when it yields the first time (await) – the accept-loop can await for other clients. Quite more scalable. Mature implementations like Kestrel have a more complex accept-loop.

1reaction
GSPPcommented, Aug 9, 2018

Adding two related issues:

Read more comments on GitHub >

github_iconTop Results From Across the Web

Frequently Asked Questions | San Francisco Proper
Guests must cancel by 3pm, 72 hours prior to arrival to avoid a penalty of one night's room and tax. Reservations with pre-paid...
Read more >
“Canceled” or “Cancelled”–Which Is Correct?
Canceled and cancelled are both correct, but the spelling depends on where you call home. Learn more about these two tricky words.
Read more >
How to Create a Cancellation Policy: Templates + Examples
Try these cancellation policy templates, tips, and examples to write a winning cancellation policy for your home service business.
Read more >
Buyer's Remorse: The FTC's Cooling-Off Rule May Help
The Cooling-Off Rule gives you three days to cancel certain sales made at your home, workplace, ... The Seller Must Tell You About...
Read more >
Right to Cancel
Where there is a right to cancel, the cancellation periods are short, typically three days, and they begin from the day you sign...
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