cacti icon indicating copy to clipboard operation
cacti copied to clipboard

fix(connector-fabric): always disconnect gateway in API methods

Open mn-ram opened this issue 7 months ago • 4 comments

Summary

  • Add try/finally with gateway.disconnect() to 5 Fabric connector API methods that were leaking gateway connections

Problem

Four methods created a Fabric Gateway via createGatewayWithOptions() but never called gateway.disconnect():

  • getBlock()
  • getChainInfo()
  • getTransactionReceiptByTxID()
  • getDiscoveryResults()

Each call leaked open gRPC connections to every discovered Fabric peer and orderer. Additionally, transact() only disconnected on the success path — any error (endorsement failure, timeout, chaincode error) leaked the gateway.

Under production load this caused gRPC connection exhaustion on Fabric peers, file descriptor exhaustion (EMFILE), and memory leaks.

Changes

File: packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/plugin-ledger-connector-fabric.ts

Method Change
transact() Moved gateway.disconnect() from success-only path to finally block
getTransactionReceiptByTxID() Added try/finally with gateway.disconnect()
getBlock() Added try/finally with gateway.disconnect()
getChainInfo() Added try/finally with gateway.disconnect()
getDiscoveryResults() Added try/finally with gateway.disconnect()

Test plan

  • [ ] Run existing Fabric connector integration tests
  • [ ] Verify all 5 methods return correct results
  • [ ] Verify transact() cleans up on both success and failure
  • [ ] Under load, confirm no gRPC connection accumulation (check with lsof)

Fixes #4186

mn-ram avatar Apr 04 '26 13:04 mn-ram

@LordKubaya thanks for approving! I shortened the commit title (now within the 72-char limit) and updated the PR title. No code changes were made.

mn-ram avatar Apr 21 '26 03:04 mn-ram

@mn-ram thank you for the PR. Please check the yarn-lint job, which is failing

RafaelAPB avatar Apr 21 '26 20:04 RafaelAPB

Addressed the Copilot feedback: -Moved gateway creation inside the try block in transact() so errors are properly caught. -Moved the "Creating Fabric Gateway instance" log before the createGateway() call for correct order. -Fixed two "it's" → "its" typos in getBlock debug messages.

Also fixed the lint issue. yarn lint now passes locally.

mn-ram avatar Apr 22 '26 04:04 mn-ram

Hi @RafaelAPB ,

Just a gentle follow-up on this PR. I’ve addressed the previous feedback and fixes, including the lint issues. There are still a couple of checks pending/failing, and I’m happy to take care of those if needed.

Would really appreciate your review when you get a chance. Thanks!

mn-ram avatar May 01 '26 07:05 mn-ram