Skip to content

fix(index): keep the shuffler scratch directory alive for the whole build - #8993

Merged
zhangyue19921010 merged 4 commits into
lance-format:mainfrom
LuciferYang:fix/ivf-rq-shuffler-scratch-dir-leak
Sep 15, 2026
Merged

zhangyue19921010 merged 4 commits into
lance-format:mainfrom
LuciferYang:fix/ivf-rq-shuffler-scratch-dir-leak

Conversation

@LuciferYang

@LuciferYang LuciferYang commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem

prepare_vector_segment_build creates the shuffle scratch directory as a local TempStdDir and returns only the shuffler, which carries just the directory path. The guard drops when the helper returns, so the directory is removed before the build writes into it. The shuffle writer recreates it, and from then on nothing owns it. Every vector index build leaks one scratch directory, and each one holds a shuffled copy of the vector column.

Fixes #8992.

What this changes

The helper returns the guard alongside the shuffler, and both callers hold it until the build finishes. The incremental build path and the optimize path already keep their guards in the enclosing function scope and are untouched.

#6980 fixed the same thing for the legacy IVF_HNSW partition staging directory.

Test plan

test_shuffle_scratch_dir_is_not_leaked runs two real index builds in a child process with TMPDIR pointed at an isolated directory, then asserts no scratch directory survives. Negative control: with the guard bound as bare _ at the local call site, it fails and lists both directories, one per build. The builds go through create_index, so the test covers the local path; the distributed path needs precomputed centroids and is not exercised by it.

End to end, one index build with TMPDIR pointed at an empty directory:

left behind
abcd800cd .tmpXXXXXX/ with shuffle_data.lance, shuffle_data.spill, shuffle_offsets.lance, shuffle_offsets.spill
this branch nothing

…uild

prepare_vector_segment_build created the shuffler's scratch directory as a local TempStdDir and returned only the shuffler, so the guard dropped when the function returned: the directory was removed before the build wrote into it (the writer recreated it) and nothing ever cleaned it up afterwards, leaking one scratch directory per index build.

Return the guard alongside the shuffler so callers hold it until the build finishes. The incremental build path already keeps its guard in the enclosing function scope and is unchanged.
@github-actions github-actions Bot added the bug Something isn't working label Sep 4, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Sep 4, 2026

@zhangyue19921010 zhangyue19921010 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Overall LGTM. But can we streamline the test cases?

@zhangyue19921010 zhangyue19921010 self-assigned this Sep 15, 2026
@LuciferYang

Copy link
Copy Markdown
Contributor Author

Dropped test_prepare_vector_segment_build_keeps_scratch_dir_alive and kept only the leak test. The helper-contract test pinned that the returned guard owns the shuffler's directory, but a caller that drops the guard early still passes it, and that is the failure mode this PR is about. test_shuffle_scratch_dir_is_not_leaked goes through create_index, so it covers that case too: with the guard bound as bare _ at the local call site it fails and lists both scratch directories, one per build.

The diff is +125/-22 now instead of +202/-22, and the test module's import block is back to untouched.

@zhangyue19921010

Copy link
Copy Markdown
Collaborator

Dropped test_prepare_vector_segment_build_keeps_scratch_dir_alive and kept only the leak test. The helper-contract test pinned that the returned guard owns the shuffler's directory, but a caller that drops the guard early still passes it, and that is the failure mode this PR is about. test_shuffle_scratch_dir_is_not_leaked goes through create_index, so it covers that case too: with the guard bound as bare _ at the local call site it fails and lists both scratch directories, one per build.

The diff is +125/-22 now instead of +202/-22, and the test module's import block is back to untouched.

build_vector_index_in_child_process is this test necessary?

@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Sep 15, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Sep 15, 2026
@LuciferYang

Copy link
Copy Markdown
Contributor Author

Not as a separate test. It only existed as the child process payload, so I folded it into test_shuffle_scratch_dir_is_not_leaked: the test re-executes itself with LANCE_SHUFFLE_LEAK_TEST_ROOT set, takes the child branch when it sees the variable, and returns. The test list has one entry for this now and no #[ignore]d function.

The child's filter comes from std::thread::current().name(), which libtest sets to the running test's path, so it cannot drift out of sync with the function name any more.

Negative control still holds: with the guard bound as bare _ at the local call site the test fails and lists both scratch directories.

@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Sep 15, 2026

@lance-gatekeeper lance-gatekeeper Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Gate recommendation: approve.

The test-only follow-up preserves the production fix and folds the child-process payload into the end-to-end scratch-directory regression. Both shuffler paths now exercise cleanup without retaining a separate ignored child test, and no material risk remains.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Sep 15, 2026

@zhangyue19921010 zhangyue19921010 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@zhangyue19921010
zhangyue19921010 merged commit 7f51716 into lance-format:main Sep 15, 2026
39 checks passed
@LuciferYang

