diff --git a/CHANGELOG.rst b/CHANGELOG.rst index bea4608b..548397ae 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,8 @@ Change Log *Fixed:* +* Write all pending index entries to the file when `gsd_flush()` is called after `gsd_write_chunk()` + and before `gsd_end_frame()` (`#319 `__). * Readthedocs builds with pandas 2.2.0 (`#322 `__). diff --git a/gsd/gsd.c b/gsd/gsd.c index 24e70cae..7c254fed 100644 --- a/gsd/gsd.c +++ b/gsd/gsd.c @@ -2012,7 +2012,7 @@ int gsd_flush(struct gsd_handle* handle) { handle->frame_index.data[i] = handle->frame_index - .data[handle->frame_index.size - handle->pending_index_entries]; + .data[handle->frame_index.size - handle->pending_index_entries + i]; } } diff --git a/gsd/test/test_fl.py b/gsd/test/test_fl.py index f3d94294..ba1185fd 100644 --- a/gsd/test/test_fl.py +++ b/gsd/test/test_fl.py @@ -1146,3 +1146,29 @@ def test_file_exists_error(): schema_version=[1, 2], ): pass + + +def test_pending_index_entries(tmp_path): + """Ensure that gsd preserves pending index entries.""" + with gsd.fl.open( + tmp_path / 'test_pending_index_entries.gsd', + 'w', + application='My application', + schema='My Schema', + schema_version=[1, 0], + ) as f: + # Frame 0 must be complete to trigger the bug. + f.write_chunk(name='0', data=numpy.array([0])) + f.end_frame() + + for i in range(16): + f.write_chunk(name=str(i), data=numpy.array([i])) + + # Flush with pending chunks in the frame index. + f.flush() + + f.end_frame() + + # All test chunks should be present in the file. + for i in range(16): + assert f.chunk_exists(name=str(i), frame=1)