fix: use context managers for file writes to prevent resource leaks#575
Open
Kailigithub wants to merge 1 commit into
Open
fix: use context managers for file writes to prevent resource leaks#575Kailigithub wants to merge 1 commit into
Kailigithub wants to merge 1 commit into
Conversation
Six call sites in three files wrote to disk via bare `open(...).write(...)`, which leaks file handles until garbage collection and can leave writes un-flushed if the interpreter exits before GC. Wrap each in `with` so the handle is deterministically closed after the write. - agentmain.py:27 global_mem.txt seed write (first-run init) - agentmain.py:31 global_mem_insight.txt template copy (first-run init) - agentmain.py:36 tmwd_cdp_bridge/config.js (first-run init) - agentmain.py:276 reflect log append (per-task) - ga.py:401 file_write tool, prepend mode (per-call, rewrites entire file) - frontends/wechatapp.py:266 WeChat media download (per-message) Behavior is unchanged: reads still happen iff the source file exists, writes still produce byte-identical output, and the same encoding flags are used. Notable: the ga.py prepend path is the most exposed — it reads the existing file then rewrites the whole thing in one shot. Without `with`, an interpreter abort between read and re-flush would silently truncate the user's file. After this change the write is always flushed and closed before the line returns.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace six
open(path, mode, ...).write(...)call sites with properwith open(...) as f:context managers so file handles are deterministically closed and writes are guaranteed to flush.The bare form relies on garbage collection to release file descriptors, which can leave buffered writes un-flushed if the interpreter is interrupted (Ctrl+C, OOM, crash). The most exposed call site is
ga.py'sfile_writetool in prepend mode, which reads the existing file then rewrites it in one shot — withoutwith, an abort between read and re-flush would silently truncate the user's file.Call sites changed
agentmain.pyglobal_mem.txtseed write (first-run init)agentmain.pyglobal_mem_insight.txttemplate copy (first-run init)agentmain.pytmwd_cdp_bridge/config.js(first-run init)agentmain.pyga.pyfile_writetool, prepend mode (per-call, rewrites entire file)frontends/wechatapp.pyBehavior
Unchanged. Each call site preserves the same read/write ordering, the same encoding, and the same conditional semantics (e.g. the template copy still only reads the source file if
os.path.exists(t)returns True).Verification
python3 -m py_compile agentmain.py ga.py frontends/wechatapp.py— passespython3 -c "import ast; ast.parse(open('agentmain.py').read())"(and same for ga.py, wechatapp.py) — passesruff check— no new findings introduced by this change; pre-existing project-style warnings (E701, F541 etc.) are untouchedtests/is empty), so runtime behavior was checked by AST + manual review onlygit diffreviewed for read-then-write ordering preservation