Skip to content

Commit

Permalink
Enhanced handling for EPOLLRDHUP event
Browse files Browse the repository at this point in the history
  • Loading branch information
dimiden committed Feb 25, 2025
1 parent e7e2d89 commit 59432ef
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
24 changes: 19 additions & 5 deletions src/projects/base/ovsocket/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1521,10 +1521,18 @@ namespace ov
{
if (_blocking_mode == BlockingMode::NonBlocking)
{
// Timed out
read_bytes = 0L;
// Actually, it is not an error
socket_error = nullptr;
if (IsEndOfStream() == false)
{
// Timed out
read_bytes = 0L;
// Actually, it is not an error
socket_error = nullptr;
}
else
{
// Socket is closed
socket_error = SocketError::CreateError(error);
}
}
else
{
Expand Down Expand Up @@ -1573,6 +1581,8 @@ namespace ov
((_socket.GetType() != SocketType::Srt) && (read_bytes == 0L)) ||
((_socket.GetType() == SocketType::Srt) && (socket_error->GetCode() == SRT_ECONNLOST)))
{
logtd("Remote is disconnected with error: %s", socket_error->What());

socket_error = SocketError::CreateError("Remote is disconnected");
*received_length = 0UL;

Expand Down Expand Up @@ -1857,7 +1867,11 @@ namespace ov
{
CHECK_STATE(>= SocketState::Closed, false);

logad("Closing %s...", GetRemoteAddress() != nullptr ? GetRemoteAddress()->ToString(false).CStr() : GetStreamId().CStr());
logad("Closing %s with state %s...",
(GetRemoteAddress() != nullptr)
? GetRemoteAddress()->ToString(false).CStr()
: GetStreamId().CStr(),
StringFromSocketState(new_state));

if (GetState() == SocketState::Closed)
{
Expand Down
2 changes: 1 addition & 1 deletion src/projects/base/ovsocket/socket_datastructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace ov
return "Unknown";
}

static const char *StringFromSocketState(SocketState state)
constexpr static const char *StringFromSocketState(SocketState state)
{
switch (state)
{
Expand Down
18 changes: 14 additions & 4 deletions src/projects/base/ovsocket/socket_pool/socket_pool_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,20 @@ namespace ov
continue;
}

if (OV_CHECK_FLAG(events, EPOLLRDHUP) || OV_CHECK_FLAG(events, EPOLLHUP))
{
if (socket->GetState() != SocketState::Error)
{
// Remote has closed the connection
// (or closed the write stream (half-close))
socket->SetEndOfStream();
}
else
{
// The socket is already in error state, so ignore this event
}
}

// Normal socket generates (EPOLLOUT | EPOLLHUP) events as soon as it is added to epoll
// Client socket generates (EPOLLOUT | EPOLLIN) events as soon as it is added to epoll
if (socket->NeedToWaitFirstEpollEvent())
Expand Down Expand Up @@ -374,8 +388,6 @@ namespace ov

if (need_to_close == false)
{
auto name = String(Platform::GetThreadName());

if (OV_CHECK_FLAG(events, EPOLLOUT))
{
if (OV_CHECK_FLAG(events, EPOLLHUP) == false)
Expand Down Expand Up @@ -445,8 +457,6 @@ namespace ov
if (socket->GetState() != SocketState::Error)
{
// Disconnected
socket->SetEndOfStream();

new_state = SocketState::Disconnected;
}
else
Expand Down

0 comments on commit 59432ef

Please sign in to comment.