Skip to content

instantout: harden instant out error handling#1144

Draft
hieblmi wants to merge 4 commits into
lightninglabs:masterfrom
hieblmi:instantout-hardening
Draft

instantout: harden instant out error handling#1144
hieblmi wants to merge 4 commits into
lightninglabs:masterfrom
hieblmi:instantout-hardening

Conversation

@hieblmi
Copy link
Copy Markdown
Collaborator

@hieblmi hieblmi commented May 20, 2026

Summary

This PR applies small hardening fixes for the instant out implementation:

  • validate peer MuSig nonce/signature slice lengths before indexing
  • store active swaps under the real swap hash after initialization
  • treat closed payment streams and nil payment errors as failures
  • use fresh timeout contexts for reservation unlock and server cancel cleanup

Why

The previous logic could panic on malformed peer MuSig data, keep active swaps under the zero hash, return nil for failed payment stream paths, or skip cleanup when the caller context was already canceled.

Validation

  • go test ./instantout/...

hieblmi added 4 commits May 20, 2026 21:48
Peer MuSig data was indexed by reservation position before validating counts.

Short server responses could panic loopd instead of returning an error.
New instant outs were added to the active map before the swap hash existed.

The FSM was stored under the zero hash, making real-hash lookups fail.
Closed payment channels yielded zero values or nil errors in failure paths.

Return explicit errors so the observer cannot report a failed swap as success.
Cleanup contexts were derived from caller contexts that may be canceled.

Use independent timeout contexts for reservation unlocks and server cancel.
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces several hardening improvements to the instant out implementation. The changes focus on increasing robustness by adding input validation, refining state management during swap initialization, and ensuring reliable resource cleanup by decoupling background tasks from potentially canceled request contexts.

Highlights

  • MuSig2 Validation: Added length validation for MuSig2 sessions, nonces, and signatures to prevent potential panics when processing malformed peer data.
  • State Management: Refactored active swap registration in the manager to ensure swaps are correctly indexed by their hash after initialization.
  • Error Handling: Improved payment stream monitoring by explicitly handling closed channels and nil errors as failures.
  • Context Management: Updated reservation cleanup and cancellation logic to use fresh background contexts, ensuring cleanup operations proceed even if the original request context is canceled.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request improves the robustness of the instant-out FSM by refining channel closure handling, ensuring cleanup tasks use a fresh context, and adding validation for MuSig2 sessions. It also optimizes locking logic in the manager. Feedback suggests that treating closed payment channels as terminal failures in PollPaymentAcceptedAction may be premature, recommending instead that they be ignored to allow server polling to determine the final state.

Comment thread instantout/actions.go
Comment on lines +247 to +252
case payRes, ok := <-payChan:
if !ok {
return f.handleErrorAndUnlockReservations(
ctx, errors.New("payment status channel closed"),
)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Failing the swap when payChan is closed might be premature. In many cases, the payment status channel is closed by the router client after a terminal state (like success) is reached. Since the FSM continues to poll the server for acceptance via PollPaymentAccepted, a closed channel should simply be ignored rather than treated as a failure. Treating it as a failure could abort a swap that has actually succeeded locally but hasn't been acknowledged by the server yet.

		case payRes, ok := <-payChan:
			if !ok {
				payChan = nil
				continue
			}

Comment thread instantout/actions.go
Comment on lines +261 to +266
case err, ok := <-paymentErrChan:
if !ok {
err = errors.New("payment error channel closed")
} else if err == nil {
err = errors.New("payment error channel returned nil")
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Similar to payChan, the paymentErrChan may be closed normally when the payment stream ends. Treating a closed channel as a terminal failure could abort a swap that has actually succeeded or is still being processed by the server. It is safer to set the channel to nil to stop selecting on it and allow the server polling to determine the final outcome.

Suggested change
case err, ok := <-paymentErrChan:
if !ok {
err = errors.New("payment error channel closed")
} else if err == nil {
err = errors.New("payment error channel returned nil")
}
case err, ok := <-paymentErrChan:
if !ok {
paymentErrChan = nil
continue
} else if err == nil {
err = errors.New("payment error channel returned nil")
}

@hieblmi hieblmi changed the title Harden instant out error handling instantout: harden instant out error handling May 20, 2026
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.

1 participant