fix(sockets): correctly handle pending writes to prevent double write #214
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The previous implementation called
send
on the sink directly without using theSinkWriter
adapter to convert it to anAsyncWrite
. This meant polling the returned future ourself which seemed to work fine for the majority of cases as it either returnedPoll::Ready
when we were able to send the item to the underlying io orPoll::Pending
when the io was busy matching the expected behavior of theAsyncWrite
trait. However there is one other case where the future returned bysend
might returnPoll::Pending
which is when it was able to send the data to the io but flushing the underlying io returnedPoll::Pending
. This is an unfornuate property of the send function which I did not realize when initially implementing it. In pratice this leads to a lot of flushes and in my tests caused the bug that occasionally a packet was sent twice as it has been written to the io but not flushed meansing the future returnedPoll::Pending
leading to the buffer being sent a second time.TLDR:
The new implementation uses the
SinkWriter
which both does not flush on every write and ensures data is written properly.