onchaind: Close a racy onchain resolution
What changed, and why it matters
This fix addresses a timing bug in Core Lightning's on-chain dispute handler. When a payment hash preimage arrives late from a downstream peer, the software could have already marked the related funds as 'ignored' because the upstream timeout had passed. The old code stopped processing once it saw an existing resolution, leaving the funds stuck. The fix clears the prior 'ignored' resolution so the late preimage can still be used to claim the funds. It is a recoverable race condition, not a direct theft of funds, but it could delay or complicate recovery of HTLC funds during on-chain settlement.
Apply the patch. Nodes running earlier versions should upgrade if they route HTLCs that may settle on-chain, especially where downstream preimages can arrive near upstream timeout deadlines. Monitor onchaind logs for 'HTLC already resolved ... when we found preimage' to detect historical occurrences.
Security signals we found
Race condition between upstream HTLC timeout and downstream preimage
Prior resolution not cleared, potentially stranding HTLC funds
Fix clears both proposal and resolved state when preimage arrives
Changelog labels this as a fixed race in onchaind
Evidence from the diff
In onchaind/onchaind.c, handle_preimage() iterates tracked outputs to apply a newly learned preimage. Previously, if an output already had a non-null outs[i]->resolved, the function logged a broken status and returned immediately. Because an upstream HTLC timeout may have set the resolution to IGNORE, a later downstream preimage could not override it, leaving the output unresolved. The patch changes the early return to continue so that subsequent outputs are still processed, and adds tal_free(outs[i]->resolved) alongside the existing tal_free(outs[i]->proposal) so the prior resolution is discarded and the preimage-based claim can be installed. This closes a race between upstream timeout and downstream preimage propagation.
Changed components
onchaind/onchaind.chandle_preimage()tracked_output resolution stateInspect captured patch +2 / −1
### onchaind/onchaind.c
@@ -1507,7 +1507,7 @@ static void handle_preimage(struct tracked_output **outs,
status_broken("HTLC already resolved by %s"
" when we found preimage",
tx_type_name(outs[i]->resolved->tx_type));
- return;
+ continue;
}
}
@@ -1517,6 +1517,7 @@ static void handle_preimage(struct tracked_output **outs,
/* Discard any previous resolution. Could be a timeout,
* could be due to multiple identical rhashes in tx. */
outs[i]->proposal = tal_free(outs[i]->proposal);
+ outs[i]->resolved = tal_free(outs[i]->resolved);
/* BOLT #5:
*Why this scored 57/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.