Skip to content

Conversation

@eriawan
Copy link
Member

@eriawan eriawan commented Dec 28, 2025

Fix #84344 to ensure sufficient stack by calling RuntimeHelpers.EnsureSufficientExecutionStack() when calling Directory.Delete recursively.

It is implemented in both FileSystem.Unix.cs and FileSystem.Windows.cs

I'm creating in draft first, to check the CI.
Looks like this PR needs to have a unit test to cover at least the same scenario as mentioned in the original issue, but unfortunately I could not find the place or file to add a unit test for calling Directory.Delete recursively.

PS: apologize it takes me more than 3 months to submit this PR, as I finally have been able to test to run locally 🙏

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Dec 28, 2025
…ient stack space when calling Directory.Delete recursively.

It is implemented in both FileSystem.Unix.cs and FileSystem.Windows.cs
@eriawan eriawan force-pushed the fix-recursive-system.io-directory-dotnetruntime-issue branch from 1994d04 to b713486 Compare December 29, 2025 13:29
@eriawan
Copy link
Member Author

eriawan commented Dec 29, 2025

Rebase as of 29th December, 2025.

@eriawan
Copy link
Member Author

eriawan commented Dec 29, 2025

Looks like the CI is green (before rebase). I think I will mark this PR as ready (not DRAFT anymore).

@eriawan eriawan marked this pull request as ready for review December 29, 2025 14:47
Copilot AI review requested due to automatic review settings December 29, 2025 14:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issue #84344 by preventing stack overflow exceptions when recursively deleting deeply nested directory structures. The fix adds stack space validation before making recursive calls in Directory.Delete.

Key Changes

  • Added RuntimeHelpers.EnsureSufficientExecutionStack() calls before recursive directory deletion to prevent stack overflow
  • Applied consistently to both Windows and Unix filesystem implementations

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.Windows.cs Added using directive and stack validation before recursive RemoveDirectoryRecursive call
src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.Unix.cs Added using directive and stack validation before recursive RemoveDirectoryRecursive call

…ue, and ensure the nested depth is large enough but not too large as the original scenario (14000). I use 6000 to test whether the stack is still sufficient and run without exception thrown.
@eriawan
Copy link
Member Author

eriawan commented Dec 29, 2025

Done adding unit test for this specific nesting scenario, using 6000 depth nesting instead of original 14000 as the test will run a long time if we use the original 14000 as the depth.
The test should be ok on both Windows, Unix (Linux) and others as long as it supports Directory.Delete and RuntimeHelpers.EnsureSufficientExecutionStack()

@dotnet/area-system-io please review.

Let me know if the tag is incorrect so I should tag the right team.

…the depth to 3000 to avoid System.IO.PathTooLongException on non Windows.
@eriawan
Copy link
Member Author

eriawan commented Dec 29, 2025

Update to limit the depth to only 3000 for non Windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.IO community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Directory.Delete(string, bool) throws StackOverflowException

1 participant