|
| 1 | +"""Unit tests for MemoryMappedDataset mmap lifecycle. |
| 2 | +
|
| 3 | +Covers the partial-open failure path (prior mmaps must be released so they |
| 4 | +don't leak through exception tracebacks) and the success-path invariant |
| 5 | +(mmaps stay open for the life of the dataset). |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from unittest.mock import patch |
| 11 | + |
| 12 | +import numpy as np |
| 13 | +import pytest |
| 14 | + |
| 15 | +from kempnerforge.data.dataset import MemoryMappedDataset |
| 16 | + |
| 17 | + |
| 18 | +def _write_npy_files(tmp_path, n_files: int, tokens_per_file: int = 1024) -> None: |
| 19 | + for i in range(n_files): |
| 20 | + arr = np.arange(tokens_per_file, dtype=np.uint32) |
| 21 | + np.save(tmp_path / f"shard_{i:03d}.npy", arr) |
| 22 | + |
| 23 | + |
| 24 | +def test_partial_open_failure_closes_prior_mmaps(tmp_path): |
| 25 | + """If np.load raises partway through, the mmaps already opened must be closed. |
| 26 | +
|
| 27 | + Without the fix, the prior mmaps stay live through any exception traceback |
| 28 | + (pytest frames, logger.exception, post-mortem debuggers), accumulating |
| 29 | + virtual-memory mappings on Lustre/NFS clusters under retry loops. |
| 30 | + """ |
| 31 | + _write_npy_files(tmp_path, n_files=5) |
| 32 | + |
| 33 | + original = np.load |
| 34 | + calls = {"n": 0} |
| 35 | + opened_mmaps: list = [] |
| 36 | + |
| 37 | + def flaky(*args, **kwargs): |
| 38 | + calls["n"] += 1 |
| 39 | + if calls["n"] == 3: |
| 40 | + raise RuntimeError("simulated Lustre hiccup") |
| 41 | + mm = original(*args, **kwargs) |
| 42 | + opened_mmaps.append(mm) |
| 43 | + return mm |
| 44 | + |
| 45 | + with ( |
| 46 | + patch("kempnerforge.data.dataset.np.load", side_effect=flaky), |
| 47 | + pytest.raises(RuntimeError, match="Lustre hiccup"), |
| 48 | + ): |
| 49 | + MemoryMappedDataset(str(tmp_path), seq_len=128) |
| 50 | + |
| 51 | + assert len(opened_mmaps) == 2, f"expected 2 opens before failure, got {len(opened_mmaps)}" |
| 52 | + for mm in opened_mmaps: |
| 53 | + inner = getattr(mm, "_mmap", None) |
| 54 | + assert inner is not None |
| 55 | + assert inner.closed, "mmap was not closed after __init__ raised" |
| 56 | + |
| 57 | + |
| 58 | +def test_close_is_idempotent_and_releases_mmaps(tmp_path): |
| 59 | + _write_npy_files(tmp_path, n_files=3) |
| 60 | + ds = MemoryMappedDataset(str(tmp_path), seq_len=128) |
| 61 | + |
| 62 | + inners = [mm._mmap for mm in ds._mmaps] |
| 63 | + assert all(not i.closed for i in inners) |
| 64 | + |
| 65 | + ds.close() |
| 66 | + assert all(i.closed for i in inners) |
| 67 | + |
| 68 | + # Second close is a no-op, not a crash. |
| 69 | + ds.close() |
| 70 | + |
| 71 | + |
| 72 | +def test_successful_init_keeps_mmaps_open(tmp_path): |
| 73 | + """Regression guard: the fix must not close mmaps on the success path.""" |
| 74 | + _write_npy_files(tmp_path, n_files=2) |
| 75 | + ds = MemoryMappedDataset(str(tmp_path), seq_len=128) |
| 76 | + assert all(not mm._mmap.closed for mm in ds._mmaps) |
| 77 | + sample = ds[0] |
| 78 | + assert "input_ids" in sample |
| 79 | + ds.close() |
| 80 | + |
| 81 | + |
| 82 | +def test_partial_open_failure_on_bin_files(tmp_path): |
| 83 | + """Same leak guarantee applies to the .bin branch.""" |
| 84 | + for i in range(4): |
| 85 | + (tmp_path / f"shard_{i:03d}.bin").write_bytes(np.arange(512, dtype=np.uint32).tobytes()) |
| 86 | + |
| 87 | + original = np.memmap |
| 88 | + opened_mmaps: list = [] |
| 89 | + calls = {"n": 0} |
| 90 | + |
| 91 | + def flaky_memmap(*args, **kwargs): |
| 92 | + calls["n"] += 1 |
| 93 | + if calls["n"] == 2: |
| 94 | + raise RuntimeError("simulated bin open failure") |
| 95 | + mm = original(*args, **kwargs) |
| 96 | + opened_mmaps.append(mm) |
| 97 | + return mm |
| 98 | + |
| 99 | + with ( |
| 100 | + patch("kempnerforge.data.dataset.np.memmap", side_effect=flaky_memmap), |
| 101 | + pytest.raises(RuntimeError, match="bin open failure"), |
| 102 | + ): |
| 103 | + MemoryMappedDataset(str(tmp_path), seq_len=128, file_pattern="*.bin") |
| 104 | + |
| 105 | + assert len(opened_mmaps) == 1 |
| 106 | + inner = getattr(opened_mmaps[0], "_mmap", None) |
| 107 | + assert inner is not None and inner.closed |
0 commit comments