Skip to content
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

Thread safe for Path.toRelativeFrom #2268

Merged
merged 2 commits into from
Mar 17, 2019
Merged

Conversation

humhei
Copy link
Contributor

@humhei humhei commented Mar 4, 2019

Description

I encounted this in async workflow
Null Reference expection was throw

before (dictionary is global)

let toRelativeFrom =
    /// A cache of relative path names.
    /// [omit]
    let relativePaths = new Dictionary<_, _>()

    /// Replaces the absolute path to a relative path.
    let inline toRelativePath basePath value =
        let key = (basePath, value)
        match relativePaths.TryGetValue key with
        | true, x -> x
        | _ -> 
            let x = ProduceRelativePath basePath value
            relativePaths.Add(key, x)
            x

    toRelativePath

now (dictionary is in block)

let toRelativeFrom basePath value =
    /// A cache of relative path names.
    /// [omit]
    let relativePaths = new Dictionary<_, _>()

    /// Replaces the absolute path to a relative path.
    let inline toRelativePath basePath value =
        let key = (basePath, value)
        match relativePaths.TryGetValue key with
        | true, x -> x
        | _ -> 
            let x = ProduceRelativePath basePath value
            relativePaths.Add(key, x)
            x

    toRelativePath basePath value

@csmager
Copy link
Contributor

csmager commented Mar 14, 2019

While this fixes the issue of concurrent access, it defeats the purpose of the cache - you might as well replace all the code with ProduceRelativePath basePath value.

If we want to cache the results and make this thread safe you'd need to add locking or use something like ConcurrentDictionary instead.

@matthid
Copy link
Member

matthid commented Mar 16, 2019

Agreed with @csmager. Either we remove the cache completely (and live with potential performance regressions) or we use some kind of locking or ConcurrentDictionary

@matthid matthid added the waiting for author Some information or action was requested and it needs to be addressed. Or a response from author label Mar 16, 2019
@matthid matthid removed the waiting for author Some information or action was requested and it needs to be addressed. Or a response from author label Mar 17, 2019
@matthid
Copy link
Member

matthid commented Mar 17, 2019

Thanks for taking care of this. A test would have been nice but I can see that this is difficult to test.

@matthid matthid merged commit db79766 into fsprojects:release/next Mar 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants