Fix panic caused by race condition when writing while dropping reader #16
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.
Writer
has a race condition that might cause a panic ifpoll_write
is called more than once while theReader
is in the processed of being dropped on another thread.The race condition is caused by the drop order of
Reader
: Since fields are dropped in the order they are defined in, the buffer pool channel is dropped before the buffer stream channel. Ifpoll_write
is called more than once between these two drop calls, then the underlying channel will panic because we've polled it after already returningNone
.The fix is to ensure the buffer stream channel is closed first before the buffer pool channel, since that is the channel we use to detect when the reader is closed. One way of doing this would be to just reverse the order these two fields are defined in, but it is better to be clear that the drop order matters by using an explicit drop handler.
With this fix, we know that the second channel will be polled at most once when closed, because subsequent calls to
poll_write
will check the first channel before polling the second.See also sagebind/isahc#295.