From 46640722b405d8b3a1a245a2d9b66dc79dd9a892 Mon Sep 17 00:00:00 2001 From: Ryan Schultz Date: Sun, 29 Mar 2026 09:51:15 -0500 Subject: [PATCH] Fix PermissionError deleting locked HDF5 cache on Windows 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. --- src/bonsai/bonsai/bim/ifc.py | 9 ++++++++- src/bonsai/bonsai/tool/raycast.py | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/bonsai/bonsai/bim/ifc.py b/src/bonsai/bonsai/bim/ifc.py index b07e584a71..7fe03630c1 100644 --- a/src/bonsai/bonsai/bim/ifc.py +++ b/src/bonsai/bonsai/bim/ifc.py @@ -18,6 +18,7 @@ from __future__ import annotations +import gc import hashlib import os import shutil @@ -170,7 +171,13 @@ class IfcStore: # No point to trying again the same operation. return - os.remove(IfcStore.cache_path) + # Force GC to release any dangling HDF5 C-level file handle (Windows). + gc.collect() + try: + os.remove(IfcStore.cache_path) + except PermissionError as e: + print(f"Could not delete locked cache file '{cache_path.name}': {str(e)}. Skipping cache.") + return try: IfcStore.cache = ifcopenshell.geom.serializers.hdf5( IfcStore.cache_path, cache_settings, serializer_settings diff --git a/src/bonsai/bonsai/tool/raycast.py b/src/bonsai/bonsai/tool/raycast.py index 70dae00438..cd5b19a1f2 100644 --- a/src/bonsai/bonsai/tool/raycast.py +++ b/src/bonsai/bonsai/tool/raycast.py @@ -25,6 +25,7 @@ import bmesh import bpy import mathutils import numpy as np +from bpy_extras import view3d_utils from mathutils import Vector import bonsai.core.tool