-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Use GetRandomFileName when creating random temp folder to avoid conflict #5229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the difference between the default seed of Random()
and Environment.TickCount
?
I see that RandomUtils.Create()
uses Random()
as its default seed, and the default seed of Random()
is "... is derived from the system clock, which has finite resolution" (source). Could Random()
(and thereby RandomUtils.Create()
already not be using Environment.TickCount
as its default seed?
public static TauswortheHybrid Create()
{
// Seed from a system random.
return new TauswortheHybrid(new Random());
}
I think the behavior of default seed of Random is not guaranteed, it depends on net framework or net core, also different version might have different behavior. In reply to: 428586963 [](ancestors = 428586963) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, thanks Frank!
Codecov Report
@@ Coverage Diff @@
## master #5229 +/- ##
=======================================
Coverage 73.47% 73.47%
=======================================
Files 1010 1010
Lines 187988 187974 -14
Branches 20262 20261 -1
=======================================
+ Hits 138118 138120 +2
+ Misses 44385 44373 -12
+ Partials 5485 5481 -4
|
@@ -118,7 +118,7 @@ internal Repository(bool needDir, IExceptionContext ectx) | |||
|
|||
private static string GetShortTempDir() | |||
{ | |||
var rnd = RandomUtils.Create(); | |||
var rnd = RandomUtils.Create(Guid.NewGuid().GetHashCode()); | |||
string path; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there an associated bug? Are we seeing an issue here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❔ Why not just use Path.GetRandomFileName()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The entire implementation of GetShortTempDir()
can be reduced to this:
var path = Path.Combine(Path.GetFullPath(Path.GetTempPath()), "ml_tests", Path.GetRandomFileName());
Directory.CreateDirectory(path);
return path;
The ml_tests subdirectory allows a user to easily identify and delete stray folders created by tests. You definitely don't want to place the folders directly in %TEMP%
which is what happens today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please use Path.GetRandomFileName()
. The original code had RandomUtils.Create
, but we have seen it cause conflicts when it comes to file names. We have started replacing those with Path.GetRandomFileName()
In reply to: 438940202 [](ancestors = 438940202)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I don't have repro, user who reported this issue also don't have repro on his local env, he get error from their product environment but not able to provide a repro
In reply to: 438942892 [](ancestors = 438942892,438936814,438920139)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that Path.GetRandomFileName()
is indeed superior at generating file and/or directory names, so I'm in favor of using this too. It does seem to be limited to generating just 11 characters, though this will probably be more than enough.
It also seems interesting that here Path.GetTempPath()
has been added, but Path.GetRandomFileName()
wasn't. I wonder if there's a reason why
Path.GetRandomFileName()` wasn't used in the first place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that is great idea, one little modification is I want to change "ml_tests" to "ml_dotnet" as this is not for test usage.
In reply to: 438941863 [](ancestors = 438941863)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
address issue: #5210
We are create random temp folder during model load, sometimes there are file name conflict when load models in multi-threading as the random temp folder name is not seeded.