fix: transactions getting permanently stuck as Pending after a node crash#1507
Open
Aman-Cool wants to merge 4 commits intohyperledger-labs:mainfrom
Open
fix: transactions getting permanently stuck as Pending after a node crash#1507Aman-Cool wants to merge 4 commits intohyperledger-labs:mainfrom
Aman-Cool wants to merge 4 commits intohyperledger-labs:mainfrom
Conversation
…estart RestoreTMS now checks tokenDB before re-registering a finality listener. If TransactionExists returns true, the node crashed after tokens.Append succeeded but before ttxDB.SetStatus(Confirmed) ran. The status is healed directly instead of waiting for Fabric block re-delivery, which is not guaranteed. Extracts the logic into recoverCommittedPending (narrow interfaces) and adds five unit tests covering the happy path, normal restart, and all error fallbacks. Signed-off-by: Aman-Cool <aman017102007@gmail.com>
6cb1458 to
691e428
Compare
Contributor
Author
|
Hey @adecaro, found a nasty silent one; if the node crashes between the two DB writes in Fix hooks into the existing Would love a second pair of eyes from someone who knows the commit pipeline well :) |
691e428 to
d2e0dba
Compare
Contributor
|
Hi @Aman-Cool , please run |
Signed-off-by: Aman-Cool <aman017102007@gmail.com>
d2e0dba to
dd7d1d6
Compare
Signed-off-by: Aman-Cool <aman017102007@gmail.com>
79fcb49 to
afee84a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
So I was digging into the commit pipeline and found something that's been quietly causing problems in production deployments.
When a token transaction gets confirmed on-chain, the finality listener does two things in sequence: first it writes all the UTXO changes to
tokenDB(new outputs created, spent inputs deleted), and then it updates the transaction's status inttxDBfromPendingtoConfirmed. These are two completely separate SQL transactions with nothing tying them together.Which means if your node dies; OOM kill, power loss, someone tripping over a cable, whatever; at exactly the wrong moment between those two writes, you end up in a state where the token balances in
tokenDBare perfectly correct butttxDBstill shows the transaction asPending. Forever. Because the in-memory retry runner that would've finished the job died with the process, and the one-shot finality listener was already consumed and never gets re-registered.The practical fallout is pretty bad. Your wallet shows the right balance (tokens are there), but any query against transaction history says the transaction never finished. Audit reports flag it as unresolved. If you have any retry logic polling for
Confirmedstatus it just spins indefinitely. And there's no alarm, no automatic recovery, no way to know this happened unless you're manually cross-referencing two databases.The fix lives in
RestoreTMS, which already runs on startup and iterates over everyPendingtransaction to re-register finality listeners. Before handing each one off to the delivery service, we now do a quick check: does thistxIDalready exist intokenDB?SetStatus(Confirmed)directly right there, log it, and move on. No need to wait for Fabric to re-deliver the block, which isn't even guaranteed depending on where the seek checkpoint landed.SetStatusfails after a positive existence check; same thing, fall back to the listener. Belt and suspenders.Both operations are idempotent by design.
tokens.Appendhas an explicit existence guard so calling it twice is a no-op.ttxDB.SetStatusis a plain SQLUPDATEso writingConfirmedto an already-Confirmedrow does nothing. There's no risk of double-applying anything.To make the recovery logic actually testable without spinning up real databases, the core check got extracted into
recoverCommittedPending; a small standalone function that takes two narrow interfaces (tokenExistenceCheckerandpendingStatusSetter) instead of the concrete storage types. Five unit tests cover:Confirmed, finality listener skippedSetStatusblows up; graceful fallback, no data corruptiontxID; both calls succeed cleanly, second one is a no-op in productionThis is particularly nasty in high-throughput environments like CBDC pilots or supply-chain tokenization platforms where you're processing a lot of transactions and a routine rolling restart or unexpected crash is basically guaranteed to hit this window eventually. The failure is completely silent: no error surfaced to the operator, no alert, just a
Pendingrecord that never moves and an audit trail with a hole in it.After this fix, any node that restarts after hitting this state will automatically heal itself during
RestoreTMSbefore it starts accepting new work. No manual intervention, no DB surgery, no need to replay blocks.