|
7 | 7 |
|
8 | 8 | import ast
|
9 | 9 | import ctypes
|
| 10 | +import errno |
10 | 11 | import functools
|
11 | 12 | import hashlib
|
12 | 13 | import inspect
|
|
17 | 18 | import os
|
18 | 19 | import platform
|
19 | 20 | import sys
|
| 21 | +import time |
20 | 22 | import types
|
21 | 23 | import typing
|
22 | 24 | import weakref
|
@@ -2131,12 +2133,34 @@ def load(self, device, block_dim=None) -> ModuleExec:
|
2131 | 2133 | # -----------------------------------------------------------
|
2132 | 2134 | # update cache
|
2133 | 2135 |
|
2134 |
| - try: |
2135 |
| - # Copy process-specific build directory to a process-independent location |
2136 |
| - os.rename(build_dir, module_dir) |
2137 |
| - except (OSError, FileExistsError): |
2138 |
| - # another process likely updated the module dir first |
2139 |
| - pass |
| 2136 | + def safe_rename(src, dst, attempts=5, delay=0.1): |
| 2137 | + for i in range(attempts): |
| 2138 | + try: |
| 2139 | + os.rename(src, dst) |
| 2140 | + return |
| 2141 | + except FileExistsError: |
| 2142 | + return |
| 2143 | + except OSError as e: |
| 2144 | + if e.errno == errno.ENOTEMPTY: |
| 2145 | + # if directory exists we assume another process |
| 2146 | + # got there first, in which case we will copy |
| 2147 | + # our output to the directory manually in second step |
| 2148 | + return |
| 2149 | + else: |
| 2150 | + # otherwise assume directory creation failed e.g.: access denied |
| 2151 | + # on Windows we see occasional failures to rename directories due to |
| 2152 | + # some process holding a lock on a file to be moved to workaround |
| 2153 | + # this we make multiple attempts to rename with some delay |
| 2154 | + if i < attempts - 1: |
| 2155 | + time.sleep(delay) |
| 2156 | + else: |
| 2157 | + print( |
| 2158 | + f"Could not update Warp cache with module binaries, trying to rename {build_dir} to {module_dir}, error {e}" |
| 2159 | + ) |
| 2160 | + raise e |
| 2161 | + |
| 2162 | + # try to move process outputs to cache |
| 2163 | + safe_rename(build_dir, module_dir) |
2140 | 2164 |
|
2141 | 2165 | if os.path.exists(module_dir):
|
2142 | 2166 | if not os.path.exists(binary_path):
|
|
0 commit comments