From 45d43160b4d9dbe57968fda945354ff4bc94aff3 Mon Sep 17 00:00:00 2001 From: Eric StJohn Date: Fri, 3 Oct 2025 10:20:19 -0700 Subject: [PATCH 1/3] Improve unique directory generation for temp files Refactor unique directory creation logic to use random file names. --- src/Microsoft.ML.Core/Data/Repository.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.ML.Core/Data/Repository.cs b/src/Microsoft.ML.Core/Data/Repository.cs index ff87dc413b..73c40a0fb1 100644 --- a/src/Microsoft.ML.Core/Data/Repository.cs +++ b/src/Microsoft.ML.Core/Data/Repository.cs @@ -123,10 +123,14 @@ private static string GetShortTempDir(IExceptionContext ectx) string tempPath = ectx is IHostEnvironmentInternal iHostInternal ? iHostInternal.TempFilePath : Path.GetTempPath(); - int dirNumber = 0; - string mlNetTempDir = null!; - while (Directory.Exists(mlNetTempDir = Path.Combine(Path.GetFullPath(tempPath), $"ml_dotnet{dirNumber++}"))) ; - var path = Path.Combine(mlNetTempDir, Path.GetRandomFileName()); + + // Find a unique directory, the directory under Temp and must be unique to this process + string path = null; + while(path is null || Directory.Exists(path)) + { + path = Path.Combine(Path.GetFullPath(tempPath), $"ml_dotnet_{Path.GetRandomFileName()}"); + } + Directory.CreateDirectory(path); return path; } From 1a4856ccfdd2ab2d49cf4076991d252d4afc846c Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed <10833894+tarekgh@users.noreply.github.com> Date: Fri, 3 Oct 2025 10:46:31 -0700 Subject: [PATCH 2/3] Update src/Microsoft.ML.Core/Data/Repository.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Microsoft.ML.Core/Data/Repository.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.ML.Core/Data/Repository.cs b/src/Microsoft.ML.Core/Data/Repository.cs index 73c40a0fb1..1c5421757c 100644 --- a/src/Microsoft.ML.Core/Data/Repository.cs +++ b/src/Microsoft.ML.Core/Data/Repository.cs @@ -126,7 +126,7 @@ private static string GetShortTempDir(IExceptionContext ectx) // Find a unique directory, the directory under Temp and must be unique to this process string path = null; - while(path is null || Directory.Exists(path)) + while (path is null || Directory.Exists(path)) { path = Path.Combine(Path.GetFullPath(tempPath), $"ml_dotnet_{Path.GetRandomFileName()}"); } From 17733bb7136b329c1f92e8348f425831a75ddbe1 Mon Sep 17 00:00:00 2001 From: Eric StJohn Date: Fri, 3 Oct 2025 11:02:17 -0700 Subject: [PATCH 3/3] Fix build --- src/Microsoft.ML.Core/Data/Repository.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.ML.Core/Data/Repository.cs b/src/Microsoft.ML.Core/Data/Repository.cs index 1c5421757c..5685582d39 100644 --- a/src/Microsoft.ML.Core/Data/Repository.cs +++ b/src/Microsoft.ML.Core/Data/Repository.cs @@ -128,9 +128,9 @@ private static string GetShortTempDir(IExceptionContext ectx) string path = null; while (path is null || Directory.Exists(path)) { - path = Path.Combine(Path.GetFullPath(tempPath), $"ml_dotnet_{Path.GetRandomFileName()}"); + path = Path.Combine(Path.GetFullPath(tempPath), $"ml_dotnet_{Path.GetFileNameWithoutExtension(Path.GetRandomFileName())}"); } - + Directory.CreateDirectory(path); return path; }