From 322e89cf668855bc089bf62dd2c9b92f703a6e38 Mon Sep 17 00:00:00 2001 From: "Joshua A. Anderson" Date: Fri, 19 Jan 2024 14:27:11 -0500 Subject: [PATCH 1/4] Add unit test that triggers incorrect behavior with pending index entries. --- gsd/test/test_fl.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/gsd/test/test_fl.py b/gsd/test/test_fl.py index f3d94294..bdfec68b 100644 --- a/gsd/test/test_fl.py +++ b/gsd/test/test_fl.py @@ -1146,3 +1146,24 @@ 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) From 45b5a4e6e915d05eb2c7729b93cf78ec0bbe5925 Mon Sep 17 00:00:00 2001 From: "Joshua A. Anderson" Date: Fri, 19 Jan 2024 14:28:15 -0500 Subject: [PATCH 2/4] Retain all pending index entries. --- gsd/gsd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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]; } } From 04dd2a9121e865635482a14c8a105a46d54a5ac4 Mon Sep 17 00:00:00 2001 From: "Joshua A. Anderson" Date: Fri, 19 Jan 2024 14:33:36 -0500 Subject: [PATCH 3/4] Run pre-commit. --- gsd/test/test_fl.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/gsd/test/test_fl.py b/gsd/test/test_fl.py index bdfec68b..ba1185fd 100644 --- a/gsd/test/test_fl.py +++ b/gsd/test/test_fl.py @@ -1150,8 +1150,13 @@ def test_file_exists_error(): 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: + 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() From a32874b8cecd459a4a76ee6f760fbea2ab4f5350 Mon Sep 17 00:00:00 2001 From: "Joshua A. Anderson" Date: Mon, 22 Jan 2024 10:10:43 -0500 Subject: [PATCH 4/4] Update change log. --- CHANGELOG.rst | 2 ++ 1 file changed, 2 insertions(+) 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 `__).