Skip to content

Commit 67778ca

Browse files
abelbraaksmabaronfel
authored andcommitted
Add procid and threadid to temp file
1 parent 11c5ff4 commit 67778ca

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

tests/service/ExprTests.fs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,22 @@ module internal Utils =
3535
if Directory.Exists tempPath then ()
3636
else Directory.CreateDirectory tempPath |> ignore
3737

38-
/// Returns the filename part of a temp file name created with Path.GetTempFileName().
38+
/// Returns the filename part of a temp file name created with Path.GetTempFileName()
39+
/// and an added process id and thread id to ensure uniqueness between threads.
3940
let getTempFileName() =
4041
let tempFileName = Path.GetTempFileName()
4142
try
42-
let tempFileName = Path.GetFileName(tempFileName)
43-
tempFileName
43+
let tempFile, tempExt = Path.GetFileNameWithoutExtension tempFileName, Path.GetExtension tempFileName
44+
let procId, threadId = System.Diagnostics.Process.GetCurrentProcess().Id, System.Threading.Thread.CurrentThread.ManagedThreadId
45+
String.concat "" [tempFile; "_"; string procId; "_"; string threadId; tempExt] // ext includes dot
4446
finally
4547
try
46-
// since Path.GetTempFileName() creates a *.tmp file in the %TEMP% folder, we want to clean it up
48+
// Since Path.GetTempFileName() creates a *.tmp file in the %TEMP% folder, we want to clean it up.
49+
// This also prevents a system to run out of available randomized temp files (the pool is only 64k large).
4750
File.Delete tempFileName
4851
with _ -> ()
4952

50-
/// Clean up after a test is run. If you need to inspect the create *.fs files, change this function to do nothing.
53+
/// Clean up after a test is run. If you need to inspect the create *.fs files, change this function to do nothing, or just break here.
5154
let cleanupTempFiles files =
5255
for fileName in files do
5356
try

0 commit comments

Comments
 (0)