-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Change exception for overflow in ArrayBufferWriter #32587
Change exception for overflow in ArrayBufferWriter #32587
Conversation
This (pre-existing) if is not clear to me. If the buffer.Length is 0, like when we just created the instance. It doesn't respect the sizeHint |
src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.T.cs
Outdated
Show resolved
Hide resolved
The test is failing in "netcoreapp5.0-OSX-Debug-x64-Mono_release-OSX.1013.Amd64.Open" environment. |
I am not sure why that is failing, but it looks like it is mono specific:
@jkotas - do you know why we aren't throwing OOM on mono OSX here? Does it support more than int.MaxValue elements somehow? var buffer = new byte[10];
Array.Resize<byte>(ref buffer, int.MaxValue); cc @alexischr |
src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.T.cs
Outdated
Show resolved
Hide resolved
Look for |
The In this case, if a user keeps asking for a buffer passing sizeHint = 0, it doesn't make sense to start the "doubling" with 1 (then 2, 4, 8, ...). So, we cap the lower bound by 256, which seems to be a reasonable starting size for most operations. |
@ahsonkhan If we keep it the way it is now, in OSX Mono environment that method will return a Buffer of int.MaxValue size, instead of the size required, breaking the contract. |
Yep, looks like we need to disable it for platforms that support arrays larger than int.Max: |
Sure, that makes sense. I am not sure what we generally do in the rest of the framework for this type of discrepancy, but given I was leaning towards having a custom exception message anyway, this works (#1308 (comment)). Consider adding and calling a throwhelper method similar to: I am going to defer to what others have to say about that as the final decision though. cc @stephentoub |
@stephentoub, should we explicitly throw OOM? And change my previous PR (#1308) to do the same? Thanks |
We throw a OutOfMemoryException instead of "Arithmetic operation resulted in an overflow"
…emory is never allocated.
d515c20
to
5c2b37f
Compare
For unblocking this change/PR, let's do it. I doubt we'll need to, but we can re-visit that decision later. |
src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.T.cs
Show resolved
Hide resolved
src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.T.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.T.cs
Outdated
Show resolved
Hide resolved
This test (https://github.com/dotnet/runtime/pull/32587/checks?check_run_id=530741887) is failing:
after I added this line in the unit test:
I'll restrict it to 64bits environment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some test suggestions.
src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.T.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.T.cs
Outdated
Show resolved
Hide resolved
Since we are trying to avoid the OOM, don't run the GetMemory_ExceedMaximumBufferSize on Linux
src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.T.cs
Outdated
Show resolved
Hide resolved
That alone won't help. I am getting OOM even on my local machine. The test is taking up 12+ GB of memory to run (even if we don't call The problem is the Rather than adding the test to the generic base class, make it specific to byte only and just add it to the runtime/src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.Byte.cs Lines 11 to 12 in 4040e60
// NOTE: GetMemory_ExceedMaximumBufferSize test is constrained to run on Windows and MacOSX because it causes
// problems on Linux due to the way deferred memory allocation works. On Linux, the allocation can
// succeed even if there is not enough memory but then the test may get killed by the OOM killer at the
// time the memory is accessed which triggers the full memory allocation.
[PlatformSpecific(TestPlatforms.Windows | TestPlatforms.OSX)]
[ConditionalFact(nameof(IsX64))]
[OuterLoop]
public void GetMemory_ExceedMaximumBufferSize()
{
var output = new ArrayBufferWriter<byte>(int.MaxValue / 2 + 1);
output.Advance(int.MaxValue / 2 + 1);
Memory<byte> memory = output.GetMemory(1); // Validate we can't double the buffer size, but can grow by sizeHint
Assert.Equal(1, memory.Length);
Assert.Throws<OutOfMemoryException>(() => output.GetMemory(int.MaxValue));
} |
CI failure is unrelated: #34087 |
Continuation from #1308