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

CA2000 ConcurrentDictionary AddOrUpdate (false positive) #3082

Closed
k1ll3r8e opened this issue Dec 1, 2019 · 1 comment
Closed

CA2000 ConcurrentDictionary AddOrUpdate (false positive) #3082

k1ll3r8e opened this issue Dec 1, 2019 · 1 comment
Assignees
Labels
DataFlow False_Positive A diagnostic is reported for non-problematic case

Comments

@k1ll3r8e
Copy link

k1ll3r8e commented Dec 1, 2019

Analyzer package

Microsoft.CodeAnalysis.FxCopAnalyzers

Package Version

v2.9.8 (Latest)

Diagnostic ID

Example: CA2000

Repro steps

  1. Using .Net Framework 4.6
  2. Adding a disposable type to a ConcurrentDictionary
    private readonly ConcurrentDictionary<uint, Channel> _chs = new ConcurrentDictionary<uint, Channel>();
    
    public IChannel GetChannel(int timeout)
    {
        if (Connected)
        {
            lock (syncThreadID)
            {
                if (_threadID > C_MAX)
                {
                    _threadID = 1;
                }
                uint id = _threadID++;
                while (_chs.ContainsKey(id))
                {
                    id = _threadID++;
                }
                return _chs.AddOrUpdate(id, new Channel(this, id, timeout), (k, v) => v);
            }
        }
        return null;
    }

Expected behavior

    return _chs.AddOrUpdate(id, new Channel(this, id, timeout), (k, v) => v);

The Channel is created, added to the ConcurrentDictionary and will be returned to the caller. So the caller has to call Dispose on it's own. The Dispose implementation in the "Channel" removes it from ConcurrentDictionary. Also, disconnecting/closing/disposing the Socket, where GetChannel(int timeout) is implemented, will raise an event which cause all "Channel"s to Dispose them self. So, in any case , the CA2000 is a false positive report in this situation.

Actual behavior

    return _chs.AddOrUpdate(id, new Channel(this, id, timeout), (k, v) => v);

A CA2000 error is emitted.

@k1ll3r8e k1ll3r8e changed the title CS2000 ConcurrentDictionary AddOrUpdate (false positive) CA2000 ConcurrentDictionary AddOrUpdate (false positive) Dec 1, 2019
@sharwell sharwell added the False_Positive A diagnostic is reported for non-problematic case label Dec 1, 2019
@mavasani mavasani self-assigned this Dec 5, 2019
@mavasani mavasani added this to the vNext milestone Dec 5, 2019
mavasani added a commit to mavasani/roslyn-analyzers that referenced this issue Dec 5, 2019
@jmarolf jmarolf modified the milestones: vNext, .NET 5 Preview 8 Aug 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DataFlow False_Positive A diagnostic is reported for non-problematic case
Projects
None yet
Development

No branches or pull requests

4 participants