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

fix handling closed socket #21

Merged
merged 1 commit into from
Mar 8, 2023
Merged

Conversation

piotr-karas
Copy link
Contributor

Hi, I recently have encountered some problem with handling closed socket.

I had a lot of processes in my application and some of them have very often received 'eof' from socket (why this happend I'm not sure yet, probably it's something with system resources/connection speed/NATS server termination).
In most cases I have just received Server xyz closed the connection, but sometimes I got for example:
Invalid protocol MS or something like Unexpected error: Expected integer, but got "" while executing "incr expMsgLength 2".

Both this messages looked like we got only part of message while reading from socket. I have read about chan gets channelId ?varName? in the documentation and it says:

If an end-of-file occurs while part way through reading a line, the partial line will be returned (or written into varName).

So we actually can read part of message, if there was eof later, and treat it as a whole causing problem with handling it further. If we move eof check above that if statement, we will terminate it quicker and do re-connect without unexpected errors.

I have encountered this in old version, but I see that it is probably still here :)

Best regards

@Kazmirchuk
Copy link
Owner

Thanks for the PR!
Tcl documentation for non-blocking gets is very misleading if not outright wrong. I assumed that checking for $readCount <= 0 is enough to be sure I never get an incomplete line, but now I've experimented with it and see that it's not true.
Regarding frequent disconnections: could it be the 'slow consumer' problem? That your app receives more messages/sec than it can process, and then NATS server terminates slow connections? maybe you can see smth in NATS logs

@Kazmirchuk Kazmirchuk merged commit 8427156 into Kazmirchuk:master Mar 8, 2023
@Kazmirchuk
Copy link
Owner

OTOH Ashok does mention this in his book on p.434:

Notice that on end of file, the buffered input content is delivered even though no newline characters were present.

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

Successfully merging this pull request may close these issues.

2 participants