Skip to content

Feature/networking issues#137

Open
engbergandreas wants to merge 8 commits into
masterfrom
feature/networking-issues
Open

Feature/networking issues#137
engbergandreas wants to merge 8 commits into
masterfrom
feature/networking-issues

Conversation

@engbergandreas

Copy link
Copy Markdown
Member

This PR fixes some networking issues where we did not previously send back a protocol response when needed, for example when a WebSocket disconnects.

Ensure protocol-level WebSocket responses (Ping/Pong/Close, etc)
Fixes an issue where protocol-level WebSocket responses were not sent back to the connected peer, which could leave the peer waiting indefinitely for a response. For example, when using the OpenSpace API to disconnect, the API would not be notified that the connection has been terminated until 10-20 seconds later, when an internal ping fails.

Also fixes an additional issue: possible lingering sockets on our end if the handshake/tear-up is not complete by the peer.

Ensure socket closes even if handshake close is not complete from peer
There was an issue where if the peer sends a WebSocket close frame, Ghoul would respond with the correct handshake (previous fix), but if the peer does not properly close the TCP socket, this could leave the OpenSpace socket connection open indefinitely(or until shutdown). This fix removes the socket regardless after receiving and sending the close frame

Fixes an issue where protocol-level websocket responses were not sent back to the connected peer, which could leave the peer waiting indefinitely for a response. For example, when using the OpenSpace API to disconnect - the api would not be notified that the connection is terminated until 10-20 seconds later when an internal ping fails.
There was an issue where if the peer sends a Websocket close frame - ghoul would respond with the corect handshake (previous fix) but if the peer does not properly close the TCP socket this could leave the OpenSpace socket connection open indefinitely(or until shutdown). This fix removes the socket regardles after receiving and sending the close frame
Comment thread include/ghoul/io/socket/tcpsocket.h Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses WebSocket networking edge cases by ensuring protocol-level frames generated by websocketpp (Ping/Pong/Close handshakes) are actually written back to the peer, and by proactively closing the underlying TCP socket on WebSocket close to avoid lingering connections.

Changes:

  • Drain websocketpp’s protocol-generated output after read_some and write it back to the TCP socket.
  • Introduce TcpSocket::closeConnection() as a non-blocking close primitive and use it from WebSocket::onClose.
  • Add API documentation for closeConnection() in the TcpSocket header.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/io/socket/websocket.cpp Sends websocketpp protocol output after read_some; closes underlying TCP connection on close.
src/io/socket/tcpsocket.cpp Adds TcpSocket::closeConnection() to stop I/O loops and close the socket without disconnect()’s join behavior.
include/ghoul/io/socket/tcpsocket.h Declares and documents the new closeConnection() API.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/io/socket/websocket.cpp Outdated
Comment thread src/io/socket/websocket.cpp
Comment thread src/io/socket/tcpsocket.cpp
Comment thread include/ghoul/io/socket/tcpsocket.h

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Comment thread src/io/socket/websocket.cpp
Comment thread include/ghoul/io/socket/tcpsocket.h
Comment thread src/io/socket/websocket.cpp

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

Comment thread src/io/socket/tcpsocket.cpp
Comment thread include/ghoul/io/socket/tcpsocket.h Outdated
Comment thread src/io/socket/tcpsocket.cpp Outdated
Comment thread src/io/socket/websocket.cpp Outdated
Comment thread src/io/socket/websocket.cpp

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

Comment thread src/io/socket/websocket.cpp
Comment thread include/ghoul/io/socket/websocket.h
Comment on lines +456 to +466
bool TcpSocket::waitForOutputQueueDrained(std::chrono::milliseconds timeout) {
std::unique_lock lock(_outputQueueMutex);
return _outputNotifier.wait_for(
lock,
timeout,
[this]() {
return _outputQueue.empty() || _shouldStopThreads ||
(!_isConnected && !_isConnecting);
}
);
}
Comment on lines +351 to +353
_outputQueue.erase(_outputQueue.begin(), _outputQueue.begin() + nSentBytes);
}
_outputNotifier.notify_all(); // Let anyone waiting on drainage know
Comment thread src/io/socket/websocket.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants