Repository navigation
System.ExecutionEngineException when trying to save image as webp #2411
Description
Activity
I have trouble reproducing the error, it works for all n for me. Is the reproduction code really correct? Its doing the same thing in the loop 100 times.
the stacktrace ends up deep in framework code... could be an issue when writing to a particular file location.
@claustsen what type of location are you trying to write to? is it just a local drive or is it a network path? or even some virtual file path (one drive or equivalent online folder sync) location?
In the sample code I am writing to a local ssd on my laptop. I tried to reduce it to bare minimum. I can see this morning that it does not fail when running it.
We have it running on some web servers on
https://content.cylindo.com/api/v2/4960/products/AAC%20121%20SOFT%20DUO/frames/13/MIKASTOOL.webp?version=3&size=1025 there it fails some times and gives correct result at other times.
It works by giving the HttpContext.Response.Body stream to SaveAsWebpAsync. This approach works fine for other image formats you have implemented.I have attached a sample of a successfull image and a failed image. From a quick look and a lack of deeper knowledge of webp format it seems like it is only the alpha part which is wrong, the rest is saved correct. The alpha is in the beginning of the file and the RGB is in the later part. Does it write the RGB first and then spools back the stream to write the alpha?
fail and succes dl.zipFYI when targeting
HttpContext.Response.Bodywe actually end up writing to an in intermediateMemoryStreamdue to the sync nature of our encoders and aspnet core error on sync writes. Before flushing that out to theBodystream.So as a work around you should be able to save to a memory stream first then flush that directly to the file on disk.
I have attached a sample of a successfull image and a failed image. From a quick look and a lack of deeper knowledge of webp format it seems like it is only the alpha part which is wrong, the rest is saved correct. The alpha is in the beginning of the file and the RGB is in the later part. Does it write the RGB first and then spools back the stream to write the alpha?
The alpha chunk is actually written first: Webp extended format
I really wonder how it can write a valid webp, if it fails inWriteAlphaChunk. I have checked the images in fail and succes dl.zip they are correct.@brianpopow I think the
alphaDataspan leaks out of the lifetime scope of its'IMemoryOwner<byte> encodedAlphaData:ImageSharp/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs
Lines 351 to 369 in 0a1f05b
Span<byte> alphaData = Span<byte>.Empty; if (hasAlpha) { // TODO: This can potentially run in an separate task. using IMemoryOwner<byte> encodedAlphaData = AlphaEncoder.EncodeAlpha( image, this.configuration, this.memoryAllocator, this.skipMetadata, this.alphaCompression, out alphaDataSize); alphaData = encodedAlphaData.GetSpan(); if (alphaDataSize < pixelCount) { // Only use compressed data, if the compressed data is actually smaller then the uncompressed data. alphaCompressionSucceeded = true; } } Which leads to memory corruption later when
alphaDatais written to the destination stream. Depending on the app's memory load, it can lead to crashes.Reacted by Brian PopowReacted by James Jackson-SouthGood catch @antonfirsov !
- linked a pull request that will close this issueWebp: Dont use using statement for encodedAlphaData #2412
on Mar 24, 2023 Sorry for closing the issue, this was done automatically.
@claustsen can you try whether the latest nightlies fix the issue for you?
You should be able to see them by addinghttps://f.feedz.io/sixlabors/sixlabors/nuget/index.jsonto your NuGet feed and enabling prerelease.Thanks for the quick fix, event though I did not give much to make it reproduceable.
Will test it out on our servers and locally and hope that they give correct images.
Have been testing it multiple times today. Found no occurrences of the error. So would say that it works now.
Reacted by Anton FirszovWe just released 3.0.1 with the fix, you should be able to consume the official package:
https://www.nuget.org/packages/SixLabors.ImageSharp/3.0.1Closing for now, feel free to reopen if we are wrong and the issue persists.
Prerequisites
DEBUGandRELEASEmodeImageSharp version
3.0.0
Other ImageSharp packages and versions
None
Environment (Operating system, version and so on)
Windows 11
.NET Framework version
dotnet 6.0.15 and dotnet 7.0.4
Description
When running the below code on attached image I get a System.ExecutionEngineException when n reaches 2. Have tried multiple quality settings where it also fails. Does not fail on loss less encoding.
It fails with the following stacktrace
Steps to Reproduce
Using the following code
Images