-
-
Notifications
You must be signed in to change notification settings - Fork 412
Description
Checklist
- I am using an up-to-date version.
- I have read the documentation.
- I have searched existing issues.
TagStudio Version
Alpha v9.3.2
Operating System & Version
Windows 10
Description
Occasionally when closing Tag Studio, via the X button, it will wipe the ts_library.json
file, leaving it with zero bytes, despite the logs claiming the library was saved.
I spent some time fiddling around with the code and stumbled upon a potential solution:
Move the call to self.to_json()
outside of the with open(
block in the save_library_to_disk
function.
Example
# If `to_json` is called in the `with` block, the file will sometimes fail to write
jsonLibrary = self.to_json()
with open(
self.library_dir / TS_FOLDER_NAME / filename, "w", encoding="utf-8"
) as outfile:
outfile.flush()
ujson.dump(
jsonLibrary,
outfile,
ensure_ascii=False,
escape_forward_slashes=False,
)
Note that having jsonLibrary = self.to_json()
inside the with open(
block did not fix the issue.
I stumbled into this when I was logging the size of the jsonLibrary
to see if it was actually being populated, and comparing the sizes at different places in the function.
(It was consistently the same size, regardless of if it wrote the bytes to disk or not.)
With that change in place I was able to go through over a dozen open/close cycles and it saves the library each time.
Where previously it would fail to save by the time I got to close number 5
I can't pretend to understand why this seems to have solved the issue, so I can not guarantee the robustness of the proposed fix.
Other Things I Tried
- Moving
outfile.flush()
after theujson.dump
call - Adding
outfile.close()
after theujson.dump
call
Work Around
During my testing I discovered if I manually save the library then immediately close the app, the bug never seemed to happen.
Additional Info
- My library
- is stored on an internal SSD
- is not stored on the C:\ disk
- has a little over 4K files in it
- json file is ~340K
- This may be related to [Bug]: Computer shutdowns and crashes will delete all fields in open libraries #320
Expected Behavior
Data should be written to the library file on exit
Steps to Reproduce
- Open Tag Studio
- Create a new library
- Manually save
- Note the size of the save file (mine is ~340KB)
- Close Tag Studio
- Open Tag Studio
- Wait for it to load the library
- Close Tag Studio
- Check the size of the save file
- If the save file is not 0 bytes, repeat steps 6-9
Logs
Log output when it fails to save
Saving Library...
[LIBRARY] Saving Library to Disk...
[LIBRARY] Formatting Tags to JSON...
[LIBRARY] Formatting Entries to JSON...
[LIBRARY] Formatting Collations to JSON...
[LIBRARY] Done Formatting to JSON!
[LIBRARY] Library saved to disk in 0.008 seconds
[SHUTDOWN] Ending Thumbnail Threads...
Log output when it successfully saves
Saving Library...
[LIBRARY] Saving Library to Disk...
[LIBRARY] Formatting Tags to JSON...
[LIBRARY] Formatting Entries to JSON...
[LIBRARY] Formatting Collations to JSON...
[LIBRARY] Done Formatting to JSON!
[LIBRARY] Library saved to disk in 0.031 seconds
[SHUTDOWN] Ending Thumbnail Threads...