-
Notifications
You must be signed in to change notification settings - Fork 62
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 bug where last few bytes on socket go unread #642
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9cefef5
add regression test
graebm 408970c
socket_channel_handler no longer shuts down the channel the moment an…
graebm aa524b4
ERROR_BROKEN_PIPE -> AWS_IO_SOCKET_CLOSED
graebm 7293f4f
put back functionality where rw_handler_write() could happen synchron…
graebm 15ae9e6
revert changes I didn't need to make
graebm 577a5d7
clang-format
graebm c253973
test IPv4 & IPv6 too
graebm 79d9fdf
skip test if IPv6 isn't supported
graebm b2d6005
fix warnings
graebm 3eb8d9b
don't try to clean up after skip
graebm 11e6267
log statement wasn't totally correct
graebm 69eea42
fix logging the wrong pointer
graebm 6c85d64
Disable failing tests on Windows, with detailed comment explaining why
graebm 7db6a74
WIN32, not WINDOWS
graebm 5ab4086
explain
graebm bc9918a
I am so done with you windows
graebm 56d92d3
comments
graebm 3b3bdef
comments++
graebm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,10 @@ static void s_on_readable_notification(struct aws_socket *socket, int error_code | |
*/ | ||
static void s_do_read(struct socket_handler *socket_handler) { | ||
|
||
if (socket_handler->shutdown_in_progress) { | ||
return; | ||
} | ||
|
||
size_t downstream_window = aws_channel_slot_downstream_read_window(socket_handler->slot); | ||
size_t max_to_read = | ||
downstream_window > socket_handler->max_rw_size ? socket_handler->max_rw_size : downstream_window; | ||
|
@@ -139,17 +143,20 @@ static void s_do_read(struct socket_handler *socket_handler) { | |
|
||
size_t total_read = 0; | ||
size_t read = 0; | ||
while (total_read < max_to_read && !socket_handler->shutdown_in_progress) { | ||
int last_error = 0; | ||
while (total_read < max_to_read) { | ||
size_t iter_max_read = max_to_read - total_read; | ||
|
||
struct aws_io_message *message = aws_channel_acquire_message_from_pool( | ||
socket_handler->slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, iter_max_read); | ||
|
||
if (!message) { | ||
last_error = aws_last_error(); | ||
break; | ||
} | ||
|
||
if (aws_socket_read(socket_handler->socket, &message->message_data, &read)) { | ||
last_error = aws_last_error(); | ||
aws_mem_release(message->allocator, message); | ||
break; | ||
} | ||
|
@@ -162,6 +169,7 @@ static void s_do_read(struct socket_handler *socket_handler) { | |
(unsigned long long)read); | ||
|
||
if (aws_channel_slot_send_message(socket_handler->slot, message, AWS_CHANNEL_DIR_READ)) { | ||
last_error = aws_last_error(); | ||
aws_mem_release(message->allocator, message); | ||
break; | ||
} | ||
|
@@ -170,30 +178,29 @@ static void s_do_read(struct socket_handler *socket_handler) { | |
AWS_LOGF_TRACE( | ||
AWS_LS_IO_SOCKET_HANDLER, | ||
"id=%p: total read on this tick %llu", | ||
(void *)&socket_handler->slot->handler, | ||
(void *)socket_handler->slot->handler, | ||
(unsigned long long)total_read); | ||
|
||
socket_handler->stats.bytes_read += total_read; | ||
|
||
/* resubscribe as long as there's no error, just return if we're in a would block scenario. */ | ||
if (total_read < max_to_read) { | ||
int last_error = aws_last_error(); | ||
AWS_ASSERT(last_error != 0); | ||
|
||
if (last_error != AWS_IO_READ_WOULD_BLOCK && !socket_handler->shutdown_in_progress) { | ||
if (last_error != AWS_IO_READ_WOULD_BLOCK) { | ||
aws_channel_shutdown(socket_handler->slot->channel, last_error); | ||
} else { | ||
AWS_LOGF_TRACE( | ||
AWS_LS_IO_SOCKET_HANDLER, | ||
"id=%p: out of data to read on socket. " | ||
"Waiting on event-loop notification.", | ||
(void *)socket_handler->slot->handler); | ||
} | ||
|
||
AWS_LOGF_TRACE( | ||
AWS_LS_IO_SOCKET_HANDLER, | ||
"id=%p: out of data to read on socket. " | ||
"Waiting on event-loop notification.", | ||
(void *)socket_handler->slot->handler); | ||
return; | ||
} | ||
/* in this case, everything was fine, but there's still pending reads. We need to schedule a task to do the read | ||
* again. */ | ||
if (!socket_handler->shutdown_in_progress && total_read == socket_handler->max_rw_size && | ||
!socket_handler->read_task_storage.task_fn) { | ||
if (total_read == socket_handler->max_rw_size && !socket_handler->read_task_storage.task_fn) { | ||
|
||
AWS_LOGF_TRACE( | ||
AWS_LS_IO_SOCKET_HANDLER, | ||
|
@@ -212,17 +219,29 @@ static void s_on_readable_notification(struct aws_socket *socket, int error_code | |
(void)socket; | ||
|
||
struct socket_handler *socket_handler = user_data; | ||
AWS_LOGF_TRACE(AWS_LS_IO_SOCKET_HANDLER, "id=%p: socket is now readable", (void *)socket_handler->slot->handler); | ||
|
||
/* read regardless so we can pick up data that was sent prior to the close. For example, peer sends a TLS ALERT | ||
* then immediately closes the socket. On some platforms, we'll never see the readable flag. So we want to make | ||
AWS_LOGF_TRACE( | ||
AWS_LS_IO_SOCKET_HANDLER, | ||
"id=%p: socket on-readable with error code %d(%s)", | ||
(void *)socket_handler->slot->handler, | ||
error_code, | ||
aws_error_name(error_code)); | ||
|
||
/* Regardless of error code call read() until it reports error or EOF, | ||
* so we can pick up data that was sent prior to the close. | ||
* | ||
* For example, if peer closes the socket immediately after sending the last | ||
* bytes of data, the READABLE and HANGUP events arrive simultaneously. | ||
graebm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* Another example, peer sends a TLS ALERT then immediately closes the socket. | ||
* On some platforms, we'll never see the readable flag. So we want to make | ||
* sure we read the ALERT, otherwise, we'll end up telling the user that the channel shutdown because of a socket | ||
* closure, when in reality it was a TLS error */ | ||
* closure, when in reality it was a TLS error | ||
* | ||
* It may take more than one read() to get all remaining data. | ||
* Also, if the downstream read-window reaches 0, we need to patiently | ||
* wait until the window opens before we can call read() again. */ | ||
(void)error_code; | ||
s_do_read(socket_handler); | ||
|
||
if (error_code && !socket_handler->shutdown_in_progress) { | ||
aws_channel_shutdown(socket_handler->slot->channel, error_code); | ||
} | ||
Comment on lines
-223
to
-225
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only line that truly HAD to change to fix this bug |
||
} | ||
|
||
/* Either the result of a context switch (for fairness in the event loop), or a window update. */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function didn't have any bugs. I'm just simplifying the code.
When this code was written,
shutdown_in_progress
could change half-way through the function, but since 2019 shutdown is always queued, so moving this check to the top of the function, instead of having it at 3 separate parts of a complicated flow