graphdb: fix potential sql tx exhaustion#10428
graphdb: fix potential sql tx exhaustion#10428yyforyongyu merged 2 commits intolightningnetwork:masterfrom
Conversation
Summary of ChangesHello @ziggie1984, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical issue where taking a mutex lock inside a database transaction could lead to deadlocks. By strategically moving the mutex acquisition to occur before the transaction begins and releasing it after the transaction completes, the changes establish a clear and consistent locking hierarchy. This ensures that database operations and application-level caching mechanisms interact without contention, thereby enhancing system stability and preventing transaction exhaustion. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request addresses a potential deadlock issue by changing the lock acquisition order in kv_store.go and sql_store.go. The changes ensure that the cacheMu read lock is taken before initiating a database transaction, which resolves the transaction exhaustion problem. The implementation is correct and clean in both files, using defer where appropriate in kv_store.go and manual lock/unlock inside the loop in sql_store.go. The code is now more robust against deadlocks. Great work!
|
A more general description of the problem: |
|
the relevant pg log from the affected noderunner is here: https://gist.github.com/xmrk-btc/f2f1c58c30ace6e109525292fc9d72dd |
|
I reported the problem, it occurred with lnd 0.20.0, I have been running for 36 hours lnd 0.20.0 + cherry-picked commit 1831ffa . No issues so far, and without the fix, lnd got stuck in less than 24 hours. Also, my current pg_stat_activity doesn't show any stuck GetChannelsByPolicyLastUpdateRange query. So lgtm. |
yyforyongyu
left a comment
There was a problem hiding this comment.
Thanks! Pending CI, otherwise good to go!
We should avoid taking the lock of a mutex inside transaction. Currently we also take this lock in other places and there is a chance that in case the application lock aquires the lock but all transactions are already blocked waiting for the mutex to unlock, we end up in a deadlock.
6829f57 to
f289e49
Compare
yyforyongyu
left a comment
There was a problem hiding this comment.
LGTM 🔨
Double checked the CI failure, and it's not related,
=========> tranche 2 finished, tested 33 cases, mined blocks: 55
--- FAIL: TestLightningNetworkDaemon (417.60s)
harness_setup.go:25: Setting up HarnessTest...
harness_setup.go:38: Prepare the miner and mine blocks to activate segwit...
harness_setup.go:46: Connecting the miner at 127.0.0.1:10023 with the chain backend...
--- FAIL: TestLightningNetworkDaemon/tranche02/71-of-260/btcd/multihop-local_claim_outgoing_htlc_anchor_zero_conf (185.72s)
harness_node.go:395: Starting node (name=Alice) with PID=5400
harness_node.go:395: Starting node (name=Bob) with PID=8916
harness_node.go:395: Starting node (name=Carol) with PID=2552
lnd_multi-hop_force_close_test.go:467: Bob's timelock to_local output=3, timelock on second stage htlc=3
harness_assertion.go:1646:
Error Trace: D:/a/lnd/lnd/lntest/harness_assertion.go:1646
D:/a/lnd/lnd/itest/lnd_multi-hop_force_close_test.go:528
D:/a/lnd/lnd/itest/lnd_multi-hop_force_close_test.go:238
D:/a/lnd/lnd/lntest/harness.go:315
D:/a/lnd/lnd/itest/lnd_test.go:130
Error: Received unexpected error:
payment: 8346cdfea1123734df610273a835877db24ac3edca93eef5d2e366527056f1cc failure reason not match, want FAILURE_REASON_NO_ROUTE(2) got FAILURE_REASON_TIMEOUT(1)
Test: TestLightningNetworkDaemon/tranche02/71-of-260/btcd/multihop-local_claim_outgoing_htlc_anchor_zero_conf
Messages: timeout checking payment failure reason
harness.go:393: finished test: multihop-local_claim_outgoing_htlc_anchor_zero_conf, start height=493, end height=527, mined blocks=34
harness.go:352: test failed, skipped cleanup
harness_rpc.go:100:
Error Trace: D:/a/lnd/lnd/lntest/rpc/harness_rpc.go:100
D:/a/lnd/lnd/lntest/rpc/lnd.go:188
D:/a/lnd/lnd/lntest/harness_assertion.go:1557
D:/a/lnd/lnd/lntest/harness_assertion.go:1631
D:/a/lnd/lnd/lntest/wait/wait.go:51
D:/a/lnd/lnd/lntest/wait/wait.go:27
C:/hostedtoolcache/windows/go/1.25.3/x64/src/runtime/asm_amd64.s:1693
Error: Received unexpected error:
rpc error: code = Canceled desc = context canceled
Messages: Alice: failed to call ListPayments
lnd_test.go:138: Failure time: 2025-12-09 11:04:46.760
FAIL
We should avoid taking the lock of a mutex inside transaction.
Currently we also take this lock in other places and there is a
chance that in case the application lock aquires the lock but
all transactions are already blocked waiting for the mutex to
unlock, we end up in a deadlock.