Skip to content

[SPARK-57530][CORE] Make SparkFileUtils.recursiveList null-safe and linear-time#56590

Draft
LuciferYang wants to merge 1 commit into
apache:masterfrom
LuciferYang:SPARK-recursivelist-robustness
Draft

[SPARK-57530][CORE] Make SparkFileUtils.recursiveList null-safe and linear-time#56590
LuciferYang wants to merge 1 commit into
apache:masterfrom
LuciferYang:SPARK-recursivelist-robustness

Conversation

@LuciferYang

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

SparkFileUtils.recursiveList walks a directory tree by reading File.listFiles and draining a buffer used as a work queue. Two problems:

  • It calls listFiles without a null check. File.listFiles returns null when a directory cannot be read (an IO error, or the directory being removed during the walk), so the walk could throw NullPointerException.
  • It takes the next directory to visit with Buffer.remove(0), which is O(n); on a wide tree the whole walk is O(n^2).

This PR rewrites the traversal to:

  • null-guard both listFiles calls, skipping (and logging) a directory that cannot be listed instead of throwing;
  • use a Queue with O(1) dequeue, so the walk is linear in the number of entries.

It also drops the now-clearly-dead Option(...).getOrElse(Array.empty) guard at the RocksDBFileManager call site, since recursiveList never returns null. For a readable tree the result (the set of files and directories, and the traversal order) is unchanged.

Why are the changes needed?

recursiveList runs on real paths, such as the RocksDB state-store file manager and LocalSparkCluster. A directory that becomes unreadable mid-walk would crash the walk with an NPE instead of returning what it could, and the O(n^2) remove(0) is needless overhead on directories with many entries.

Does this PR introduce any user-facing change?

No. For a readable directory tree the returned entries are identical; the only difference is that a directory which cannot be listed is now skipped with a warning instead of raising an internal NullPointerException.

How was this patch tested?

Added SparkFileUtilsSuite with three cases: a nested listing, a root whose listFiles returns null, and a subdirectory whose listFiles returns null mid-walk (with a spy confirming non-directories are not recursed into). The two null cases throw on the old code and pass with the fix. build/sbt 'common-utils/testOnly *SparkFileUtilsSuite' passes.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Opus 4.8)

…inear-time

`SparkFileUtils.recursiveList` dereferenced `File.listFiles` without a null
check (it returns null on an IO error or if the directory is removed during the
walk), so it could throw NullPointerException; it also used `Buffer.remove(0)`
as a queue, which is O(n) and made the walk O(n^2) on wide trees.

Rewrite the traversal to null-guard both `listFiles` calls (skipping and logging
an unreadable directory instead of throwing) and to use a queue with O(1)
dequeue, keeping it linear. Drop the now-clearly-dead
`Option(...).getOrElse(Array.empty)` guard at the `RocksDBFileManager` call site,
since `recursiveList` never returns null. Add `SparkFileUtilsSuite`.
@LuciferYang LuciferYang marked this pull request as draft June 18, 2026 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant