TcpStream Ctrl^C

I have some sample code written using async-std - a client and a server. I am able to send messages from the client to the server and everything works. However when I Ctrl^C the client I see a flurry of the last message sent by the client at the server. Any pointers as where I should look to potentially debug this problem? Unfortunately no code sample to share to make it simplistic. Any pointers would be helpful.

Which problem are you actually having? By Ctrl+C-ing the client you're actually SIGINT-ing your whole program, which by default terminates its execution. Your client (whatever it is), has been set to bombard the server with a bunch of info in case of an unexpected error, and this is precisely what happens.

So what's the issue?

If you want to a graceful shutdown, use something like async-ctrlc, spawn your server, save the handle of the task that comes back and terminate it when you receive a Ctrl+C, that's it.

Let me elaborate a bit. Each message sent has a unique ID. Let's say I send one message from the client to the server with ID = 1. Now when I hit Ctrl^C on the client I see the server receive 10s of the same message i.e with ID = 1. That's bizarre. Is that expected behavior that I need to handle using async-ctrlc? Is that what you are suggesting? Thanks in advance. I will look into async-ctrlc in parallel.

Without knowing which kind of client you're using, and how your server is set up to receive your messages, it's impossible to tell - and I'm not a mind reader, unfortunately, nor can I connect to your machine remotely to study what exactly happens in a situation you've described. If you could be just a bit more explicit about the libraries that you're using, at least, that would help a lot.

Again, can't suggest much without knowing almost anything about the implementation of your program. Off the top of my head, if you're confident that each message with a unique ID must only be received once, you can configure your server to ignore any client messages containing the ID that was already received in the past. Otherwise, configure your client to gracefully terminate using async-ctrlc.

For the rest, I'll need a bit more than "this client (which client?) that sends these messages (which messages?) to this server (which server?) sends too many of them when I terminate the program in a way that it doesn't expect".

If I had to guess based on what’s written here, I would suspect that when you Ctrl+C the client it is closing the socket, and your server you are calling TCPStream::read() but not checking for a return value of Ok(0) (which is what it returns when the peer closes the connection) or Err (which it could also return if the connection is forcibly reset instead of gracefully closed). If you’re reusing the same buffer over and over to read a message each time, and you’re not checking for a zero or Err return value, then you’d see the old message over and over, not because you’re actually receiving that message, but rather because it’s still sitting in the buffer and hasn’t been overwritten.

1 Like

Thanks that was indeed the problem. The "match" for the return values were messed up. Thanks.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.