Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/release-notes/release-notes-0.20.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
addresses](https://github.com/lightningnetwork/lnd/pull/10341) were added to
the node announcement and `getinfo` output.

* [Fix potential sql tx exhaustion
issue](https://github.com/lightningnetwork/lnd/pull/10428) in LND which might
happen when running postgres with a limited number of connections configured.

# New Features

## Functional Enhancements
Expand Down
14 changes: 8 additions & 6 deletions graph/db/kv_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2116,6 +2116,13 @@ func (c *KVStore) fetchNextChanUpdateBatch(
batch []ChannelEdge
hasMore bool
)

// Acquire read lock before starting transaction to ensure
// consistent lock ordering (cacheMu -> DB) and prevent
// deadlock with write operations.
c.cacheMu.RLock()
defer c.cacheMu.RUnlock()

err := kvdb.View(c.db, func(tx kvdb.RTx) error {
edges := tx.ReadBucket(edgeBucket)
if edges == nil {
Expand Down Expand Up @@ -2195,9 +2202,7 @@ func (c *KVStore) fetchNextChanUpdateBatch(
continue
}

// Before we read the edge info, we'll see if this
// element is already in the cache or not.
c.cacheMu.RLock()
// Check cache (we already hold shared read lock).
if channel, ok := c.chanCache.get(chanIDInt); ok {
state.edgesSeen[chanIDInt] = struct{}{}

Expand All @@ -2208,11 +2213,8 @@ func (c *KVStore) fetchNextChanUpdateBatch(

indexKey, _ = updateCursor.Next()

c.cacheMu.RUnlock()

continue
}
c.cacheMu.RUnlock()

// The edge wasn't in the cache, so we'll fetch it along
// w/ the edge policies and nodes.
Expand Down
12 changes: 10 additions & 2 deletions graph/db/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,11 @@ func (s *SQLStore) ChanUpdatesInHorizon(startTime, endTime time.Time,
for hasMore {
var batch []ChannelEdge

// Acquire read lock before starting transaction to
// ensure consistent lock ordering (cacheMu -> DB) and
// prevent deadlock with write operations.
s.cacheMu.RLock()

err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(),
func(db SQLQueries) error {
//nolint:ll
Expand Down Expand Up @@ -1178,11 +1183,11 @@ func (s *SQLStore) ChanUpdatesInHorizon(startTime, endTime time.Time,
continue
}

s.cacheMu.RLock()
// Check cache (we already hold
// shared read lock).
channel, ok := s.chanCache.get(
chanIDInt,
)
s.cacheMu.RUnlock()
if ok {
hits++
total++
Expand Down Expand Up @@ -1216,6 +1221,9 @@ func (s *SQLStore) ChanUpdatesInHorizon(startTime, endTime time.Time,
)
})

// Release read lock after transaction completes.
s.cacheMu.RUnlock()

if err != nil {
log.Errorf("ChanUpdatesInHorizon "+
"batch error: %v", err)
Expand Down
Loading