From 7bbe01461b82638dfa78832f5cf1e691062b6711 Mon Sep 17 00:00:00 2001 From: lxcxjxhx Date: Wed, 29 Jul 2026 11:30:10 +0800 Subject: [PATCH] fix: close HTTP response and file handles to prevent resource leaks --- infra/build/build_status/fuzz_introspector_page_gen.py | 7 ++++--- infra/chronos/coverage_test_collection.py | 5 ++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/infra/build/build_status/fuzz_introspector_page_gen.py b/infra/build/build_status/fuzz_introspector_page_gen.py index e72bf6bbfe22..d88b6c47f86b 100644 --- a/infra/build/build_status/fuzz_introspector_page_gen.py +++ b/infra/build/build_status/fuzz_introspector_page_gen.py @@ -231,8 +231,8 @@ def fetch_fuzz_introspector_summary(report_url): """ # Extract json summary file. summary_url = report_url.replace('fuzz_report.html', 'summary.json') - response = urlopen(summary_url) - json_data = json.loads(response.read()) + with urlopen(summary_url) as response: + json_data = json.loads(response.read()) # 1) Extract fuzzer count. This corresponds to all but two elements at the # top level of the dictionary. @@ -251,7 +251,8 @@ def fetch_fuzz_introspector_summary(report_url): # Momentarily, we will get this from the HTML page because it's not yet # in the summary.json. This will change in the near future, but in the # spirit of time we keep it like this for now. - fuzz_report_html = urlopen(report_url).read() + with urlopen(report_url) as resp: + fuzz_report_html = resp.read() soup = BeautifulSoup(fuzz_report_html, 'html.parser') target_divs = soup.findAll('text', {'class': 'percentage'}) diff --git a/infra/chronos/coverage_test_collection.py b/infra/chronos/coverage_test_collection.py index 5e415bd4be4c..2ddfbb4bb3ea 100644 --- a/infra/chronos/coverage_test_collection.py +++ b/infra/chronos/coverage_test_collection.py @@ -108,9 +108,8 @@ def run_llvm_html_generation(objects, out_dir, workdir=COV_WORKDIR): f'-instr-profile={instr_profile}', objects, ] - stdout_fp = open(os.path.join(out_dir, 'summary.json'), 'w') - subprocess.check_call(' '.join(cmd), shell=True, stdout=stdout_fp) - stdout_fp.close() + with open(os.path.join(out_dir, 'summary.json'), 'w') as stdout_fp: + subprocess.check_call(' '.join(cmd), shell=True, stdout=stdout_fp) def reset_cov_workdir():