Commit 50cad71
committed
channeldb: fix race condition in link node pruning
This commit fixes a critical race condition in MarkChanFullyClosed and
pruneLinkNode where link nodes could be incorrectly deleted despite
having pending or open channels.
The race occurred because the check for open channels and the link node
deletion happened in separate database transactions:
Thread A: TX1 checks open channels → [] (empty)
Thread A: TX1 commits
Thread B: Opens new channel with same peer
Thread A: TX2 deletes link node (using stale data)
Result: Link node deleted despite pending channel existing
This creates a TOCTOU (time-of-check to time-of-use) vulnerability where
database state changes between reading the channel count and deleting
the node.
Fix for MarkChanFullyClosed:
- Move link node deletion into the same transaction as the channel
closing check, making the check-and-delete operation atomic
Fix for pruneLinkNode:
- Add double-check within the write transaction to verify no channels
were opened since the caller's initial check
- Maintains performance by keeping early return for common case
- Prevents deletion if channels exist at delete time
This ensures the invariant: "link node exists iff channels exist"
is never violated, preventing database corruption and potential
connection issues.1 parent 91423ee commit 50cad71
1 file changed
Lines changed: 57 additions & 26 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1363 | 1363 | | |
1364 | 1364 | | |
1365 | 1365 | | |
1366 | | - | |
1367 | | - | |
1368 | | - | |
1369 | | - | |
1370 | | - | |
| 1366 | + | |
1371 | 1367 | | |
1372 | 1368 | | |
1373 | 1369 | | |
| |||
1413 | 1409 | | |
1414 | 1410 | | |
1415 | 1411 | | |
1416 | | - | |
1417 | | - | |
1418 | | - | |
1419 | | - | |
| 1412 | + | |
| 1413 | + | |
1420 | 1414 | | |
1421 | 1415 | | |
1422 | 1416 | | |
1423 | | - | |
| 1417 | + | |
1424 | 1418 | | |
1425 | 1419 | | |
1426 | | - | |
1427 | | - | |
1428 | | - | |
1429 | | - | |
1430 | | - | |
1431 | | - | |
1432 | | - | |
1433 | | - | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
1434 | 1437 | | |
1435 | | - | |
1436 | | - | |
1437 | | - | |
| 1438 | + | |
| 1439 | + | |
1438 | 1440 | | |
1439 | 1441 | | |
1440 | 1442 | | |
1441 | | - | |
1442 | | - | |
| 1443 | + | |
| 1444 | + | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + | |
| 1448 | + | |
1443 | 1449 | | |
1444 | 1450 | | |
1445 | 1451 | | |
| 1452 | + | |
1446 | 1453 | | |
1447 | 1454 | | |
1448 | 1455 | | |
1449 | 1456 | | |
1450 | | - | |
1451 | | - | |
| 1457 | + | |
| 1458 | + | |
| 1459 | + | |
| 1460 | + | |
| 1461 | + | |
| 1462 | + | |
| 1463 | + | |
| 1464 | + | |
| 1465 | + | |
| 1466 | + | |
| 1467 | + | |
| 1468 | + | |
| 1469 | + | |
| 1470 | + | |
| 1471 | + | |
| 1472 | + | |
| 1473 | + | |
| 1474 | + | |
| 1475 | + | |
| 1476 | + | |
| 1477 | + | |
| 1478 | + | |
| 1479 | + | |
| 1480 | + | |
| 1481 | + | |
1452 | 1482 | | |
1453 | | - | |
| 1483 | + | |
| 1484 | + | |
1454 | 1485 | | |
1455 | 1486 | | |
1456 | 1487 | | |
| |||
0 commit comments