fix: force contiguous copy for sliced list arrays in embed_array_storage
Summary
Fixes SIGKILL crash in embed_array_storage when processing sliced/sharded datasets with nested types like Sequence(Nifti()) or Sequence(Image()).
Root cause: When ds.shard() or ds.select() creates a sliced view, array.values on a sliced ListArray returns values with internal offset references. For nested types, PyArrow's C++ layer can crash (SIGKILL, exit code 137) when materializing these sliced nested structs.
Fix: Force a contiguous copy via pa.concat_arrays([array]) when the array has a non-zero offset before processing list/large_list arrays.
Changes
- Add offset check in
embed_array_storagefor list/large_list arrays - Force contiguous copy when
array.offset > 0to break internal references - Add regression tests for sliced arrays with Image, Nifti, and LargeList types
Test plan
- [x] Added
tests/features/test_embed_storage_sliced.pywith 3 tests:test_embed_array_storage_sliced_list_imagetest_embed_array_storage_sliced_list_niftitest_embed_array_storage_sliced_large_list
- [x] All tests verify
embedded.offset == 0(contiguous result) - [x] All tests pass locally
- [x] ruff check passes
Context
This was discovered while uploading a 270GB neuroimaging dataset (ARC) with Sequence(Nifti()) columns. The process crashed with SIGKILL (no Python traceback) when embed_table_storage was called on sharded data.
Workaround that confirmed the fix: pandas round-trip (shard.to_pandas() → Dataset.from_pandas()) which forces a contiguous copy.
Fixes #7894 Related: #6686, #7852, #6790
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.
Hi @The-Obstacle-Is-The-Way, I haven't had a chance to try your repro at https://github.com/huggingface/datasets/issues/7894#issuecomment-3618888430 but I tried the test functions in your PRs and they all pass without your change, is it expected ? (I ran the tests without the checks on the offsets)
Hi @lhoestq, you're right - the tests pass without the fix.
I think the SIGKILL crash is scale-dependent. As I documented in #7894, it only manifests with large, real-world data (the 273GB ARC dataset) and other large dataset upload issues, not with synthetic test files:
| Data | Crash? |
|---|---|
| Synthetic 64³ NIfTI | ✅ No crash |
| Real ARC (273GB) | ❌ SIGKILL |
The tests verify that the fix's mechanism works (sliced arrays get made contiguous), but they can't reproduce the crash because that requires the full-scale data.
I understand this makes it harder to review - the fix is essentially defensive against a crash that's difficult to reproduce in CI. A few options:
- Keep as-is - the fix is low-risk (
pa.concat_arrayson sliced list arrays) and I've confirmed it resolves the real-world crash - Simplify tests - remove the offset assertions if they're confusing, keep just the "doesn't crash" aspect
- Close - if the crash can't be reproduced in CI, I understand if this doesn't meet the bar for merging
Happy to do whatever you think is best. Thank you for your time and dedication!