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

Fix yielding send_payment handling with timeout #958

Open
wants to merge 1 commit into
base: savage-send-payment-yield
Choose a base branch
from
Open
Show file tree
Hide file tree
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
29 changes: 14 additions & 15 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,32 +328,31 @@ impl BreezServices {
self.notify_event_listeners(BreezEvent::payment_started(pending_payment.clone()))
.await?;

let pending_timeout_sec = req.pending_timeout_sec.unwrap_or(u64::MAX);
let (tx, rx) = std::sync::mpsc::channel();
let (tx, rx) = tokio::sync::oneshot::channel();
let cloned = self.clone();
tokio::spawn(async move {
let label = req.label;
let payee_pk = parsed_invoice.payee_pubkey.clone();
let bolt11 = parsed_invoice.bolt11.clone();

let payment_res = cloned
.node_api
.send_payment(
parsed_invoice.bolt11.clone(),
req.amount_msat,
req.label.clone(),
)
.send_payment(bolt11, req.amount_msat, label.clone())
.map_err(Into::into)
.await;
let result = cloned
.on_payment_completed(
parsed_invoice.payee_pubkey.clone(),
Some(parsed_invoice),
req.label,
payment_res,
)
.on_payment_completed(payee_pk, Some(parsed_invoice), label, payment_res)
.await;
let _ = tx.send(result);
});

match rx.recv_timeout(Duration::from_secs(pending_timeout_sec)) {
Ok(result) => result.map(SendPaymentResponse::from_payment),
let pending_timeout_sec = req.pending_timeout_sec.unwrap_or(u64::MAX);
match tokio::time::timeout(Duration::from_secs(pending_timeout_sec), rx).await {
Ok(Ok(result)) => result.map(SendPaymentResponse::from_payment),
Ok(Err(_)) => {
error!("The send_payment sender (tx) dropped without sending a message");
Err(SendPaymentError::payment_failed("Payment call interrupted"))
}
Err(_) => Ok(SendPaymentResponse::from_payment(pending_payment)),
}
}
Expand Down
7 changes: 7 additions & 0 deletions libs/sdk-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,13 @@ pub enum SendPaymentError {
#[error("Service connectivity: {err}")]
ServiceConnectivity { err: String },
}
impl SendPaymentError {
pub(crate) fn payment_failed(err: &str) -> Self {
Self::PaymentFailed {
err: err.to_string(),
}
}
}

impl From<anyhow::Error> for SendPaymentError {
fn from(err: anyhow::Error) -> Self {
Expand Down
Loading