Skip to content

Commit fe31c5b

Browse files
fix: return parsed session when post-parse getmtime fails
1 parent d8b87b0 commit fe31c5b

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

tests/test_session_cache.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,22 @@ def counting_parse(p: str):
128128
def test_set_max_entries_rejects_negative() -> None:
129129
with pytest.raises(ValueError, match="non-negative"):
130130
set_max_entries(-1)
131+
132+
133+
def test_returns_parsed_when_mtime_after_parse_raises(
134+
sample_session: Path, monkeypatch: pytest.MonkeyPatch
135+
) -> None:
136+
path = str(sample_session)
137+
real_getmtime = os.path.getmtime
138+
calls = 0
139+
140+
def getmtime_side_effect(p: str) -> float:
141+
nonlocal calls
142+
calls += 1
143+
if calls == 2:
144+
raise OSError("file removed after parse")
145+
return real_getmtime(p)
146+
147+
monkeypatch.setattr(os.path, "getmtime", getmtime_side_effect)
148+
result = get_cached_session(path)
149+
assert result == parse_session(path)

utils/session_cache.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ def get_cached_session(path: str) -> SessionDict:
3232
_cache.move_to_end(abspath)
3333
return hit[1]
3434
parsed = parse_session(abspath)
35-
mtime_after = os.path.getmtime(abspath)
35+
try:
36+
mtime_after = os.path.getmtime(abspath)
37+
except OSError:
38+
return parsed
3639
with _lock:
3740
if _max_entries > 0 and mtime_after == mtime_before:
3841
_cache[abspath] = (mtime_after, parsed)

0 commit comments

Comments
 (0)