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

Use spawn_blocking when processing synedrion messages #1285

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions crates/protocol/src/execute_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use synedrion::{
AuxInfo, KeyResharingInputs, KeyShare, NewHolder, OldHolder, PrehashedMessage,
RecoverableSignature, ThresholdKeyShare,
};
use tokio::sync::mpsc;
use tokio::{sync::mpsc, task::spawn_blocking};

use crate::{
errors::{GenericProtocolError, ProtocolExecutionErr},
Expand Down Expand Up @@ -100,7 +100,7 @@ where
let tx = tx.clone();
let my_id = my_id.clone();
let destination = destination.clone();
tokio::spawn(async move {
spawn_blocking(move || {
session_arc
.make_message(&mut OsRng, &destination)
.map(|(message, artifact)| {
Expand All @@ -125,7 +125,7 @@ where
// Process cached messages
let join_handles = cached_messages.into_iter().map(|preprocessed| {
let session_arc = session_arc.clone();
tokio::spawn(async move { session_arc.process_message(&mut OsRng, preprocessed) })
spawn_blocking(move || session_arc.process_message(&mut OsRng, preprocessed))
});

for result in try_join_all(join_handles).await? {
Expand Down Expand Up @@ -154,7 +154,8 @@ where
let tx = process_tx.clone();
tokio::spawn(async move {
let result = session_arc.process_message(&mut OsRng, preprocessed);
if tx.send(result).await.is_err() {

if futures::executor::block_on(tx.send(result)).is_err() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here we have the issue that the channel is async but we are now in a sync block. However because tx.send is likely to be considerably faster than session.process_message, i think it still makes sense to do this.

The alternative would be to use std::sync::mpsc rather than tokio::sync::mpsc, but then we have the problem that we can't use the receiver with tokio::select!.

tracing::error!("Protocol finished before message processing result sent");
}
});
Expand Down