Commit 6ed79d9
Defer MonitorUpdatingPersister writes to flush()
Update MonitorUpdatingPersister and MonitorUpdatingPersisterAsync to
queue persist operations in memory instead of writing immediately to
disk. The Persist trait methods now return ChannelMonitorUpdateStatus::
InProgress and the actual writes happen when flush() is called.
This fixes a race condition that could cause channel force closures:
previously, if the node crashed after writing channel monitors but
before writing the channel manager, the monitors would be ahead of
the manager on restart. By deferring monitor writes until after the
channel manager is persisted (via flush()), we ensure the manager is
always at least as up-to-date as the monitors.
The flush() method takes a count parameter specifying how many queued
writes to flush. The background processor captures the queue size
before persisting the channel manager, then flushes exactly that many
writes afterward. This prevents flushing monitor updates that arrived
after the manager state was captured.
Key changes:
- Add PendingWrite enum with FullMonitor and Update variants for queued writes
- Add pending_writes queue to MonitorUpdatingPersisterAsyncInner
- Add pending_write_count() and flush(count) to Persist trait and ChainMonitor
- ChainMonitor::flush() calls channel_monitor_updated for each completed write
- Stale update cleanup happens in flush() after full monitor is written
- Call flush() in background processor after channel manager persistence
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>1 parent b4fb555 commit 6ed79d9
File tree
4 files changed
+314
-220
lines changed- lightning-background-processor/src
- lightning/src
- chain
- util
4 files changed
+314
-220
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1152 | 1152 | | |
1153 | 1153 | | |
1154 | 1154 | | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
1155 | 1160 | | |
1156 | 1161 | | |
1157 | 1162 | | |
| |||
1349 | 1354 | | |
1350 | 1355 | | |
1351 | 1356 | | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
1352 | 1366 | | |
1353 | 1367 | | |
1354 | 1368 | | |
| |||
1413 | 1427 | | |
1414 | 1428 | | |
1415 | 1429 | | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
1416 | 1440 | | |
1417 | 1441 | | |
1418 | 1442 | | |
| |||
1722 | 1746 | | |
1723 | 1747 | | |
1724 | 1748 | | |
| 1749 | + | |
| 1750 | + | |
| 1751 | + | |
1725 | 1752 | | |
1726 | 1753 | | |
1727 | 1754 | | |
| |||
1733 | 1760 | | |
1734 | 1761 | | |
1735 | 1762 | | |
| 1763 | + | |
| 1764 | + | |
| 1765 | + | |
| 1766 | + | |
| 1767 | + | |
| 1768 | + | |
| 1769 | + | |
| 1770 | + | |
| 1771 | + | |
| 1772 | + | |
1736 | 1773 | | |
1737 | 1774 | | |
1738 | 1775 | | |
| |||
1853 | 1890 | | |
1854 | 1891 | | |
1855 | 1892 | | |
| 1893 | + | |
| 1894 | + | |
| 1895 | + | |
| 1896 | + | |
| 1897 | + | |
| 1898 | + | |
| 1899 | + | |
| 1900 | + | |
| 1901 | + | |
| 1902 | + | |
| 1903 | + | |
| 1904 | + | |
1856 | 1905 | | |
1857 | 1906 | | |
1858 | 1907 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| |||
198 | 199 | | |
199 | 200 | | |
200 | 201 | | |
201 | | - | |
202 | | - | |
| 202 | + | |
203 | 203 | | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
210 | 208 | | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
211 | 219 | | |
212 | 220 | | |
213 | 221 | | |
| |||
272 | 280 | | |
273 | 281 | | |
274 | 282 | | |
275 | | - | |
276 | 283 | | |
277 | 284 | | |
278 | 285 | | |
| |||
320 | 327 | | |
321 | 328 | | |
322 | 329 | | |
323 | | - | |
324 | | - | |
| 330 | + | |
325 | 331 | | |
326 | 332 | | |
327 | 333 | | |
328 | 334 | | |
329 | 335 | | |
330 | 336 | | |
331 | 337 | | |
332 | | - | |
333 | | - | |
| 338 | + | |
334 | 339 | | |
335 | 340 | | |
336 | 341 | | |
337 | 342 | | |
338 | 343 | | |
339 | 344 | | |
340 | 345 | | |
341 | | - | |
342 | | - | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
343 | 352 | | |
344 | 353 | | |
345 | 354 | | |
| |||
440 | 449 | | |
441 | 450 | | |
442 | 451 | | |
443 | | - | |
444 | 452 | | |
445 | 453 | | |
446 | 454 | | |
| |||
450 | 458 | | |
451 | 459 | | |
452 | 460 | | |
453 | | - | |
454 | | - | |
| 461 | + | |
| 462 | + | |
455 | 463 | | |
456 | 464 | | |
457 | 465 | | |
| |||
742 | 750 | | |
743 | 751 | | |
744 | 752 | | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
745 | 777 | | |
746 | 778 | | |
747 | 779 | | |
| |||
1497 | 1529 | | |
1498 | 1530 | | |
1499 | 1531 | | |
1500 | | - | |
1501 | | - | |
1502 | | - | |
1503 | 1532 | | |
1504 | 1533 | | |
1505 | 1534 | | |
| |||
0 commit comments