[bug]: incoming update_add_htlc after a reconnect with a persisted shutdown gets a warning and a disconnect

Open
#11,603 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
40/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
payments

Research direction

Start in htlcswitch/link.go: resumeLink (around L4112-L4119) disables incoming adds right after reestablish, and the 'received add while link is flushing' check is around L4194-L4221. Run the reproducer TestAddAfterReconnectWithSavedShutdown from the issue (a new file such as htlcswitch/link_add_reconnect_test.go) with -v. Done when the saved=true case no longer produces a link failure or disconnect, while the saved=false case still behaves the same.

Written by the indexing model from the issue text.

Description

bug needs triage
Pre-Submission Checklist
  • I have searched the existing issues and believe this is a new bug.
  • I am not asking a question about how to use lnd, but reporting a bug (otherwise open a discussion).
LND Version

master 8cdbf85c0860 (2026-11-18), v0.21.8-beta and v0.20.8-beta

LND Configuration

Default.

Backend Version

None. The code runs inside lnd's own packages.

Backend Configuration

None.

OS/Distribution

Linux x86_64

Bug Details & Steps to Reproduce

On lnd master 8cdbf85c0860, after a reconnect with a persisted shutdown, resumeLink disables incoming adds right after the reestablish (link.go L4112-L4119), before the retransmitted shutdown can have reached the peer. An update_add_htlc that the peer sent before it read that shutdown then gets a warning and a disconnect ("received add while link is flushing", link.go L4194-L4221). Without the persisted shutdown, the same add is accepted. BOLT 2 says a node that has sent shutdown SHOULD fail to route any HTLC added after it.

Test, package htlcswitch. The link resumes with SyncStates set, as the peer sets it on a reconnection, with and without a persisted shutdown. The peer's channel_reestablish is delivered, then an update_add_htlc with its commitment signature:

// An incoming update_add_htlc after a reconnect, on a link whose shutdown was
// persisted, compared with the same link without a persisted shutdown.
package htlcswitch

import (
    "bytes"
    "strings"
    "testing"
    "time"

    "github.com/btcsuite/btclog/v2"
    "github.com/lightningnetwork/lnd/fn/v2"
    "github.com/lightningnetwork/lnd/lnwire"
    "github.com/stretchr/testify/require"
)

func addAfterReconnect(t *testing.T, savedShutdown bool) {
    h, err := newSingleLinkTestHarness(t, 1_000_000, 10_000)
    require.NoError(t, err)

    link := h.aliceLink.(*channelLink)
    failures := make(chan LinkFailureError, 1)
    link.cfg.OnChannelFailure = func(_ lnwire.ChannelID,
        _ lnwire.ShortChannelID, e LinkFailureError) {

        failures <- e
    }

    // The link resumes with a channel_reestablish exchange, as the peer
    // sets it up for a link on a reconnection.
    logBuf := &bytes.Buffer{}
    link.log = btclog.NewSLogger(btclog.NewDefaultHandler(
        logBuf, btclog.WithNoTimestamp(),
    ))
    link.cfg.SyncStates = true
    if savedShutdown {
        link.cfg.PreviouslySentShutdown = fn.Some(lnwire.Shutdown{
            ChannelID: link.ChanID(),
            Address: append([]byte{0x00, 0x14}, make([]byte, 20)...),
        })
    }
    require.NoError(t, h.start())

    sent := link.cfg.Peer.(*mockPeer).sentMsgs
    var names []string
    drain := func(wait time.Duration) {
        for {
            select {
            case m := <-sent:
                names = append(names, m.MsgType().String())
            case <-time.After(wait):
                return
            }
        }
    }

    // The peer's channel_reestablish, then an update_add_htlc that it sent
    // before it read our shutdown.
    bobSync, err := h.bobChannel.State().ChanSyncMsg()
    require.NoError(t, err)
    link.HandleChannelUpdate(bobSync)
    drain(time.Second)

    amt, timelock, hops := generateHops(
        lnwire.NewMSatFromSatoshis(10_000), testStartingHeight, link,
    )
    blob, err := generateRoute(hops...)
    require.NoError(t, err)
    invoice, htlc, _, err := generatePayment(amt, amt, timelock, blob)
    require.NoError(t, err)
    require.NoError(t, link.cfg.Registry.(*mockInvoiceRegistry).AddInvoice(
        t.Context(), *invoice, htlc.PaymentHash,
    ))
    htlc.ID = 0
    _, err = h.bobChannel.AddHTLC(htlc, nil)
    require.NoError(t, err)
    link.HandleChannelUpdate(htlc)

    // The peer then signs, as it does after an add.
    sigs, err := h.bobChannel.SignNextCommitment(t.Context())
    require.NoError(t, err)
    link.HandleChannelUpdate(&lnwire.CommitSig{
        CommitSig: sigs.CommitSig, HtlcSigs: sigs.HtlcSigs,
    })

    select {
    case e := <-failures:
        t.Logf("shutdown saved=%v: link failure: %v, disconnect %v, "+
            "warning %v", savedShutdown, e.Error(),
            e.FailureAction == LinkFailureDisconnect, e.Warning)
        for _, l := range strings.Split(logBuf.String(), "\n") {
            if strings.Contains(l, "while link is flushing") {
                t.Logf("shutdown saved=%v: link log: %s", savedShutdown, l)
            }
        }
    case <-time.After(2 * time.Second):
        t.Logf("shutdown saved=%v: no link failure", savedShutdown)
    }
    drain(time.Second)
    t.Logf("shutdown saved=%v: link sent %v", savedShutdown, names)
}

func TestAddAfterReconnectWithSavedShutdown(t *testing.T) {
    t.Run("no saved shutdown", func(t *testing.T) { addAfterReconnect(t, false) })
    t.Run("saved shutdown", func(t *testing.T) { addAfterReconnect(t, true) })
}

Run go test -count=1 -run TestAddAfterReconnectWithSavedShutdown -v ./htlcswitch/. The output is the same on master, v0.21.8-beta and v0.20.8-beta:

    link_add_reconnect_test.go:97: shutdown saved=false: no link failure
    link_add_reconnect_test.go:100: shutdown saved=false: link sent [ChannelReestablish ChannelReady RevokeAndAck CommitSig]
    link_add_reconnect_test.go:88: shutdown saved=true: link failure: invalid update, disconnect true, warning true
    link_add_reconnect_test.go:93: shutdown saved=true: link log: [ERR]: failing link: received add while link is flushing with error: invalid update
    link_add_reconnect_test.go:93: shutdown saved=true: link log: [ERR]: failed to process remote UpdateAddHTLC: received add while link is flushing
    link_add_reconnect_test.go:100: shutdown saved=true: link sent [ChannelReestablish ChannelReady Shutdown]
Expected Behavior

The add is accepted and failed back, as BOLT 2 describes for HTLCs added after the node sent shutdown.

Debug Information

No response

Environment

No response

Dominant language
Go
Stars
8.2k
Forks
2.3k
Avg merge
1d 11h
Merged PRs (30d)
103

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from lightningnetwork/lnd

All issues in lightningnetwork/lnd

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.