Skip to content

Commit 93cbd54

Browse files
committed
Try to fix segfault/bus error by holding onto temp dir
1 parent 6a3e1d4 commit 93cbd54

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

optimum/executorch/modeling.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,30 @@ def __init__(self, models: Dict[str, "ExecuTorchModule"], config: "PretrainedCon
102102
setattr(self, key, value)
103103

104104
self.stats = Stats()
105+
106+
# Initialize cleanup tracking
107+
self._temp_dir = None
108+
109+
def __del__(self):
110+
"""Clean up temporary files when the model instance is destroyed."""
111+
self._cleanup_temp_resources()
112+
113+
def _cleanup_temp_resources(self):
114+
"""Clean up temporary directory and files."""
115+
if hasattr(self, '_temp_dir') and self._temp_dir is not None:
116+
try:
117+
if hasattr(self._temp_dir, 'cleanup'):
118+
# It's a TemporaryDirectory object
119+
self._temp_dir.cleanup()
120+
elif hasattr(self._temp_dir, 'rmtree') or isinstance(self._temp_dir, (str, Path)):
121+
# It's a path
122+
import shutil
123+
shutil.rmtree(self._temp_dir, ignore_errors=True)
124+
except Exception:
125+
# Ignore cleanup errors to avoid issues during shutdown
126+
pass
127+
finally:
128+
self._temp_dir = None
105129

106130
@abstractmethod
107131
def forward(self, *args, **kwargs):
@@ -274,6 +298,10 @@ def _export(
274298
for name, _ in executorch_progs.items():
275299
models.update(cls._from_pretrained(save_dir_path, file_name=f"{name}.pte", config=config))
276300

301+
# Store the TemporaryDirectory reference in each model to prevent GC
302+
for model in models.values():
303+
model._temp_dir = save_dir
304+
277305
return models
278306

279307
def _save_pretrained(self, save_directory):

0 commit comments

Comments
 (0)