[Blender Asset Tracer] Files are not closed on Windows after using BAT to trace .blend file in Add-on #65933
Labels
No Label
legacy module
Platforms, Builds, Tests & Devices
legacy project
Blender Asset Tracer
legacy project
Platform: Windows
Priority::Normal
Status::Archived
Status::Confirmed
Status::Needs Triage
Status::Resolved
Type::Design
Type::Report
Type::To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender-asset-tracer#65933
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
System Information
Operating system: Windows 10
Short description of error
Importing BAT as a module into a blender add-on and performing a trace or pack operation on the currently open .blend file will result in the following error upon trying to save the .blend file.
Exact steps for others to reproduce the error
I have attached a simple add-on for the purposes of demonstrating this behavior. It adds a panel to blender with a single button called "Trace" which will trace the currently open blend file if it has been saved.
BAT_bug_addon.zip
To reproduce:
I believe the issue is that BAT is not correctly closing files on Windows. I have tried manually calling the blender_asset_tracer.blendfile.close_all_cached() method but nothing changes.
Added subscriber: @haiku
[Blender Asset Tracer]to [Blender Asset Tracer] Files are not closed on Windows after using BAT to trace .blend file in Add-onProcess explorer show that once the 'trace' method is called Blender keeps open file handles for all .blend files found by BAT during the trace (This includes linked .blend files).
I can manually close these files as administrator using process explorer which resolves the saving issue but if I then try and trace again using the Add-on I get the following error:
Traceback (most recent call last):
OSError: [Errno 9] Bad file descriptor
I have been able to solve this issue by adding the following code:
For the deps() method intrace_init_.py just add a bfile.close() to the end of the method. This will close the main .blend file being traced after iterating through all the blocks.
All libraries also need closing (although not necessary to fix the original saving bug mentioned in the original post) since they also never get closed.
To do this just add a libfile.close() to the end of the _visit_linked_blocks() method in trace/file2blocks.py
Added subscriber: @dr.sybren
Changed status from 'Open' to: 'Archived'
Calling
libfile.close()
like that is not a good idea. Opening a blend file is the most expensive part of the tracing process, which is why it's cached in the first place by callingblendfile.open_cached()
. Also in this example it's not called when an exception occurs, which means it can still keep files open.These cached files should be closed by calling
blender_asset_tracer.blendfile.close_all_cached()
. For me this works fine:import blender_asset_tracer.blendfile; blender_asset_tracer.blendfile.close_all_cached()
The problem is not with BAT, but with the way it's loaded in your add-on. You add the
lib
path tosys.path
so that BAT can import its own submodules fromblender_asset_tracer
, but you also import BAT asfrom .lib.blender_asset_tracer import trace
. This means that BAT is now imported two times; once asbat_bug_addon.lib.blender_asset_tracer
and once asblender_asset_tracer
. The cached files are inblender_asset_tracer.blendfile._cached_bfiles
, but I'm guessing you're callingbat_bug_addon.lib.blender_asset_tracer.blendfile.close_cached()
.The solution is to do one of the two, but not both:
lib
directory tosys.path
and import everything in there as if it's top-level.lib
tosys.path
and import everything frombat_bug_addon.lib
(and adjust whatever is in there to only use relative imports).Thank you, that makes a lot of sense.