Skip to content

Commit c98711a

Browse files
committed
lightningd: don't crash if peer manages to spend onchain HTLC after we've abandoned upstream.
Since we now abandon HTLCs to avoid breaking a channel (in limited circumstances), the peer can take the funds if they beat us to spend the HTLC before we timeout. This is extremely unusual, but finally happend in CI. First it fails our internal sanity checks, but then it would fail when we tried to fulfill an already-failed HTLC: ``` **BROKEN** lightningd: fulfill_our_htlc_out:Output preimage, input failonion? **BROKEN** lightningd: FATAL SIGNAL 6 (version f82fedb-modded) **BROKEN** lightningd: backtrace: common/daemon.c:38 (send_backtrace) 0x55c09b04f0b5 **BROKEN** lightningd: backtrace: common/daemon.c:75 (crashdump) 0x55c09b04f247 **BROKEN** lightningd: backtrace: ./signal/../sysdeps/unix/sysv/linux/x86_64/libc_sigaction.c:0 ((null)) 0x7f607463551f **BROKEN** lightningd: backtrace: ./nptl/pthread_kill.c:44 (__pthread_kill_implementation) 0x7f6074689a7c **BROKEN** lightningd: backtrace: ./nptl/pthread_kill.c:78 (__pthread_kill_internal) 0x7f6074689a7c **BROKEN** lightningd: backtrace: ./nptl/pthread_kill.c:89 (__GI___pthread_kill) 0x7f6074689a7c **BROKEN** lightningd: backtrace: ../sysdeps/posix/raise.c:26 (__GI_raise) 0x7f6074635475 **BROKEN** lightningd: backtrace: ./stdlib/abort.c:79 (__GI_abort) 0x7f607461b7f2 **BROKEN** lightningd: backtrace: lightningd/log.c:1016 (fatal_vfmt) 0x55c09afdb7cb **BROKEN** lightningd: backtrace: lightningd/log.c:1026 (fatal) 0x55c09afdb880 **BROKEN** lightningd: backtrace: lightningd/htlc_end.c:87 (corrupt) 0x55c09afc9472 **BROKEN** lightningd: backtrace: lightningd/htlc_end.c:207 (htlc_out_check) 0x55c09afc9c6b **BROKEN** lightningd: backtrace: lightningd/peer_htlcs.c:1451 (fulfill_our_htlc_out) 0x55c09b004dd7 **BROKEN** lightningd: backtrace: lightningd/peer_htlcs.c:1526 (onchain_fulfilled_htlc) 0x55c09b0050fe **BROKEN** lightningd: backtrace: lightningd/onchain_control.c:313 (handle_extracted_preimage) 0x55c09afdf9f8 **BROKEN** lightningd: backtrace: lightningd/onchain_control.c:1423 (onchain_msg) 0x55c09afe2da9 **BROKEN** lightningd: backtrace: lightningd/subd.c:557 (sd_msg_read) 0x55c09b019ac8 ``` Signed-off-by: Rusty Russell <[email protected]>
1 parent 5f20728 commit c98711a

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lightningd/htlc_end.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ struct htlc_out *htlc_out_check(const struct htlc_out *hout,
203203
return corrupt(abortstr,
204204
"Output failmsg, input preimage");
205205
} else if (hout->preimage) {
206-
if (hout->in->failonion)
207-
return corrupt(abortstr,
208-
"Output preimage, input failonion");
206+
/* If we abandoned the HTLC to save the incoming channel,
207+
* (see consider_failing_incoming), hout->in->failonion
208+
* will be set! */
209209
if (hout->in->badonion)
210210
return corrupt(abortstr,
211211
"Output preimage, input badonion");

lightningd/peer_htlcs.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,11 +1464,18 @@ static void fulfill_our_htlc_out(struct channel *channel, struct htlc_out *hout,
14641464
if (hout->am_origin)
14651465
payment_succeeded(ld, &hout->payment_hash, hout->partid, hout->groupid, preimage);
14661466
else if (hout->in) {
1467-
fulfill_htlc(hout->in, preimage);
1468-
wallet_forwarded_payment_add(ld->wallet, hout->in,
1469-
FORWARD_STYLE_TLV,
1470-
channel_scid_or_local_alias(hout->key.channel), hout,
1471-
FORWARD_SETTLED, 0);
1467+
/* Did we abandon the incoming? Oops! */
1468+
if (hout->in->failonion) {
1469+
/* FIXME: Accounting? */
1470+
log_unusual(channel->log, "FUNDS LOSS of %s: peer took funds onchain before we could time out the HTLC, but we abandoned incoming HTLC to save the incoming channel",
1471+
fmt_amount_msat(tmpctx, hout->msat));
1472+
} else {
1473+
fulfill_htlc(hout->in, preimage);
1474+
wallet_forwarded_payment_add(ld->wallet, hout->in,
1475+
FORWARD_STYLE_TLV,
1476+
channel_scid_or_local_alias(hout->key.channel), hout,
1477+
FORWARD_SETTLED, 0);
1478+
}
14721479
}
14731480
}
14741481

0 commit comments

Comments
 (0)