GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(challenge): purge stale staging temp files · CortexLM/hypertrain@8750a74

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/hypertrain/challenge
    • store.py
  • tests/challenge
    • test_island_job_idempotent.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/hypertrain/challenge/store.py‎

Copy file name to clipboardExpand all lines: src/hypertrain/challenge/store.py
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,20 @@ def _stage_once(path: Path, data: bytes) -> None:
326326
complete or absent; a SIGKILL leaves at most a unique `.name.*.part` temp, never reused."""
327327
import os
328328

329+
# Best-effort purge of this name's crash leftovers; the age floor spares a concurrent
330+
# emission's live temp, and lstat/unlink never follow a planted symlink.
331+
stale = time.time() - 600
332+
try:
333+
for entry in path.parent.iterdir():
334+
name = entry.name
335+
if name.startswith(f".{path.name}.") and name.endswith(".part"):
336+
try:
337+
if not entry.is_symlink() and entry.lstat().st_mtime < stale:
338+
entry.unlink(missing_ok=True)
339+
except OSError:
340+
pass
341+
except OSError:
342+
pass
329343
if not os.path.lexists(path):
330344
fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=f".{path.name}.", suffix=".part")
331345
try:
Collapse file

‎tests/challenge/test_island_job_idempotent.py‎

Copy file name to clipboardExpand all lines: tests/challenge/test_island_job_idempotent.py
+40-1Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
from __future__ import annotations
44

55
import importlib.util
6+
import os
67
import sys
8+
import time
79
from pathlib import Path
810

911
import pytest
1012

1113
from hypertrain.auditor.replay import pack_state
12-
from hypertrain.challenge.store import ChallengeError
14+
from hypertrain.challenge.store import ChallengeError, _stage_once
1315
from hypertrain.data.store import put_large
1416
from hypertrain.protocol.messages_v2 import StartStateV2
1517
from hypertrain.trainer.config import TrainConfig
@@ -106,3 +108,40 @@ def test_island_job_route_second_get_is_200_same_body(network, monkeypatch):
106108
second = network.client.get(network.url + "/rounds/0/job/" + HOT)
107109
assert second.status_code == 200, second.text
108110
assert second.json() == first.json()
111+
112+
113+
def _part(path: Path, data: bytes, age: float = 0.0) -> Path:
114+
path.write_bytes(data)
115+
when = time.time() - age
116+
os.utime(path, (when, when))
117+
return path
118+
119+
120+
def test_stage_once_purges_stale_part_files_of_its_own_name(tmp_path):
121+
stale = _part(tmp_path / ".start_state.abc123.part", b"torn", age=3600)
122+
_stage_once(tmp_path / "start_state", b"x")
123+
assert not stale.exists()
124+
assert (tmp_path / "start_state").read_bytes() == b"x"
125+
126+
127+
def test_stage_once_keeps_fresh_part_files_and_other_names(tmp_path):
128+
stale = _part(tmp_path / ".start_state.abc123.part", b"torn", age=3600)
129+
fresh = _part(tmp_path / ".start_state.def456.part", b"live") # a concurrent emission's temp
130+
other = _part(tmp_path / ".ef_in.zzz.part", b"other", age=3600)
131+
_stage_once(tmp_path / "start_state", b"x")
132+
assert not stale.exists()
133+
assert fresh.read_bytes() == b"live"
134+
assert other.read_bytes() == b"other"
135+
assert (tmp_path / "start_state").read_bytes() == b"x"
136+
137+
138+
def test_stage_once_purge_does_not_follow_symlinks(tmp_path):
139+
target = _part(tmp_path / "outside", b"keep", age=3600)
140+
job = tmp_path / "job"
141+
job.mkdir()
142+
link = job / ".start_state.link.part"
143+
link.symlink_to(target)
144+
os.utime(link, (target.stat().st_mtime,) * 2, follow_symlinks=False) # stale link too
145+
_stage_once(job / "start_state", b"x")
146+
assert link.is_symlink()
147+
assert target.read_bytes() == b"keep"

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.