Skip to content

Fix PermissionError deleting locked HDF5 cache on Windows#7870

Open
theoryshaw wants to merge 1 commit intov0.8.0from
Fix_PermissionError
Open

Fix PermissionError deleting locked HDF5 cache on Windows#7870
theoryshaw wants to merge 1 commit intov0.8.0from
Fix_PermissionError

Conversation

@theoryshaw
Copy link
Copy Markdown
Member

When the hdf5 serializer constructor fails on an existing file, the HDF5 C library retains the file handle on Windows. Force gc.collect() before os.remove() to release it, and catch PermissionError to skip the cache gracefully if the file is still locked.

Generated with the assistance of an AI coding tool.

Was getting the following errors...

Python: Traceback (most recent call last):
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\bim\ifc.py", line 527, in execute_ifc_operator
    result = getattr(operator, "_modal")(context, event)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\bim\module\model\product.py", line 236, in _modal
    self.handle_mouse_move(context, event)
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\bim\module\model\polyline.py", line 411, in handle_mouse_move
    detected_snaps = tool.Snap.detect_snapping_points(context, event, self.objs_2d_bbox, self.tool_state)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\tool\snap.py", line 402, in detect_snapping_points
    snap_points = tool.Raycast.ray_cast_by_proximity_2d(context, event, snap_obj)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\tool\raycast.py", line 380, in ray_cast_by_proximity_2d
    verts_2d = [
               ^
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\tool\raycast.py", line 381, in <listcomp>
    view3d_utils.location_3d_to_region_2d(region, rv3d, v) for v in snap_obj.verts_3d
    ^^^^^^^^^^^^
NameError: name 'view3d_utils' is not defined


bpy.ops.bim.draw_occurrence()
Python: Traceback (most recent call last):
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\bim\ifc.py", line 527, in execute_ifc_operator
    result = getattr(operator, "_modal")(context, event)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\bim\module\model\product.py", line 250, in _modal
    self.create_occurrence(context, event)
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\bim\module\model\product.py", line 201, in create_occurrence
    bpy.ops.bim.add_occurrence("INVOKE_DEFAULT")
  File "C:\Program Files\Blender Foundation\Blender 4.5\4.5\scripts\modules\bpy\ops.py", line 107, in __call__
    ret = _op_call(self.idname_py(), kw, C_exec, C_undo)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Error: Python: Traceback (most recent call last):
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\bim\ifc.py", line 158, in get_cache
    IfcStore.cache = ifcopenshell.geom.serializers.hdf5(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\ifcopenshell\ifcopenshell_wrapper.py", line 3384, in __init__
    _ifcopenshell_wrapper.HdfSerializer_swiginit(self, _ifcopenshell_wrapper.new_HdfSerializer(hdf_filename, geometry_settings, settings, read_only))
                                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: H5Fopen failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\bim\ifc.py", line 523, in execute_ifc_operator
    result = getattr(operator, "_execute")(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\bim\module\model\product.py", line 358, in _execute
    if obj := profile.DumbProfileGenerator(relating_type).generate():
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\bim\module\model\profile.py", line 85, in generate
    return self.derive_from_cursor()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\bim\module\model\profile.py", line 110, in derive_from_cursor
    return self.create_profile()
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\bim\module\model\profile.py", line 147, in create_profile
    bonsai.core.geometry.edit_object_placement(tool.Ifc, tool.Geometry, tool.Surveyor, obj=obj)
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\core\geometry.py", line 47, in edit_object_placement
    geometry.clear_cache(element)
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\tool\geometry.py", line 118, in clear_cache
    cache = IfcStore.get_cache()
            ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ryan Schultz\AppData\Roaming\Blender Foundation\Blender\4.5\extensions\.local\lib\python3.11\site-packages\bonsai\bim\ifc.py", line 173, in get_cache
    os.remove(IfcStore.cache_path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\Ryan Schultz\\AppData\\Local\\bonsai\\bonsai\\Cache\\7f428058a8b495ff5aede5f4698b3bb5.h5'
Location: C:\Program Files\Blender Foundation\Blender 4.5\4.5\scripts\modules\bpy\ops.py:107

When the hdf5 serializer constructor fails on an existing
file, the HDF5 C library retains the file handle on Windows.
Force gc.collect() before os.remove() to release it, and
catch PermissionError to skip the cache gracefully if the
file is still locked.

Generated with the assistance of an AI coding tool.
@theoryshaw theoryshaw requested a review from Andrej730 March 29, 2026 14:53
@theoryshaw
Copy link
Copy Markdown
Member Author

Hi @Andrej730, can you take a look at this? Might be related to your recent work.
Cheers, Ryan

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.

1 participant