From 5bb3940517dd45f1007eff3fab0843e3e63123ad Mon Sep 17 00:00:00 2001 From: Kailigithub <12250313+Kailigithub@users.noreply.github.com> Date: Tue, 7 Jul 2026 03:10:20 +0800 Subject: [PATCH] refactor(ga): wrap cr_header read in context manager to close file handle open(cr_header, ...).read() on every code_run invocation leaks a file descriptor until garbage collection. With long sessions this can show up as ResourceWarning or EBADF under load. Wrap in a with block; net behavior unchanged. --- ga.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ga.py b/ga.py index d2b25aef4..d112fa72b 100644 --- a/ga.py +++ b/ga.py @@ -24,7 +24,8 @@ def code_run(code, code_type="python", timeout=60, cwd=None, code_cwd=None, stop if code_type in ["python", "py"]: tmp_file = tempfile.NamedTemporaryFile(suffix=".ai.py", delete=False, mode='w', encoding='utf-8', dir=code_cwd) cr_header = os.path.join(script_dir, 'assets', 'code_run_header.py') - if os.path.exists(cr_header): tmp_file.write(open(cr_header, encoding='utf-8').read()) + if os.path.exists(cr_header): + with open(cr_header, encoding='utf-8') as _h: tmp_file.write(_h.read()) tmp_file.write(code) tmp_path = tmp_file.name tmp_file.close()