-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
297 lines (257 loc) · 10.8 KB
/
utils.py
File metadata and controls
297 lines (257 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""Utility for SWE-bench agent service."""
import logging
import os
import shutil
import threading
import time
from collections import OrderedDict, defaultdict
from contextlib import contextmanager
from pathlib import Path
import docker
logger = logging.getLogger(__name__)
class ImageLRUCache:
"""Thread-safe LRU cache for Docker images."""
def __init__(self, max_size: int | None = None) -> None:
"""Initialize the cache.
Args:
max_size: Maximum number of images to retain.
"""
self.max_size = (
max_size
if max_size is not None
else int(os.getenv("IMAGE_CACHE_MAX_SIZE", "20"))
)
self.cache = OrderedDict() # {image_key: timestamp}
self.usage_count = defaultdict(
int
) # {image_key: int} - reference count for images in use
self.deleting = set() # {image_key} - images currently being deleted
self.lock = threading.Lock()
self.condition = threading.Condition(self.lock)
def cleanup_old_images(self, client: docker.DockerClient) -> None:
"""Remove the oldest images while the cache size exceeds max_size."""
max_attempts = 50
attempts = 0
while attempts < max_attempts:
# Step 1: Find the candidate image to remove
candidate_key = None
with self.lock:
# Calculate the effective size (current size - images being deleted)
effective_keys = [k for k in self.cache if k not in self.deleting]
if len(effective_keys) <= self.max_size:
break
# Find a candidate from the effective keys
# Since effective_keys preserves order (from OrderedDict), the first one is the oldest
for key in effective_keys:
# Check if the image is in use
if self.usage_count[key] > 0:
self.cache.move_to_end(key)
# Modified the dict, so restart the search to be safe
candidate_key = None
break
# Found a valid candidate
candidate_key = key
self.deleting.add(candidate_key)
break
if candidate_key is None:
# Either moved something to end (LRU update) or didn't find any candidate
attempts += 1
continue
# Step 2: Remove image from Docker
if candidate_key is None:
break
removed = False
try:
if self._image_exists(client, candidate_key):
client.images.remove(candidate_key, force=True)
removed = True
else:
# Image doesn't exist in Docker, can remove from the cache
removed = True
except Exception as e:
logger.warning(f"Failed to remove image {candidate_key}: {e}")
attempts += 1
# Continue to clean up the 'deleting' set
# Step 3: Update cache if removal succeeded and the image is still not in use (in lock)
with self.lock:
self.deleting.remove(candidate_key)
self.condition.notify_all()
if (
removed
and candidate_key in self.cache
and self.usage_count[candidate_key] == 0
):
del self.cache[candidate_key]
attempts = 0 # Successfully removed, reset counter
else:
# Image became in-use during deletion or failed to remove
attempts += 1
@staticmethod
def _image_exists(client: docker.DockerClient, image_key: str) -> bool:
"""Check if the image exists in Docker."""
try:
client.images.get(image_key)
return True
except docker.errors.ImageNotFound:
return False
@contextmanager
def use(self, image_key: str):
"""Context manager to safely manage image reference counting.
Automatically marks image as accessed (updates LRU order) and manages
reference counting. Increments reference count on entry and decrements
on exit.
Args:
image_key: Image identifier to protect
"""
with self.lock:
wait_timeout = float(os.getenv("IMAGE_CACHE_DELETE_WAIT_TIMEOUT", "1200"))
start_time = time.monotonic()
while image_key in self.deleting:
remaining = wait_timeout - (time.monotonic() - start_time)
if remaining <= 0:
raise TimeoutError(
f"Timed out waiting for image deletion lock: {image_key}"
)
self.condition.wait(timeout=remaining)
if image_key in self.cache:
self.cache.move_to_end(image_key)
else:
self.cache[image_key] = time.time()
self.usage_count[image_key] += 1
try:
yield
finally:
with self.lock:
if image_key in self.usage_count:
self.usage_count[image_key] -= 1
if self.usage_count[image_key] <= 0:
del self.usage_count[image_key]
@contextmanager
def get_docker_client():
"""Context manager for the Docker client to ensure proper cleanup.
Docker connection can be configured via the DOCKER_HOST environment variable.
Both Docker Python SDK (used here) and docker CLI (used by DockerEnvironment)
will respect this environment variable.
Examples:
- Local socket: unix:///var/run/docker.sock (default)
- Remote daemon: tcp://192.168.1.100:2375
"""
docker_host = os.getenv("DOCKER_HOST", "unix:///var/run/docker.sock")
client = docker.DockerClient(base_url=docker_host)
try:
yield client
finally:
try:
client.close()
except Exception as e:
logger.warning(f"Failed to close Docker client: {e}")
def ensure_image_loaded(client: docker.DockerClient, image_key: str) -> None:
"""
Ensure the Docker image is loaded from the tar file if not already present.
Args:
client: Docker client instance
image_key: Image identifier
"""
# Check if the image exists
try:
client.images.get(image_key)
logger.debug(f"Image {image_key} already exists, skipping load")
return # Image exists, no need to load
except docker.errors.ImageNotFound:
pass # Image doesn't exist, need to load
# Load image from the tar file or pull from Docker Hub
images_path_str = os.getenv("SWE_BENCH_IMAGES_PATH")
if images_path_str:
# Load from local tar file
images_path = Path(images_path_str)
tar_filename = f"{image_key.replace('/', '_').replace(':', '_')}.tar"
tar_path = images_path / tar_filename
if not tar_path.exists():
raise Exception(
f"Image tar file not found: {tar_path} for remote image {image_key}"
)
else:
# Pull from Docker Hub with retry
max_retries = 3
retry_delay = 1 # seconds
for attempt in range(max_retries):
try:
logger.info(
f"Pulling image from Docker Hub: {image_key} (attempt {attempt + 1}/{max_retries})"
)
client.images.pull(image_key)
logger.info(f"Successfully pulled image {image_key} from Docker Hub")
return
except Exception as e:
# Check if the image now exists (another process might have pulled it)
try:
client.images.get(image_key)
logger.info(f"Image {image_key} was pulled by another process")
return
except docker.errors.ImageNotFound:
if attempt < max_retries - 1:
logger.warning(
f"Failed to pull image (attempt {attempt + 1}/{max_retries}): {e}"
)
logger.info(f"Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
retry_delay *= 2 # Exponential backoff
else:
logger.error(
f"Failed to pull image after {max_retries} attempts: {e}"
)
raise
max_retries = 3
retry_delay = 1 # seconds
for attempt in range(max_retries):
try:
logger.info(
f"Loading image from local tar file: {tar_path} (attempt {attempt + 1}/{max_retries})"
)
with tar_path.open("rb") as f:
client.images.load(f)
logger.info(f"Successfully loaded image {image_key} from {tar_path}")
return
except Exception as e:
# Check if the image now exists (another thread might have loaded it)
try:
client.images.get(image_key)
logger.info(f"Image {image_key} was loaded by another thread")
return
except docker.errors.ImageNotFound:
# Image still doesn't exist
if attempt < max_retries - 1:
logger.warning(
f"Failed to load image (attempt {attempt + 1}/{max_retries}): {e}"
)
logger.info(f"Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
retry_delay *= 2 # Exponential backoff
else:
# The final attempt failed, re-raise the error
logger.error(
f"Failed to load image from tar file after {max_retries} attempts: {e}"
)
raise
def cleanup_logs(run_id: str) -> None:
"""
Clean up log files and intermediate results after evaluation.
Removes the specific run_id directory from logs/run_evaluation/.
Controlled by SWE_BENCH_CLEANUP_LOGS environment variable (default: true).
"""
from swebench.harness.constants import RUN_EVALUATION_LOG_DIR
# Check if cleanup is enabled via environment variable
cleanup_enabled = os.getenv("SWE_BENCH_CLEANUP_LOGS", "true").lower() in {
"true",
"1",
"yes",
}
if not cleanup_enabled:
return
log_dir = Path(RUN_EVALUATION_LOG_DIR) / run_id
try:
if log_dir.exists():
shutil.rmtree(log_dir)
logger.debug(f"Cleaned up logs for run_id: {run_id}")
except Exception as e:
logger.warning(f"Failed to clean up logs for {run_id}: {e}")