Copy link
Copy Markdown
Contributor Author

Thank you @zhangyue19921010

dacorvo added a commit to huggingface/funes that referenced this pull request Sep 25, 2026
Every IVF index build leaves a `.tmp*` directory behind holding a shuffled
copy of the vector column — tens of MB apiece, one per `funes index` run
that rebuilds the vector index. On a machine that does not clear $TMPDIR
they accumulate without bound; #175 reports 25.5 GB over five days.

lance's `prepare_vector_segment_build` creates the scratch directory, hands
the shuffler its *path*, and returns only the shuffler, so the `TempStdDir`
guard drops on return. strace shows the directory removed while still empty,
then recreated by the shuffle writer through `create_dir_all(parent)` — from
then on nothing owns it. lance fixed this in 13.0.0 (lance-format/lance#8993,
merged a day after v12.0.0-rc.1 was cut); 11 and 12 both leak, so funes has
to clean up after them until the pin can move.

Sweep on the way in, not on the way out: funes' memory lock is per-FUNES_HOME
and does not cover another lance process on the same $TMPDIR, so the sweep
must never touch a directory a build could still be filling. One that is over
an hour old and holds nothing but the four shuffle files is settled — mid-write
object_store's staging files sit alongside them. Steady state is one directory.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Xuanwo added a commit that referenced this pull request Oct 23, 2026
)

## Problem

The v3 shufflers hold only `output_dir: Path`, so whoever creates the
shuffle scratch directory has to keep a `TempStdDir` guard alive for the
whole build. #8993 fixed one instance of that going wrong, but the
invariant it left behind is a naming convention: `let (.., _) =
prepare_vector_segment_build(..)` compiles, binds nothing, drops the
guard at the end of the statement, and the leak is back. `#[must_use]`
does not help, because binding to `_` counts as a use. Three build paths
each kept their own guard alive by hand, and the regression test reaches
only one of them.

Fixes #9264.

## What this changes

`IvfShuffler` and `TwoFileShuffler` can now own the directory they
shuffle into, the way the legacy shuffler already does with
`owned_temp_dir`. A private `ScratchDir` pairs the directory with its
guard in one constructor, so outside the shuffler module a shuffler
cannot be handed a guard for some directory other than the one it writes
into. `create_owned_ivf_shuffler` creates the directory and hands it
over; `create_ivf_shuffler` keeps its signature for the two call sites
that shuffle into a subdirectory of a staging area they clean up
themselves.

The readers get a clone of the guard as well, not just the shufflers.
`IvfIndexBuilder` declares `shuffler` before `shuffle_reader`, so on
drop the reader outlives the shuffler and can be the last thing that
still needs the directory. Without that clone, dropping the shuffler
first would delete the partition files before the reader reads them.

`prepare_vector_segment_build` returns its original 4-tuple again, and
the incremental and optimize paths no longer build a `TempStdDir` of
their own. No existing public item of `lance-index` changes signature;
`create_owned_ivf_shuffler` is new and takes no `tempfile` type, and the
builder methods that do are private.

## Test plan

`test_owned_scratch_dir_outlives_the_shuffler` is an `rstest` over both
shufflers. Each case drops the shuffler, asserts the directory is still
there and reads a partition through the reader, then drops the reader
and asserts the directory is gone. Negative control: with the readers'
guard clone replaced by `None`, both cases fail on the first assertion,
including the `IvfShuffler` case that nothing exercised before.

`test_shuffle_scratch_dir_is_not_leaked` from #8993 is unchanged and
covers the fresh `create_index` build end to end. The incremental,
optimize and distributed paths still have no leak test of their own;
they now go through the same `create_owned_ivf_shuffler` that the
shuffler-level test pins.

Not covered, and worth knowing: `create_shuffler` selects between the
two shufflers on `LANCE_LEGACY_SHUFFLER`, which nothing in the repo
sets, so that branch itself is still driven only by the default arm.

---------

Co-authored-by: Xuanwo <[email protected]>
dacorvo added a commit to huggingface/funes that referenced this pull request Nov 3, 2026
lance 13 holds the IVF shuffler's scratch directory for the whole build
(lance-format/lance#8993), so an index build leaves nothing in $TMPDIR and
the sweep has nothing left to reclaim. The sweep goes, with its unit test,
the tripwire test (which fails on lance 13, as designed) and the Cargo.toml
note that asked for this.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
dacorvo added a commit to huggingface/funes that referenced this pull request Nov 3, 2026
lance 13 holds the IVF shuffler's scratch directory for the whole build
(lance-format/lance#8993), so an index build leaves nothing in $TMPDIR and
the sweep has nothing left to reclaim. The sweep goes, with its unit test,
the tripwire test (which fails on lance 13, as designed) and the Cargo.toml
note that asked for this.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working K-approved Latest Gatekeeper recommendation permits acceptance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: vector index builds leak their shuffle scratch directory

2 participants