Skip to content

fix(config): close resource streams - #48

Open
Federico2014 wants to merge 1 commit into
developfrom
feature/resource-stream-hardening
Open

fix(config): close resource streams#48
Federico2014 wants to merge 1 commit into
developfrom
feature/resource-stream-hardening

Conversation

@Federico2014

@Federico2014 Federico2014 commented Aug 4, 2026

Copy link
Copy Markdown
Owner

What does this PR do?

Closes classpath resource streams used by configuration, Git metadata, and zk parameter loading. It also scopes analogous Files.walk() streams in plugin utilities and affected tests, and handles missing zk parameter resources explicitly.

Why are these changes required?

Unclosed classpath and NIO streams can retain file or directory handles until garbage collection, potentially causing resource exhaustion or platform-dependent failures during repeated operations.

This PR has been tested by:

  • Existing Framework and Plugin unit tests
  • JaCoCo changed-line coverage: 94.12% (16/17)
  • Framework and Plugin Checkstyle
  • Manual Testing: Not performed; the changes only affect resource lifecycle management

Follow up

None

Extra details

The repository was scanned for analogous resource-management omissions. No new test methods or test classes were added.

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR fixes resource leaks in java-tron by converting manual stream handling to try-with-resources in configuration loading, version/commit metadata reading, zk-snark parameter loading, and plugin directory-walk utilities. Tests are updated to match. A differential review report documents the changes.

Changes

Resource leak remediation

Layer / File(s) Summary
Classpath resource existence check
common/src/main/java/org/tron/core/config/Configuration.java
Changes the resource existence check in resolveConfigFile from getResourceAsStream to getResource, and removes trailing blank lines.
Version and commit metadata streams
framework/src/main/java/org/tron/core/config/args/Args.java
printVersion() and getCommitIdAbbrev() now use try-with-resources for InputStream allocation instead of manual close handling.
Zk-snark parameter file loading
framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java
getParamsFile resolves the resource path first, closes the stream with try-with-resources, and throws IllegalStateException when the resource is missing.
Plugin directory walk stream closure
plugins/src/main/java/common/org/tron/plugins/utils/FileUtils.java
copyDatabases and copyDir wrap Files.walk streams in try-with-resources; the Stream import is added.
Test resource handling updates
framework/src/test/java/org/tron/common/utils/FileUtilTest.java, framework/src/test/java/org/tron/core/services/http/BroadcastServletTest.java, framework/src/test/java/org/tron/core/services/http/solidity/GetTransactionByIdSolidityServletTest.java
Test cleanup and file-reading logic use try-with-resources; writers are explicitly closed before file reads; imports are adjusted.
Differential review report
JAVA_TRON_DIFFERENTIAL_REVIEW_2026-08-04.md
Adds a report covering scope, findings, test coverage, historical context, audit results, methodology, and confidence for the remediation.

Estimated code review effort: 2 (Simple) | ~15 minutes

Suggested reviewers: halibobo1205

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(config): close resource streams' directly matches the PR's primary objective of closing unclosed classpath and NIO resource streams across multiple configuration, metadata, and utility files.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/resource-stream-hardening

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java`:
- Around line 60-66: Update the resourcePath construction in the
ZksnarkInitService initialization flow to always use "/" between "params" and
fileName for ClassLoader.getResourceAsStream. Keep File.separator unchanged for
the temporary filesystem path used by fileOut.

In `@JAVA_TRON_DIFFERENTIAL_REVIEW_2026-08-04.md`:
- Around line 109-110: Update the PR `#45` statement in the review document to
remove the claim that the analogous Files.walk() fixes were omitted, since those
fixes are already included in the reviewed scope. Preserve the statement about
missing tests, or rename the PR reference only if the line is intended to
describe a different pull request.
- Around line 95-100: Revise the coverage conclusion in the report to explicitly
state that the missing-resource branch throwing IllegalStateException remains
untested, while retaining the 94.12% metric and CI-threshold qualification. Do
not claim that existing tests provide complete coverage unless you add a focused
test for that branch.
- Around line 114-128: The audit result claims all remaining resource-leak
matches have been identified, but the listed search scope does not include NIO
factory methods like Files.newInputStream, Files.newBufferedReader, and
FileChannel.open that can also own file descriptors. Either expand the
bullet-point list of searched APIs to include these NIO factories if the audit
covered them, or reword the conclusion statement to explicitly scope the "all
remaining matches" claim to only the APIs listed in the search.
- Around line 63-65: Update the classpath resource lookup to build its name with
the literal forward slash in the parameter-loading logic, while retaining
File.separator only for filesystem paths. Revise the nearby
platform-compatibility wording to no longer claim the resource construction is
platform-specific.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 8a143d35-cbb2-4dc9-a02b-2cbb21110236

📥 Commits

Reviewing files that changed from the base of the PR and between c2e1eea and 911607c.

📒 Files selected for processing (8)
  • JAVA_TRON_DIFFERENTIAL_REVIEW_2026-08-04.md
  • common/src/main/java/org/tron/core/config/Configuration.java
  • framework/src/main/java/org/tron/core/config/args/Args.java
  • framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java
  • framework/src/test/java/org/tron/common/utils/FileUtilTest.java
  • framework/src/test/java/org/tron/core/services/http/BroadcastServletTest.java
  • framework/src/test/java/org/tron/core/services/http/solidity/GetTransactionByIdSolidityServletTest.java
  • plugins/src/main/java/common/org/tron/plugins/utils/FileUtils.java

Comment thread framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java
Comment thread JAVA_TRON_DIFFERENTIAL_REVIEW_2026-08-04.md Outdated
Comment thread JAVA_TRON_DIFFERENTIAL_REVIEW_2026-08-04.md Outdated
Comment thread JAVA_TRON_DIFFERENTIAL_REVIEW_2026-08-04.md Outdated
Comment thread JAVA_TRON_DIFFERENTIAL_REVIEW_2026-08-04.md Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 8 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java
@Federico2014
Federico2014 force-pushed the feature/resource-stream-hardening branch from 911607c to 9bbf15f Compare August 4, 2026 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant