mirror of
https://github.com/zed-industries/zed.git
synced 2024-10-26 16:37:58 +00:00
Use a FuturesUnordered
to process foreground messages
This prevents deadlocks when e.g., client A performs a request to client B and client B performs a request to client A. If both clients stop processing further messages until their respective request completes, they won't have a chance to respond to the other client's request and cause a deadlock. This arrangement ensures we will attempt to process earlier messages first, but fall back to processing messages arrived later in the spirit of making progress.
This commit is contained in:
parent
6eb3e72c36
commit
fb2590d913
1 changed files with 32 additions and 17 deletions
|
@ -26,6 +26,7 @@ use collections::HashMap;
|
||||||
use futures::{
|
use futures::{
|
||||||
channel::mpsc,
|
channel::mpsc,
|
||||||
future::{self, BoxFuture},
|
future::{self, BoxFuture},
|
||||||
|
stream::FuturesUnordered,
|
||||||
FutureExt, SinkExt, StreamExt, TryStreamExt,
|
FutureExt, SinkExt, StreamExt, TryStreamExt,
|
||||||
};
|
};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
@ -398,6 +399,16 @@ impl Server {
|
||||||
|
|
||||||
let handle_io = handle_io.fuse();
|
let handle_io = handle_io.fuse();
|
||||||
futures::pin_mut!(handle_io);
|
futures::pin_mut!(handle_io);
|
||||||
|
|
||||||
|
// Handlers for foreground messages are pushed into the following `FuturesUnordered`.
|
||||||
|
// This prevents deadlocks when e.g., client A performs a request to client B and
|
||||||
|
// client B performs a request to client A. If both clients stop processing further
|
||||||
|
// messages until their respective request completes, they won't have a chance to
|
||||||
|
// respond to the other client's request and cause a deadlock.
|
||||||
|
//
|
||||||
|
// This arrangement ensures we will attempt to process earlier messages first, but fall
|
||||||
|
// back to processing messages arrived later in the spirit of making progress.
|
||||||
|
let mut foreground_message_handlers = FuturesUnordered::new();
|
||||||
loop {
|
loop {
|
||||||
let next_message = incoming_rx.next().fuse();
|
let next_message = incoming_rx.next().fuse();
|
||||||
futures::pin_mut!(next_message);
|
futures::pin_mut!(next_message);
|
||||||
|
@ -408,30 +419,33 @@ impl Server {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
_ = foreground_message_handlers.next() => {}
|
||||||
message = next_message => {
|
message = next_message => {
|
||||||
if let Some(message) = message {
|
if let Some(message) = message {
|
||||||
let type_name = message.payload_type_name();
|
let type_name = message.payload_type_name();
|
||||||
let span = tracing::info_span!("receive message", %user_id, %login, %connection_id, %address, type_name);
|
let span = tracing::info_span!("receive message", %user_id, %login, %connection_id, %address, type_name);
|
||||||
async {
|
let span_enter = span.enter();
|
||||||
if let Some(handler) = this.handlers.get(&message.payload_type_id()) {
|
if let Some(handler) = this.handlers.get(&message.payload_type_id()) {
|
||||||
let notifications = this.notifications.clone();
|
let notifications = this.notifications.clone();
|
||||||
let is_background = message.is_background();
|
let is_background = message.is_background();
|
||||||
let handle_message = (handler)(this.clone(), message);
|
let handle_message = (handler)(this.clone(), message);
|
||||||
let handle_message = async move {
|
|
||||||
handle_message.await;
|
drop(span_enter);
|
||||||
if let Some(mut notifications) = notifications {
|
let handle_message = async move {
|
||||||
let _ = notifications.send(()).await;
|
handle_message.await;
|
||||||
}
|
if let Some(mut notifications) = notifications {
|
||||||
};
|
let _ = notifications.send(()).await;
|
||||||
if is_background {
|
|
||||||
executor.spawn_detached(handle_message);
|
|
||||||
} else {
|
|
||||||
handle_message.await;
|
|
||||||
}
|
}
|
||||||
|
}.instrument(span);
|
||||||
|
|
||||||
|
if is_background {
|
||||||
|
executor.spawn_detached(handle_message);
|
||||||
} else {
|
} else {
|
||||||
tracing::error!(%user_id, %login, %connection_id, %address, "no message handler");
|
foreground_message_handlers.push(handle_message);
|
||||||
}
|
}
|
||||||
}.instrument(span).await;
|
} else {
|
||||||
|
tracing::error!(%user_id, %login, %connection_id, %address, "no message handler");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
tracing::info!(%user_id, %login, %connection_id, %address, "connection closed");
|
tracing::info!(%user_id, %login, %connection_id, %address, "connection closed");
|
||||||
break;
|
break;
|
||||||
|
@ -440,6 +454,7 @@ impl Server {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
drop(foreground_message_handlers);
|
||||||
tracing::info!(%user_id, %login, %connection_id, %address, "signing out");
|
tracing::info!(%user_id, %login, %connection_id, %address, "signing out");
|
||||||
if let Err(error) = this.sign_out(connection_id).await {
|
if let Err(error) = this.sign_out(connection_id).await {
|
||||||
tracing::error!(%user_id, %login, %connection_id, %address, ?error, "error signing out");
|
tracing::error!(%user_id, %login, %connection_id, %address, ?error, "error signing out");
|
||||||
|
|
Loading…
Reference in a new issue