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

ArrayPool<T>.Shared.Return does not check if array already returned #75518

Closed
kefyru opened this issue Sep 13, 2022 · 2 comments
Closed

ArrayPool<T>.Shared.Return does not check if array already returned #75518

kefyru opened this issue Sep 13, 2022 · 2 comments

Comments

@kefyru
Copy link

kefyru commented Sep 13, 2022

Description

I think it is wrong behaviour.
ArrayPool should throw if we return array which already in pool, otherwise it leads to unpredictable behavior and very difficult error detection

var arr = ArrayPool<byte>.Shared.Rent(8);
ArrayPool<byte>.Shared.Return(arr);
ArrayPool<byte>.Shared.Return(arr);
var arr1 = ArrayPool<byte>.Shared.Rent(8);
var arr2 = ArrayPool<byte>.Shared.Rent(8);
if (ReferenceEquals(arr1, arr2)) throw new Exception(); // throws !!!

Reproduction Steps

see description

Expected behavior

var arr = ArrayPool<byte>.Shared.Rent(8);
ArrayPool<byte>.Shared.Return(arr); // ok
ArrayPool<byte>.Shared.Return(arr); // should throw InvalidArgumentException("array already in pool")

Actual behavior

var arr = ArrayPool<byte>.Shared.Rent(8);
ArrayPool<byte>.Shared.Return(arr); // ok
ArrayPool<byte>.Shared.Return(arr); // ok!!

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Sep 13, 2022
@ghost
Copy link

ghost commented Sep 13, 2022

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

Issue Details

Description

I think it is wrong behaviour.
ArrayPool should throw if we return array which already in pool, otherwise it leads to unpredictable behavior and very difficult error detection

var arr = ArrayPool<byte>.Shared.Rent(8);
ArrayPool<byte>.Shared.Return(arr);
ArrayPool<byte>.Shared.Return(arr);
var arr1 = ArrayPool<byte>.Shared.Rent(8);
var arr2 = ArrayPool<byte>.Shared.Rent(8);
if (ReferenceEquals(arr1, arr2)) throw new Exception(); // throws !!!

Reproduction Steps

see description

Expected behavior

var arr = ArrayPool<byte>.Shared.Rent(8);
ArrayPool<byte>.Shared.Return(arr); // ok
ArrayPool<byte>.Shared.Return(arr); // should throw InvalidArgumentException("array already in pool")

Actual behavior

var arr = ArrayPool<byte>.Shared.Rent(8);
ArrayPool<byte>.Shared.Return(arr); // ok
ArrayPool<byte>.Shared.Return(arr); // ok!!

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: kefyru
Assignees: -
Labels:

area-System.Buffers

Milestone: -

@stephentoub
Copy link
Member

Duplicate of #7532

This would be untenably slow by default.

@stephentoub stephentoub closed this as not planned Won't fix, can't repro, duplicate, stale Sep 13, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Sep 13, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Oct 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants