Skip to content

Fix: close consensus node gRPC channels in Client.close() - #2203

Merged
exploreriii merged 2 commits into
hiero-ledger:mainfrom
mohityadav8:fix/grpc-channel-leak
May 3, 2026
Merged

Fix: close consensus node gRPC channels in Client.close()#2203
exploreriii merged 2 commits into
hiero-ledger:mainfrom
mohityadav8:fix/grpc-channel-leak

Conversation

@mohityadav8

Copy link
Copy Markdown
Contributor

Description:
Fix resource leak in Client.close() by closing all consensus node gRPC channels.

Previously, Client.close() only closed the mirror node channel, leaving all
consensus node channels (_Node._channel) open. This caused lingering gRPC
connections even when using the client as a context manager.

Changes

  • Close all _Node._channel instances in client.network.nodes
  • Ensure proper cleanup of all gRPC resources on Client.close()
  • Prevent connection leaks and improve resource management

Fixes #2202

Notes for reviewer:

  • Verified that all gRPC channels are closed after calling client.close()
  • Tested using context manager (with Client(...))
  • No runtime errors observed after cleanup

Checklist

  • Documented (Code comments added)
  • Tested (basic manual testing with client lifecycle)

@mohityadav8
mohityadav8 requested review from a team as code owners April 27, 2026 11:56
@codacy-production

codacy-production Bot commented Apr 27, 2026

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@exploreriii exploreriii added step: 1st 1st stage of the review approval process reviewer: committer request review help from a committer labels Apr 27, 2026
@coderabbitai

coderabbitai Bot commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Client.close() now performs best-effort cleanup of consensus node resources: if self.network and self.network.nodes exist, it iterates the nodes and calls node._close() in addition to clearing the mirror channel/stub.

Changes

Cohort / File(s) Summary
Client Shutdown Logic
src/hiero_sdk_python/client/client.py
Extended Client.close() to, when available, iterate self.network.nodes and call node._close() for each consensus node in addition to closing/clearing the mirror channel and stub, addressing gRPC channel/resource leaks.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and specifically describes the main change: closing consensus node gRPC channels in the Client.close() method to fix a resource leak.
Description check ✅ Passed The description clearly explains the resource leak issue, the changes made, and verification steps, directly relating to the changeset's objective.
Linked Issues check ✅ Passed The PR implementation directly addresses issue #2202 by adding logic to close all consensus node gRPC channels in Client.close(), matching the expected behavior and proposed fix.
Out of Scope Changes check ✅ Passed All changes are scoped to the objective of closing consensus node gRPC channels; no unrelated modifications are present in the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 88977a9a-5f67-46b3-bc40-bb4c4e7bc5c2

📥 Commits

Reviewing files that changed from the base of the PR and between fd13539 and b908a7f.

📒 Files selected for processing (1)
  • src/hiero_sdk_python/client/client.py

Comment thread src/hiero_sdk_python/client/client.py
@mohityadav8
mohityadav8 force-pushed the fix/grpc-channel-leak branch from 59d1552 to e307b6c Compare April 27, 2026 12:14
@mohityadav8
mohityadav8 requested a review from a team as a code owner April 27, 2026 12:14
@mohityadav8
mohityadav8 force-pushed the fix/grpc-channel-leak branch from e307b6c to 1dffb12 Compare April 27, 2026 12:20

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
src/hiero_sdk_python/client/client.py (1)

210-213: ⚠️ Potential issue | 🔴 Critical

Critical: node.close() does not exist — Client.close() will still raise AttributeError.

Per src/hiero_sdk_python/node.py (lines 80–114), _Node only defines a private _close() method; there is no public close(). Any Client with at least one node will raise AttributeError: '_Node' object has no attribute 'close' when close() (or __exit__) runs — the opposite of this PR's stated goal.

This was previously flagged on a prior commit; the .values() issue was fixed, but the call was changed to node.close() instead of node._close(), which is still broken. A unit test that actually executes Client.close() on a real client (or exits a with Client(...) block) and asserts no exception is raised — and that each node._channel is None afterward — would catch this and should be added alongside the fix.

🐛 Proposed fix
-        # Fix: Close all consensus node channels
-        if self.network and self.network.nodes:
-            for node in self.network.nodes:
-                node.close()
+        # Close all consensus node channels
+        if self.network is not None and self.network.nodes:
+            for node in self.network.nodes:
+                node._close()  # pylint: disable=protected-access

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 528d7635-1b58-4565-9a9c-5918f585cceb

📥 Commits

Reviewing files that changed from the base of the PR and between b908a7f and 99d2b15.

📒 Files selected for processing (1)
  • src/hiero_sdk_python/client/client.py

@mohityadav8
mohityadav8 requested a review from aceppaluni April 27, 2026 18:23
Comment thread src/hiero_sdk_python/client/client.py Outdated
@mohityadav8
mohityadav8 requested a review from MonaaEid April 28, 2026 06:11

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
src/hiero_sdk_python/client/client.py (1)

210-213: ⚠️ Potential issue | 🔴 Critical

Client.close() is syntactically broken here.

Line 212 introduces a for loop with no body, so this file will not parse. If this block was truncated accidentally, the body should delegate to src/hiero_sdk_python/node.py:_Node._close() rather than reimplementing channel teardown here.

🐛 Proposed fix
-        # Fix: Close all consensus node channels
-        if self.network and self.network.nodes:
-            for node in self.network.nodes:
+        # Close all consensus node channels
+        if self.network and self.network.nodes:
+            for node in self.network.nodes:
+                node._close()  # pylint: disable=protected-access

You can verify the current file still lacks a loop body with:

#!/bin/bash
sed -n '208,215p' src/hiero_sdk_python/client/client.py

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 8ab94894-591d-4d7c-9227-98cb86ed905d

📥 Commits

Reviewing files that changed from the base of the PR and between 99d2b15 and c8f6912.

📒 Files selected for processing (1)
  • src/hiero_sdk_python/client/client.py

Comment thread src/hiero_sdk_python/client/client.py
@mohityadav8
mohityadav8 requested a review from MonaaEid April 28, 2026 19:33

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 12bca3ee-c772-45c4-9590-e155e9e773be

📥 Commits

Reviewing files that changed from the base of the PR and between c8f6912 and aca4aa9.

📒 Files selected for processing (1)
  • src/hiero_sdk_python/client/client.py

Comment thread src/hiero_sdk_python/client/client.py Outdated
@mohityadav8

Copy link
Copy Markdown
Contributor Author

cc @nadineloepfe @aceppaluni

@codecov

codecov Bot commented Apr 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2203   +/-   ##
=======================================
  Coverage   93.73%   93.73%           
=======================================
  Files         145      145           
  Lines        9477     9480    +3     
=======================================
+ Hits         8883     8886    +3     
  Misses        594      594           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@manishdait

Copy link
Copy Markdown
Contributor

@mohityadav8, thanks for the PR please apply the pre-commit you can follow the docs here https://github.com/hiero-ledger/hiero-sdk-python/blob/main/docs/sdk_developers/setup.md

@tech0priyanshu tech0priyanshu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM Please apply the pre-commit. Thanks for your contribution

@mohityadav8
mohityadav8 force-pushed the fix/grpc-channel-leak branch from a7c15aa to 8b87e10 Compare April 29, 2026 13:39
@manishdait

Copy link
Copy Markdown
Contributor

@mohityadav8, can you please update the branch again

@mohityadav8

Copy link
Copy Markdown
Contributor Author

Done @manishdait

MonaaEid
MonaaEid previously approved these changes Apr 30, 2026
manishdait
manishdait previously approved these changes Apr 30, 2026
@mohityadav8

Copy link
Copy Markdown
Contributor Author

cc @exploreriii

@codacy-production

codacy-production Bot commented May 1, 2026

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@mohityadav8
mohityadav8 requested a review from tech0priyanshu May 1, 2026 19:18
@tech0priyanshu

Copy link
Copy Markdown
Contributor

cc @hiero-ledger/hiero-sdk-python-maintainers
Please approve workflow run . Changes LTGM

@aceppaluni aceppaluni added reviewer: maintainer PR needs a review from the maintainer team reviewer: committer request review help from a committer labels May 1, 2026
@mohityadav8

Copy link
Copy Markdown
Contributor Author

Have you set up the GPG key? If so, your commit needs to be verified, and you can perform a soft reset on your commits: git reset --soft HEAD~11 git commit -s -S -m "your commit message" git push --force-with-lease origin YOUR-BRANCH

done

@manishdait

Copy link
Copy Markdown
Contributor

@mohityadav8, your first two commit is missing the gpg-sign.

@mohityadav8
mohityadav8 force-pushed the fix/grpc-channel-leak branch from 78dce33 to a4f3b77 Compare May 3, 2026 10:33
@mohityadav8
mohityadav8 requested a review from a team as a code owner May 3, 2026 10:33
@mohityadav8
mohityadav8 requested a review from georgi-l95 May 3, 2026 10:33
Signed-off-by: Mohit Yadav <ymohit799057@gmail.com>
@mohityadav8
mohityadav8 force-pushed the fix/grpc-channel-leak branch from a4f3b77 to 0982bee Compare May 3, 2026 10:35
@mohityadav8

Copy link
Copy Markdown
Contributor Author

@mohityadav8, your first two commit is missing the gpg-sign.

@manishdait ready for review

@exploreriii exploreriii added step: 2nd second stage of the review approval process reviewer: write requires a review with write permissions and removed reviewer: maintainer PR needs a review from the maintainer team reviewer: committer request review help from a committer labels May 3, 2026
@MonaaEid

MonaaEid commented May 3, 2026

Copy link
Copy Markdown
Contributor

Update branch please

@exploreriii exploreriii added step: merge ready PR is ready and waiting to merge status: update branch developer needs to click update branch and removed step: 2nd second stage of the review approval process reviewer: write requires a review with write permissions labels May 3, 2026
@mohityadav8
mohityadav8 requested a review from MonaaEid May 3, 2026 13:02
@exploreriii
exploreriii merged commit 2bde066 into hiero-ledger:main May 3, 2026
28 checks passed
NssGourav pushed a commit to NssGourav/hiero-sdk-python that referenced this pull request May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: update branch developer needs to click update branch step: merge ready PR is ready and waiting to merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix: client.close() does not close consensus node gRPC channels (resource leak)

6 participants