Bug 60791 - Path.GetTempFileName is not thread-safe
Summary: Path.GetTempFileName is not thread-safe
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: master
Hardware: PC Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Rodrigo Kumpera
URL:
Depends on:
Blocks:
 
Reported: 2017-11-21 21:00 UTC by Jérémie Laval
Modified: 2018-03-14 00:40 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_REPRODUCIBLE

Description Jérémie Laval 2017-11-21 21:00:55 UTC
The naive implementation of Path.GetTempFileName is not thread-safe and can return the same filename multiple time.

Repro:

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.Concurrent;

class T
{
    public static async Task Main ()
    {
        var createdPaths = new HashSet<string> ();
        for (int i = 0; i < 10000; i++) {
            var queue = new ConcurrentQueue<string> ();
            await Task.WhenAll (
                Task.Run (() => queue.Enqueue (Path.GetTempFileName ())),
                Task.Run (() => queue.Enqueue (Path.GetTempFileName ())),
                Task.Run (() => queue.Enqueue (Path.GetTempFileName ())),
                Task.Run (() => queue.Enqueue (Path.GetTempFileName ())),
                Task.Run (() => queue.Enqueue (Path.GetTempFileName ())),
                Task.Run (() => queue.Enqueue (Path.GetTempFileName ())),
                Task.Run (() => queue.Enqueue (Path.GetTempFileName ()))
            );
            while (queue.TryDequeue (out var path)) {
                if (!createdPaths.Add (path))
                    Console.WriteLine ($"/!\\ Path {path} has been created twice");
                File.Delete (path);
            }
        }
    }
}

Output:

% mono repro-gettempfilename.exe
/!\ Path /var/folders/sj/m6n49_qx5w3_9n7s0pdmc3gc0000gn/T/tmp5408018.tmp has been created twice
/!\ Path /var/folders/sj/m6n49_qx5w3_9n7s0pdmc3gc0000gn/T/tmp52146c7b.tmp has been created twice

To compare, .NET core uses the proper mktemp family of syscalls which are correct.
Comment 1 Rodrigo Kumpera 2018-03-14 00:22:49 UTC
working on it.
Comment 2 Rodrigo Kumpera 2018-03-14 00:40:53 UTC
I tried your test case multiple times and it did not find a single case of duplicated file paths.

I checked how it's implemented in mono and could not find what you refer as a naive implementation. It's doing exactly the same thing as mktemp does.

The API is unfortunately racy, as it should return a FileStream instead of a string, but that's a separate story.