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

Warn about ArrayPool.Return without clearArray specified when T is not a value type or has references #71698

Open
Tracked by #79053
YohDeadfall opened this issue May 17, 2022 · 6 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer
Milestone

Comments

@YohDeadfall
Copy link
Contributor

Describe the problem you are trying to solve

A new rule checking that clearArray is specified on ArrayPool.Return would solve the problem with unintentional return of an array which contains references and prevent these object being garbage collected. Currently there's a problem with that method the mentioned parameter is optional and can be easily be forgotten especially by new developers, and that clearArray is false by default just for performance optimization purposes not taking into account that there are reference types. The best solution would be to have an enum for that parameter telling the pool that it should decide the right logic depending on T, but the current method is written in stone and cannot be changed. Therefore, the best possible solution is to add a rule.

Describe suggestions on how to achieve the rule

The rule should warn the developer when clearArray is omitted for types which are not value types or has references inside. Explicit setting the parameter to any value resolves the issue.

@mavasani mavasani transferred this issue from dotnet/roslyn-analyzers Jul 6, 2022
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 6, 2022
@mavasani mavasani added the code-analyzer Marks an issue that suggests a Roslyn analyzer label Jul 6, 2022
@ghost
Copy link

ghost commented Jul 6, 2022

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

Describe the problem you are trying to solve

A new rule checking that clearArray is specified on ArrayPool.Return would solve the problem with unintentional return of an array which contains references and prevent these object being garbage collected. Currently there's a problem with that method the mentioned parameter is optional and can be easily be forgotten especially by new developers, and that clearArray is false by default just for performance optimization purposes not taking into account that there are reference types. The best solution would be to have an enum for that parameter telling the pool that it should decide the right logic depending on T, but the current method is written in stone and cannot be changed. Therefore, the best possible solution is to add a rule.

Describe suggestions on how to achieve the rule

The rule should warn the developer when clearArray is omitted for types which are not value types or has references inside. Explicit setting the parameter to any value resolves the issue.

Author: YohDeadfall
Assignees: -
Labels:

area-System.Runtime, untriaged, code-analyzer

Milestone: -

@tannergooding tannergooding removed the untriaged New issue has not been triaged by the area owner label Jul 15, 2022
@tannergooding tannergooding added this to the Future milestone Jul 15, 2022
@stephentoub stephentoub modified the milestones: Future, 8.0.0 Nov 17, 2022
@buyaa-n buyaa-n added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Dec 19, 2022
@stephentoub stephentoub added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Jan 24, 2023
@stephentoub
Copy link
Member

One of the things that makes this rule less of a slam dunk is that code will often handle clearing on its own prior to returning an array to the pool. That's in part because the pool will necessarily clear the whole array, whereas if the code knows how much of the array it used, it can clear only the portions it wrote to and spend less time doing so. To avoid false positives, the analyzer would need to somehow validate that the array being returned wasn't cleared in this manner, and that would be much more challenging to get right.

@stephentoub stephentoub added api-suggestion Early API idea and discussion, it is NOT ready for implementation and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Feb 13, 2023
@colejohnson66
Copy link

@stephentoub If the user is indeed clearing the arrays themselves, couldn't they just disable the analyzer for that ArrayPool.Return call?

@stephentoub
Copy link
Member

If the user is indeed clearing the arrays themselves, couldn't they just disable the analyzer for that ArrayPool.Return call?

Yes, it could be suppressed, e.g.

#pragma warning disable CAWHATEVERIDHERE
...
#pragma warning restore CAWHATEVERIDHERE

at which point the question becomes how much value does the analyzer provide for finding real issues vs how much noise and maintenance burden does it cost. At some point if you have to suppress it too much, the rule switches from being useful to a drag on productivity.

@danmoseley
Copy link
Member

Related #7532

(Although until we have a way to have a toggle able debug mode with zero cost when it's off, any debug checks of course would only help find bugs in our own libraries)

@tannergooding tannergooding modified the milestones: 8.0.0, Future Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer
Projects
None yet
Development

No branches or pull requests

8 participants