Skip to content

fix(docx): collect temporary images by their real temp path, not /tmp - #5872

Closed
gouyu-hou wants to merge 1 commit into
Tencent:mainfrom
gouyu-hou:fix/docx-temp-images-cleanup
Closed

gouyu-hou wants to merge 1 commit into
Tencent:mainfrom
gouyu-hou:fix/docx-temp-images-cleanup

Conversation

@gouyu-hou

Copy link
Copy Markdown

Observed

The multiprocess path saves each picture with tempfile.mkdtemp(prefix="docx_img_") and then recognises its own files by a literal POSIX prefix. On Windows mkdtemp returns a path under %TEMP%, so that predicate is False for every image the parser just wrote:

$ python -c "from docreader.parser.docx_parser import _save_image_to_temp; ..."
saved      : C:/Users/GouYu/AppData/Local/Temp/docx_img_mh1fq8bk/page_1_img_0.png
old rule   : p.startswith("/tmp/docx_img_") -> False

Nothing is collected, so _cleanup_temp_image_files is called with an empty set and returns at its first if not temp_paths. Every multimodal .docx parse leaves one directory plus one PNG per image in the temp directory, on Windows only.

Expected

The images the parser wrote for its workers are removed when the batch finishes, on every platform.

Root cause

docreader/parser/docx_parser.py, two hard-coded "/tmp/docx_img_" checks:

  • _process_batch_results collects image_data.local_path only when it starts with that prefix;
  • _cleanup_temp_image_files removes the per-image directory only when the directory starts with it.

tempfile.mkdtemp puts the directory wherever gettempdir() points, so the prefix only ever matches on hosts whose temp directory happens to be /tmp.

Fix

Add one predicate — the parent directory's name starts with docx_img_, which is what mkdtemp(prefix=...) guarantees — and use it at both call sites. Path separators no longer matter, and the POSIX behaviour is unchanged because those paths satisfy the new predicate too.

Reproduction and verification

Windows 11 / Python 3.13.

Before, on main, with the real path the function returns (_save_image_to_temp probe above): the old rule rejects it, so the file is never collected.

After:

$ python -m pytest docreader/tests/test_docx_temp_images.py -q
2 passed in 2.34s

test_an_image_saved_for_a_worker_is_collected_and_removed saves a real image through _save_image_to_temp, asserts the path is collected, runs _cleanup_temp_image_files and asserts both the PNG and its directory are gone. test_a_path_outside_the_image_temp_dirs_is_not_collected keeps the predicate from over-reaching.

Affected surface (docx parsing suites):

$ python -m pytest docreader/tests/test_docx_tables.py -q     # 19 passed
$ python -m pytest docreader/tests/test_docx_merge.py -q      # 15 passed, 3 subtests passed
$ python -m compileall -q docreader/parser/docx_parser.py     # no output

The multiprocess path saves each picture through tempfile.mkdtemp(prefix=
"docx_img_"), then recognises its own files with a literal POSIX prefix
("/tmp/docx_img_") in _process_batch_results and again in
_cleanup_temp_image_files. On Windows mkdtemp returns
C://Users//...//Temp//docx_img_xxxx, so the predicate is False for every
image the parser just wrote: nothing is collected and every multimodal
docx parse leaves its PNGs and their directories behind.

Decide from the directory name instead, which is identical on every
platform, and keep one predicate for both call sites.
Copilot AI balanced review requested due to automatic review settings November 23, 2026 09:08

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@gouyu-hou

Copy link
Copy Markdown
Author

Closing as a duplicate of #5795, which carries the same fix for the same function (and the same test file) from the previous day. Sorry for the noise.

@gouyu-hou gouyu-hou closed this Nov 23, 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