[Bug] WordExtractor cannot extract remote .docx files on Windows (PermissionError: temp file is still open)
- Dominant language
- TypeScript
- Stars
- 156k
- Forks
- 24.6k
- Avg merge
- 22h 9m
- Merged PRs (30d)
- 610
Description
- [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542).
- [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general).
- [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones.
- [x] I confirm that I am using English to submit this report, otherwise it will be closed.
- [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
- [x] Please do not modify this template :) and fill in all the required fields.
### Dify version
`main` @ `b6824334cf` (source install, API 1.16.x)
### Cloud or Self Hosted
- [x] Self Hosted (Source)
### Steps to reproduce
On Windows (Python 3.12), run the existing unit test:
```bash
uv run --project api pytest api/tests/unit_tests/core/rag/extractor/test_word_extractor.py::test_init_downloads_via_remote_fetcher
```
It fails deterministically with:
```text
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\...\\AppData\\Local\\Temp\\tmpxxx'
```
The same failure happens at runtime: create a `WordExtractor` with a remote `.docx` URL and call `extract()`.
### ✔️ Expected Behavior
The remote file is downloaded to a temporary file, `extract()` opens it and returns the parsed documents. Works on Linux/macOS.
### ❌ Actual Behavior
`WordExtractor.__init__` keeps a `tempfile.NamedTemporaryFile()` handle open after writing the response:
```python
self.temp_file = tempfile.NamedTemporaryFile() # noqa SIM115
self.temp_file.write(response.content)
self.temp_file.flush()
self.file_path = self.temp_file.name
```
`extract()` later reopens `self.file_path` (via python-docx). On Windows a `NamedTemporaryFile` that is still open cannot be reopened by another handle, so extraction fails with `PermissionError` (surfaced by python-docx as `PackageNotFoundError`). Python's own documentation calls out this platform difference. This makes remote .docx ingestion broken on Windows, and the repository's unit test fails as well.
Proposed fix: close the temporary file immediately after writing (`delete=False`), keep only the path, and unlink it in `close()`.
Contributor guide
Research direction
Start by running api/tests/unit_tests/core/rag/extractor/test_word_extractor.py::test_init_downloads_via_remote_fetcher on Windows, then inspect WordExtractor.__init__ and close(). Verify that the downloaded temporary file can be reopened during extract() and that cleanup still removes it afterward; the test should pass without PermissionError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 82/100