Skip to content

Conversation

@ziggie1984
Copy link
Collaborator

@ziggie1984 ziggie1984 commented Dec 7, 2025

Backports #10428

@gemini-code-assist
Copy link

Summary of Changes

Hello @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 potential deadlock and SQL transaction exhaustion issue by carefully reordering the acquisition of a mutex lock relative to database transactions. Previously, taking a mutex lock within a transaction could lead to a situation where all transactions became blocked, causing a deadlock. The changes ensure that the cacheMu read lock is acquired before the database transaction begins and released after it completes, establishing a consistent lock ordering and significantly improving the stability and reliability of database operations.

Highlights

  • Deadlock Prevention: Addresses a critical deadlock scenario where acquiring a mutex lock inside a database transaction could block all transactions waiting for the mutex, potentially leading to transaction exhaustion.
  • Consistent Lock Ordering: Ensures a consistent lock ordering (cacheMu -> DB) by acquiring the cacheMu read lock before initiating the database transaction, preventing race conditions and deadlocks.
  • Refactored Lock Management: The cacheMu.RLock() and cacheMu.RUnlock() calls are now managed outside the transaction execution blocks in fetchNextChanUpdateBatch and ChanUpdatesInHorizon functions, simplifying the locking logic within transactions.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a potential deadlock issue in both the kv_store and sql_store implementations by ensuring a consistent lock ordering. The change moves the acquisition of cacheMu's read lock to before the database transaction begins, preventing a situation where the database lock is held while waiting for the mutex, which could lead to a deadlock with write operations that acquire the locks in the opposite order. The implementation in both kv_store.go and sql_store.go is correct and effectively resolves the potential for transaction exhaustion and deadlocks. The code changes are clean, well-commented, and I have no further suggestions for improvement.

@saubyk saubyk added the back port candidate pr which should be back ported to last major release label Dec 7, 2025
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.
@ziggie1984 ziggie1984 force-pushed the backport/fix-sql-pool-exhaustion branch from 1831ffa to 0c7db1a Compare December 9, 2025 15:16
@yyforyongyu
Copy link
Member

Can you confirm the flake is unrelated? From this build, https://github.com/lightningnetwork/lnd/actions/runs/20068530441/job/57563704057?pr=10429

I see permanently failed with no route: no_route in the log.

@ziggie1984
Copy link
Collaborator Author

ziggie1984 commented Dec 10, 2025

yes they are unrelated, going to open a PR which fixes this race.

See: #10440

Copy link
Member

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM🥇

@yyforyongyu yyforyongyu merged commit 189e23a into lightningnetwork:v0.20.x-branch Dec 11, 2025
36 of 39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

back port candidate pr which should be back ported to last major release no-changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants