Skip to content

fix: use context managers to prevent file descriptor leaks in genMetrics.py#4066

Merged
maliberty merged 3 commits into
The-OpenROAD-Project:masterfrom
ashnaaseth2325-oss:fix/genmetrics-fd-leaks
Mar 30, 2026
Merged

fix: use context managers to prevent file descriptor leaks in genMetrics.py#4066
maliberty merged 3 commits into
The-OpenROAD-Project:masterfrom
ashnaaseth2325-oss:fix/genmetrics-fd-leaks

Conversation

@ashnaaseth2325-oss

Copy link
Copy Markdown
Contributor

SUMMARY

This PR fixes file descriptor leaks in flow/util/genMetrics.py by replacing manual file handling with context managers. The changes primarily affect the read_sdc() and merge_jsons() functions, ensuring files are always properly closed even when exceptions occur.


FIX

# Before
file = open(path, "r")
data = json.load(file)
output.update(data)
file.close()

# After
with open(path, "r") as file:
    data = json.load(file)
output.update(data)

VERIFICATION

Run a normal OpenROAD flow to make sure everything works as before with valid inputs. Then try breaking a metrics JSON file (e.g., truncate it) and rerun the flow. It should handle the error cleanly without leaking file descriptors or crashing.

Replace bare open()/close() calls in read_sdc() and merge_jsons()
with context managers so file descriptors are released on exception.

Signed-off-by: Ashnaa Seth <ashnaaseth2325@gmail.com>
Signed-off-by: ashnaaseth2325-oss <ashnaaseth2325@gmail.com>
@ashnaaseth2325-oss

Copy link
Copy Markdown
Contributor Author

Hi @maliberty @eder-matheus,
Kindly review this PR.
Thanks!

Comment thread flow/util/genMetrics.py Outdated
ashnaaseth2325-oss and others added 2 commits March 28, 2026 23:20
Co-authored-by: Matt Liberty <matt.liberty@gmail.com>
Signed-off-by: ashnaaseth2325-oss <ashnaaseth2325@gmail.com>
Signed-off-by: Ashnaa Seth <ashnaaseth2325@gmail.com>
@ashnaaseth2325-oss

Copy link
Copy Markdown
Contributor Author

Hi @maliberty, thanks for the review!
I've addressed the feedback and fixed the DCO issue. All checks should pass now.
Kindly take another look when you get a chance. Thanks!

@ashnaaseth2325-oss

Copy link
Copy Markdown
Contributor Author

Hello @maliberty @eder-matheus
Any feedback on this PR would be appreciated.

@maliberty maliberty merged commit 8e37ce8 into The-OpenROAD-Project:master Mar 30, 2026
9 checks passed
@oharboe oharboe mentioned this pull request Apr 7, 2026
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.

2 participants