Skip to content
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

Improve typed socket connection robustness #810

Merged
merged 6 commits into from
Sep 10, 2024

Conversation

paulgb
Copy link
Member

@paulgb paulgb commented Sep 10, 2024

What we know:

  • Sometimes, when there are network issues, some (but not all) drones fail to reconnect. In the logs, it shows up as failed to send heartbeat (from heartbeat.rs), with the err value of Disconnected (typed_socket).
  • When we “send” websocket messages, we are actually sending messages to a queue that gets picked up by the websocket event loop asynchronously. Disconnected actually means that the message queue is full, which implies we are disconnected, but is not immediate.
  • On a TypedSocket, send() uses Sender::send, while TypedSocketSender's send() uses try_send.
  • In tokio, send on a full channel blocks until the channel has capacity. try_send returns immediately if the channel is full.

My leading theory is that when the network is interrupted abruptly, upon reconnecting to the controller, the controller sends a bunch of messages, causing a deadlock:

  • the new_client loop is stalled waiting for capacity in send_to_client, which won't happen until socket.recv() is called in the main drone event loop
  • the main drone event loop is waiting on a call to socket.send() when acking an action

This PR introduces several changes, which should improve the robustness of reconnects:

  • Instead of handling messages from the controller directly in the drone event loop, they are sent to separate tasks. This means that nothing can get in the way of the drone event loop's ability to call socket.recv().
  • TypedSocket::send now uses Sender::try_send, for consistency with TypedSocketSender::send. Note that as a result of moving handling out of the drone event loop, messages sent from the drone loop now use TypedSocketSender instead of TypedSocket anyway, so those messages would now use try_send regardless.
  • As a result of using try_send, TypedSocket::send no longer needs to be async, so that's removed.
  • TypedSocketError::Disconnected is renamed to TypedSocketError::Clogged, to better reflect what the issue is.

Copy link

vercel bot commented Sep 10, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
plane ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 10, 2024 10:43pm

@@ -42,13 +42,13 @@ pub enum TypedSocketError {
#[error("Socket closed")]
Closed,
#[error("Socket disconnected")]
Copy link
Member

Choose a reason for hiding this comment

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

"Socket clogged"?

Copy link
Member

@rolyatmax rolyatmax left a comment

Choose a reason for hiding this comment

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

LGTM

@paulgb paulgb merged commit 3c4cdbd into main Sep 10, 2024
6 checks passed
@paulgb paulgb deleted the paul/dis-2636-plane-sometimes-does-not-reconnect branch September 10, 2024 22:54
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.

2 participants