Skip to content

Commit 9398f3f

Browse files
authored
Try again if rmtree raises OsError: Directory not empty (#123)
I get this error a lot when rmtree tries to remove the directory. I am not sure why it happens, but let's just try again in this case.
1 parent 419d853 commit 9398f3f

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pyodide_build/common.py

+19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import subprocess
1010
import sys
1111
import textwrap
12+
import time
1213
import tomllib
1314
import warnings
1415
import zipfile
@@ -493,3 +494,21 @@ def download_and_unpack_archive(
493494
# https://github.com/python/cpython/issues/112760
494495
warnings.simplefilter("ignore")
495496
shutil.unpack_archive(str(f_path), path)
497+
498+
499+
def retrying_rmtree(d):
500+
"""Sometimes rmtree fails with OSError: Directory not empty
501+
502+
Try again a few times if this happens.
503+
See: https://github.com/python/cpython/issues/128076
504+
"""
505+
for _ in range(3):
506+
try:
507+
return shutil.rmtree(d)
508+
except OSError as e:
509+
if e.strerror == "Directory not empty":
510+
# wait a bit and try again up to 3 tries
511+
time.sleep(0.01)
512+
else:
513+
raise
514+
raise RuntimeError(f"shutil.rmtree('{d}') failed with ENOTEMPTY three times")

pyodide_build/recipe/builder.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
make_zip_archive,
3838
modify_wheel,
3939
retag_wheel,
40+
retrying_rmtree,
4041
)
4142
from pyodide_build.logger import logger
4243
from pyodide_build.recipe.bash_runner import (
@@ -243,7 +244,7 @@ def _prepare_source(self) -> None:
243244

244245
# clear the build directory
245246
if self.build_dir.resolve().is_dir():
246-
shutil.rmtree(self.build_dir)
247+
retrying_rmtree(self.build_dir)
247248

248249
self.build_dir.mkdir(parents=True, exist_ok=True)
249250

0 commit comments

Comments
 (0)