Requester icon indicating copy to clipboard operation
Requester copied to clipboard

FileExistsError on Windows when writing history backup file

Open dpc00 opened this issue 2 months ago • 2 comments

`write_json_file` in `core/init.py` (called from `persist_requests`, which every successful request triggers) writes history atomically via a temp file renamed to a `.bkp` backup path:

``` File "core/init.py", line 407, in write_json_file FileExistsError: [WinError 183] Cannot create a file when that file already exists: '...\Packages\User\Requester.history.json' -> '...\Packages\User\Requester.history.json.bkp' ```

`os.rename()` on POSIX silently overwrites an existing destination; on Windows it raises `FileExistsError` instead if the destination already exists. Since `Requester.history.json.bkp` already existed on this machine from a prior run, the very next request crashed here.

Reproduction: run any request once (creating `Requester.history.json.bkp`), then run a second request -- the second one hits the `.bkp` rename and raises.

The exception doesn't visibly break the visible request/response flow (the response tab still renders correctly), so it's easy to miss unless the console is checked -- this was found via a full console read, not a failed request.

Suggested fix: use `os.replace()` instead of `os.rename()` for the backup step -- `os.replace()` is documented to overwrite an existing destination atomically on both POSIX and Windows.

Sublime Text 4200, Windows.

dpc00 avatar Sep 10 '26 11:09 dpc00

Claude reports: closing this after 30 days with no response. Reopen it if it is still relevant.

dpc00 avatar Oct 27 '26 19:10 dpc00

Still reproduces on 3.0.0 (released 2026-08-22), Sublime Text 4215, Windows 11, Package Control install, 2026-11-04.

Fresh run: requests.get('http://127.0.0.1:8765/x.json') in a .pyr file against a local python -m http.server. The response tab renders correctly (200 OK, headers, formatted JSON), and the console then shows

File ".../Requester.sublime-package/core/__init__.py", line 407, in write_json_file
    os.rename(path, path_backup)  # create backup file in case rename is unsuccessful
FileExistsError: [WinError 183] Cannot create a file when that file already exists:
'...\Packages\User\Requester.history.json' -> '...\Packages\User\Requester.history.json.bkp'

State left behind in Data/Packages/User after two requests:

Requester.history.json        2 bytes   (never updated)
Requester.history.json.bkp    0 bytes
Requester.history.json.tmp  403 bytes   (written, then orphaned by the failed rename)

So request history silently stops updating on Windows from the second request on, and each run leaves a stale .tmp. os.replace(path, path_backup) (or unlinking the backup first) fixes it, since os.replace overwrites an existing destination on both platforms.

dpc00 avatar Nov 05 '26 04:11 dpc00