Repository navigation
fix: finalize the ArrowWriter on a clean context-manager exit - #9113
Open
horizonbymuneeb wants to merge 1 commit into
Open
horizonbymuneeb wants to merge 1 commit into
horizonbymuneeb wants to merge 1 commit into
Conversation
`ArrowWriter` is a context manager, so `with ArrowWriter(path=...) as writer: writer.write(...)` is the natural usage, but `__exit__` only called `close()`. `close()` closes `pa_writer` and the stream without flushing anything: the examples staged in `current_examples` only reach the file through `finalize()`. Everything written below `writer_batch_size` (1000 by default) was therefore dropped, leaving a 0-byte file that pyarrow cannot read. `__exit__` now calls `finalize()` when the block exits cleanly and there is staged data. It keeps the previous quiet `close()` when the body raised, when nothing was written (there is no schema to infer, so `finalize()` would raise `SchemaInferenceError`), or when the caller already finalized. Found by reading the issue; reproduced with three staged examples, which produced a 0-byte file before the fix and a readable 432-byte stream with three rows after it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ArrowWriteris a context manager, but leaving the block silently discards everything still staged in the buffer:Root cause
__exit__calledclose():close()closespa_writerand the stream, but it does not flush anything.write()buffers examples incurrent_examplesuntil it haswriter_batch_sizeof them (1000 by default) and onlyfinalize()writes that remainder out, so any block that wrote fewer than a full batch lost all of its data and left a 0-byte file. The context manager was usable in the only way that looked natural and broken in exactly that way.Fix
finalize()writes the staged examples, closes the writer and closes the stream, so the file is complete and the existing "the stream is closed after the block" guarantee still holds.finalize()is deliberately not called in three cases, to keep the previous behaviour rather than introduce new failures:close()only — the data is incomplete by definitionclose()only — there is no schema to infer andfinalize()would raiseSchemaInferenceErrorfinalize()/ closed the streamclose()only — the file is already completeTests
test_arrow_writer_context_manager_writes_staged_exampleswrites three examples through the context manager and asserts the stream is closed, the file is non-empty, and it reads back as three rows.Mutation check — the new test fails without the fix:
src/datasets/arrow_writer.pyrevertedassert os.path.getsize(path) > 0→AssertionError: assert 0 > 0pytest tests/test_arrow_writer.pyruff check/ruff format --checkThe existing
test_arrow_writer_closes_stream, which covers both the raising and the non-raising path, still passes, so no caller that relied on the old behaviour changes.Compatibility
Behaviour change only for code that relied on the data loss: a
with ArrowWriter(...)block now leaves a readable file instead of a 0-byte one. Explicitclose()withoutfinalize()keeps its current semantics, so anything that manages the writer by hand is unaffected.