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

Use sync socket write to ensure that full message is written #39

Closed

Conversation

danhermann
Copy link

@danhermann danhermann commented Jun 6, 2019

This replaces the select call on the socket with a simple sync write since the async write call was not properly verifying that the entire payload was written to the socket and there wasn't really a performance benefit with the existing async write. The failure to verify that the entire payload was written to the socket is the suspected cause of truncated messages in situations where unusually long messages were sent via TCP.

May relate to #30 and #33.

Copy link
Contributor

@yaauie yaauie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this directly solves the truncation issues, but have suggested an improvement that should.

See in-line comments.


# Now send the payload
client_socket.syswrite(payload) if w.any?
client_socket.syswrite(payload)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the cause of truncated messages is likely caused by our failure to check the return value of IO#syswrite; if a payload is partially written at this level, we make no attempt to continue writing from where we left off.

We can fix that by performing a byteslice and repeating the IO#syswrite call with the remainder until the remaining payload is empty:

Suggested change
client_socket.syswrite(payload)
while payload && payload.bytesize > 0
written = client_socket.syswrite(payload)
payload = payload.byteslice(written..-1)
end

Separately, the code that this removes gives us an opportunity to have an EOFError be raised before we send data if the remote end closes the socket on us, while the replacement will raise a SystemCallError when IO#syswrite is sent to a closed socket. I have not thought through all of the ramifications of changing the error being raised.

I think there is definitely a bug in the implementation, in which a socket that is readable (e.g., the remote end has sent bytes that are not EOF) and not writable (e.g., has a zero window) would cause the payload to be dropped instead of written.

I believe this secondary issue can be solved by retrying the IO::select until we have a writable client_socket:

            begin
              r,w,_ = IO.select([client_socket],[client_socket])
              # don't expect any reads, but a readable socket might	
              # mean the remote end closed, so read it and throw it away.	
              # we'll get an EOFError if it happens.
              r.each { |readable| readable.sysread(16384) }
              retry unless w.any?
            end

There may be dragons here, if the socket never becomes writable or readable with an EOF, but these dragons also exist in the current implementation and are not a new addition.

@roaksoax
Copy link

This PR was superseded by #49, as such, I'm closing this PR.

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

Successfully merging this pull request may close these issues.

5 participants