Skip to content

[c++] fail on incomplete binary file writes - #7348

Merged
jameslamb merged 3 commits into
lightgbm-org:masterfrom
wgu9:fix-save-binary-short-write
Jul 2, 2026
Merged

jameslamb merged 3 commits into
lightgbm-org:masterfrom
wgu9:fix-save-binary-short-write

Conversation

@wgu9

@wgu9 wgu9 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Fixes #7334.

What changed

Dataset.save_binary() now fails immediately if the local file writer writes fewer bytes than requested. Previously, LocalFile::Write() collapsed any short fwrite() to 0, but callers did not check the returned byte count, so save_binary() could return successfully after producing a truncated binary file.

This changes the local writer to report partial writes via Log::Fatal, which also covers aligned writes used by dataset headers, metadata, and feature groups.

Tests

  • cmake -B build -S . -DUSE_OPENMP=OFF
  • cmake --build build --target _lightgbm -j4
  • sh build-python.sh install --precompile
  • python -m pytest tests/python_package_test/test_basic.py::test_save_binary_raises_on_truncated_write tests/python_package_test/test_basic.py::test_save_dataset_subset_and_load_from_file -q
  • python -m py_compile tests/python_package_test/test_basic.py
  • python -m ruff check tests/python_package_test/test_basic.py
  • pre-commit run --files src/io/file_io.cpp tests/python_package_test/test_basic.py
  • git diff --check

@jameslamb jameslamb changed the title [io] fail on incomplete binary file writes [c++] fail on incomplete binary file writes Jul 2, 2026

@jameslamb jameslamb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a great fix, thanks very much!!

It seems very likely that you used AI to write this... if you come back in the future to contribute here, please write PR titles and descriptions yourself. We're happy to talk to you, but not to talk to an LLM through you.


data = rng.standard_normal(size=(1000, 20))
ds = lgb.Dataset(data).construct()
original_limit = resource.getrlimit(resource.RLIMIT_FSIZE)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could see this behavior being useful in other tests too, might be worth putting into a function-scoped fixture in the future, named like very_limited_disk or something. If that interests you and you agree, you're welcome to add that here.

Not required though, it could be a followup.

@jameslamb jameslamb added the fix label Jul 2, 2026
@wgu9

wgu9 commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks James, really appreciate the review.

Yeah, the fixture idea makes sense. Since this one is already approved, I will leave it as a follow-up for now instead of adding more churn here.

And fair point on the title / description. I will keep future PRs more direct and in my own voice.

@jameslamb
jameslamb merged commit a3a682b into lightgbm-org:master Jul 2, 2026
60 checks passed
@wgu9
wgu9 deleted the fix-save-binary-short-write branch July 2, 2026 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dataset.save_binary() silently produces a corrupt file when disk fills during write

2 participants