Skip to content

Forward channel closures and failed opens to the LSPS2 service - #1233

Open
tnull wants to merge 2 commits into
lightningdevkit:mainfrom
tnull:2026-11-lsps2-forward-channel-closed
Open

tnull wants to merge 2 commits into
lightningdevkit:mainfrom
tnull:2026-11-lsps2-forward-channel-closed

Conversation

@tnull

@tnull tnull commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

lightning-liquidity expects LSPS2 services to forward Event::ChannelClosed
and to report failed JIT channel opens. We did neither:

  • Closed JIT channels were never pruned, so the service kept (and persisted)
    the state of every client that ever paid for a channel. That state also
    counts towards the service's global peer limit.
  • If a JIT channel open failed, the intercepted HTLCs were held until they
    timed out, and the JIT state stayed stuck waiting for the channel, so
    retrying the same invoice could never trigger another open.

This PR fixes both, one commit each:

  1. Forward ChannelClosed to LSPS2ServiceHandler::channel_closed.
  2. Call LSPS2ServiceHandler::channel_open_failed when we can't open the
    channel (peer not connected, insufficient on-chain funds, create_channel
    failing) and when the channel closes before becoming ready. The held HTLCs
    are failed back right away and the payer can retry.

`lightning-liquidity` requires `Event::ChannelClosed` to be forwarded
to the LSPS2 service handler, which we never did. The service hence
kept the state of every JIT channel that ever got paid for, and with
it the persisted state of the respective client, forever. That state
also counts towards the service's global peer limit.

We now hand closed channels to the LSPS2 service, allowing it to prune
the state of JIT channels that are gone.

Co-Authored-By: HAL 9000
When we couldn't open a JIT channel as an LSPS2 service, or when the
channel closed before becoming ready, we never told
`lightning-liquidity` about it. The intercepted HTLCs were therefore
held until they timed out, and as the JIT channel state stayed stuck
waiting for the channel, no later payment to the same invoice could
trigger another attempt.

We now report failed channel opens to the LSPS2 service, which fails
the held HTLCs back right away and lets the payer retry.

Co-Authored-By: HAL 9000
@tnull tnull added this to the 0.8 milestone Nov 12, 2026
@tnull
tnull requested a review from jkczyz November 12, 2026 08:49
@ldk-reviews-bot

ldk-reviews-bot commented Nov 12, 2026 •

Copy link
Copy Markdown

👋 Thanks for assigning @jkczyz as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@tnull
tnull force-pushed the 2026-11-lsps2-forward-channel-closed branch from 049f1a4 to 3a3dafb Compare November 12, 2026 08:49
// A JIT channel closing before it became ready is a failed channel open. This errors
// for any channel that isn't such a JIT channel, which is the common case.
if let Err(e) = lsps2_service_handler
.channel_open_failed(counterparty_node_id, user_channel_id)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤖 If add_event fails after this, the ChannelClosed is replayed and we call channel_open_failed again. If the payer re-sent in the meantime and a second channel is pending, the replay fails its HTLCs and resets the state. That channel's channel_ready then errors, so we fund a channel with nothing to forward.

The window is narrow, but a cheap guard would be to skip this when list_channels_with_counterparty still has another channel with the same user_channel_id.

);
}

if let Err(e) = lsps2_service_handler.channel_closed(channel_id).await {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤖 lightning-liquidity's channel_closed only prunes JIT channels in PaymentForwarded. A JIT channel that closes after channel_ready but before the payment is forwarded (PendingPaymentForward / PendingPayment) is still never pruned, and channel_open_failed rejects it too. Is that intended, or a follow-up upstream?

let Some(peer) = peer_manager.peer_by_node_id(&their_network_key) else {
log_error!(
self.logger,
"Failed to open LSPS2 channel to {} due to peer not being not connected.",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pre-existing but need to drop one "not" here.

// TODO: We fail the payment back here. Eventually we will need to remember
// the pending requests and regularly retry opening the channel until we
// succeed.
self.fail_channel_open(&their_network_key, user_channel_id).await;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤖 The insufficient-funds path is tested, but this one isn't, and it's easy to trigger. For example: stop the client, pay, expect PaymentFailed, then restart the client and check that a retry succeeds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants