Skip to content

Commit 7c3a23b

Browse files
author
Eugene Ostroukhov
committed
inspector: handle socket close before close frame
This change handles clients that respond to close request with a TCP close instead of close response. PR-URL: #12937 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 642bd4d commit 7c3a23b

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/inspector_socket.cc

+4
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ static void websockets_data_cb(uv_stream_t* stream, ssize_t nread,
335335
if (!inspector->shutting_down && inspector->ws_state->read_cb) {
336336
inspector->ws_state->read_cb(stream, nread, nullptr);
337337
}
338+
if (inspector->ws_state->close_sent &&
339+
!inspector->ws_state->received_close) {
340+
shutdown_complete(inspector); // invoke callback
341+
}
338342
} else {
339343
#if DUMP_READS
340344
printf("%s read %ld bytes\n", __FUNCTION__, nread);

test/cctest/test_inspector_socket.cc

+31-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ static std::string last_path; // NOLINT(runtime/string)
3535
static void (*handshake_delegate)(enum inspector_handshake_event state,
3636
const std::string& path,
3737
bool* should_continue);
38+
static const char SERVER_CLOSE_FRAME[] = {'\x88', '\x00'};
39+
3840

3941
struct read_expects {
4042
const char* expected;
@@ -879,7 +881,6 @@ TEST_F(InspectorSocketTest, Send1Mb) {
879881
// 3. Close
880882
const char CLIENT_CLOSE_FRAME[] = {'\x88', '\x80', '\x2D',
881883
'\x0E', '\x1E', '\xFA'};
882-
const char SERVER_CLOSE_FRAME[] = {'\x88', '\x00'};
883884
do_write(CLIENT_CLOSE_FRAME, sizeof(CLIENT_CLOSE_FRAME));
884885
expect_on_client(SERVER_CLOSE_FRAME, sizeof(SERVER_CLOSE_FRAME));
885886
GTEST_ASSERT_EQ(0, uv_is_active(
@@ -906,4 +907,33 @@ TEST_F(InspectorSocketTest, ErrorCleansUpTheSocket) {
906907
EXPECT_EQ(UV_EPROTO, err);
907908
}
908909

910+
static void ServerClosedByClient_cb(InspectorSocket* socket, int code) {
911+
*static_cast<bool*>(socket->data) = true;
912+
}
913+
914+
TEST_F(InspectorSocketTest, NoCloseResponseFromClinet) {
915+
ASSERT_TRUE(connected);
916+
ASSERT_FALSE(inspector_ready);
917+
do_write(const_cast<char*>(HANDSHAKE_REQ), sizeof(HANDSHAKE_REQ) - 1);
918+
SPIN_WHILE(!inspector_ready);
919+
expect_handshake();
920+
921+
// 2. Brief exchange
922+
const char SERVER_MESSAGE[] = "abcd";
923+
const char CLIENT_FRAME[] = {'\x81', '\x04', 'a', 'b', 'c', 'd'};
924+
inspector_write(&inspector, SERVER_MESSAGE, sizeof(SERVER_MESSAGE) - 1);
925+
expect_on_client(CLIENT_FRAME, sizeof(CLIENT_FRAME));
926+
927+
bool closed = false;
928+
929+
inspector.data = &closed;
930+
inspector_close(&inspector, ServerClosedByClient_cb);
931+
expect_on_client(SERVER_CLOSE_FRAME, sizeof(SERVER_CLOSE_FRAME));
932+
uv_close(reinterpret_cast<uv_handle_t*>(&client_socket), nullptr);
933+
SPIN_WHILE(!closed);
934+
inspector.data = nullptr;
935+
GTEST_ASSERT_EQ(0, uv_is_active(
936+
reinterpret_cast<uv_handle_t*>(&client_socket)));
937+
}
938+
909939
} // anonymous namespace

0 commit comments

Comments
 (0)