aws-lambda-dotnet icon indicating copy to clipboard operation
aws-lambda-dotnet copied to clipboard

"Unable to expand length of this stream beyond its capacity" in serialize large object

Open rslangham opened this issue 8 years ago • 15 comments

I am getting the error below when I try to return a large json from my lambda web api method. I believe this is due to these lines of code https://github.com/aws/aws-lambda-dotnet/blob/master/Libraries/src/Amazon.Lambda.Serialization.Json/JsonSerializer.cs#L76-L78 and I assume the Amazon.Lambda.Serialization.Json wrapper around the Newtonsoft.Json.Serialize method.

Not sure if the limitation is due to the responseStream or the StreamWriter. I need to get around this by either using my own JsonSerializer or ILambdaSerializer, or possibly defaulting back to standard Newtonsoft serialize. This does not occur when run locally, or when running on Lambda.

Unable to expand length of this stream beyond its capacity.: NotSupportedException at System.IO.UnmanagedMemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder) at System.IO.StreamWriter.Write(String value) at Newtonsoft.Json.Utilities.JavaScriptUtils.WriteEscapedJavaScriptString(TextWriter writer, String s Unable to expand length of this stream beyond its capacity.: NotSupportedException at System.IO.UnmanagedMemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder) at System.IO.StreamWriter.Write(String value) at Newtonsoft.Json.Utilities.JavaScriptUtils.WriteEscapedJavaScriptString(TextWriter writer, String s, Char delimiter, Boolean appendDelimiters, Boolean[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IArrayPool`1 bufferPool, Char[]& writeBuffer) at Newtonsoft.Json.JsonTextWriter.WriteEscapedString(String value, Boolean quote) at Newtonsoft.Json.JsonTextWriter.WriteValue(String value) at Newtonsoft.Json.JsonWriter.WriteValue(JsonWriter writer, PrimitiveTypeCode typeCode, Object value)

rslangham avatar Apr 04 '18 13:04 rslangham

After a little more research, do not think this is an issue with replacing the serializer, but believe this is an issue with the Http Response Stream passed to the web api method and that it is set to a capacity size smaller than my response.. So, may be in trouble here unless I have a way to increase its size before it reaches my web api method.

I would ideally like to have my response gzipped in the .net core layer, but seems that there is some incompatability with api gateway and being able to do this. I could only get the gzip to work through the api gateway by letting it handle this instead of being done within the .net web api. I am able to enable the gzip compression in .net web api and it works locally, but fails when behind the api gateway. Believe I recall this is due to api gateway screwing around with the headers, why can't it just be a passthrough.

rslangham avatar Apr 04 '18 13:04 rslangham

How large of a JSON string are you returning? Lambda does have the limit of a 6 Meg return limit. I haven't tried returning back a gzipped response before.

normj avatar Apr 12 '18 05:04 normj

@normj it was near 80mb. I understand there is a 6MB request body size limit, but didn't think this applied to a response body. I didn't see any limits on the response size. We have been able to return sets of data larger than 6MB even up to 50MB or more from Lambda. API Gateway has a reponse body limit of 10MB, but we are typically getting 90% compression on json data using its built-in gzip/deflate compression, so this should have resulted in around 8MB return size and being under its limitation. Though, I can tell from the error that it doesn't even allow me to return the data back to the API gateway for this check to be done, it fails in the C# code when trying to write to the passed Stream. Apparently the Stream passed by the C# Lambda wrapper ( from AWS ) has a max size limit of something below 80MB. I haven't check on its size, but pretty sure this is the case.

What would be nice is if API Gateway didn't screw around with the response headers from the C# Web API. If so, I would then be able to use the response compression available in .NET Core 2.0 and skip the API Gateway response compression completely and also resolve this issue. It would then be returning the 8MB all the way from .net core to the client. No such luck here.

We eventually gave up on API Gateway and Lambda for our C# Web api, just too many limitations and issues for our case. Not saying it doesn't work great for other scenarios, but for our high volume API with varying amounts of return data and execution times, we were starting to change code to workaround the AWS limitations, a bad sign moving forward. Couldn't trust going to production with it.

rslangham avatar Apr 15 '18 21:04 rslangham

The response size limitation for Lambda is 6MB. That's a hard limit. It sounds like you were able to go over 6MB but I'm not sure how you were able to do it.

Regardless, you're hitting a limit at some level because you're receiving that message. You could cache results in S3 and return a presigned URL from the lambda function. Aside from that I'm not sure what else you can do.

vellozzi avatar Apr 16 '18 21:04 vellozzi

Closing due to inactivity.

vellozzi avatar May 02 '18 20:05 vellozzi

I am also getting same message while sending the data in lambda response for an export to csv functionality.

"Unable to expand length of this stream beyond its capacity.: NotSupportedException"

Sharonsudhan avatar Sep 28 '18 11:09 Sharonsudhan

Check the size of the response and log it. If it's > 6MB you'll have to find a different way to handle it. If that's the case, let me know the specifics of what you're doing and I'll help you find a way to accomplish it.

vellozzi avatar Sep 28 '18 16:09 vellozzi

Any chance, as suggested above, that API Gateway could let standard HTTP headers for response compression to work and that would fix this problem for a much larger data payload to occur. Been awhile since I looked at this, eventually gave up and went back to EC2 AMIs. I am guessing that the API gateway stripped out the Accept headers from the client saying that it could support gzip, or potentially screwed around with the headers on the way. back. The only option to use response compression was to rely on the API Gateway instead of being at the API instance (e.g. net core).

If the client request to support gzip headers made it all the way to the .net core instance, then could use standard gzip compression in .net core and then return back a much smaller data payload all the way through the api gateway. This could potentially allow at times that data payloads maybe to be as much as 800mb assuming 80% compression of json. Right now limited at 10mb.

ronl avatar Sep 28 '18 16:09 ronl

I've hit the same issue. A 6MB limit on an HTML page rules out a lot of activity. What are the options available for increasing this?

michaelthwaite avatar Jul 08 '19 21:07 michaelthwaite

We were able to get around this by one, getting away from the API Gateway and using the ELB Application Load Balancer which can now support Lambda target groups. This also solved the problem we had with the API Gateway max 30 second timeout. Using this, our Accept-Encoding gzip headers flowed through to the backend lambda where the .net would then compress the response. In our case, we had control of the client, so it was then required that all clients specify the Accept-Encoding: gzip. This allowed the max response size to be increased up to something much larger, e.g. 40mb or more.

UPDATE/CORRECTION: Not 40MB, but around 10-12MB. I double checked this today and here can see that the max Lambda response size is 1MB, not 6MB.

See https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html

I was basing the 40MB from recollection where we were getting about 8x to 10x compression. In my real world test that I viewed today, I saw an uncompressed response of nearly 12MB with the gzip response being around 780KB. This actually came up today because my integration test actually broke due to the 1MB limit due to the return body size increased. So.... I actually need to decrease the "take" of data for my integration test because of this stupid unrealistic 1MB limit, which is really an AWS issue and not .net issue.

I think the previous reported 6MB is associated with API Gateway. There had issues with the gzip headers not flowing through and letting .net do the response compression inside the lambda. So, this resulted in 6mb max being returned. Using the ALB/Lambda you seem to have a smaller limit of 1MB response, but since the gzip headers flow through and .net can do the response compression within the lambda, you basically can get a larger response returned than the API Gateway.

Though, still to freaking small, hopefully AWS will fix this.

rslangham avatar Jul 08 '19 22:07 rslangham

using the ELB Application Load Balancer which can now support Lambda target groups.

I'm actually already using ALB but how did you enable gzip?

michaelthwaite avatar Jul 08 '19 22:07 michaelthwaite

we are using .net core 2.1, generally easy to find just googling for your particular version of .net. Then, the client needs to specify the Accept-Encoding: gzip.

couple of possible links.. https://docs.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-2.2

https://www.talkingdotnet.com/how-to-enable-gzip-compression-in-asp-net-core/

rslangham avatar Jul 08 '19 23:07 rslangham

That's a great help! It'll probably get me working for at least 90% of the pages that are breaking through the 6MB limit. For the rest, it's time to break out pagination I think. Thanks for your help!

michaelthwaite avatar Jul 08 '19 23:07 michaelthwaite

@michaelthwaite did you implemented Gzip successfully? Thanks

Luke-1988 avatar Sep 12 '19 14:09 Luke-1988

@Luke-1988 No, I couldn't find the right set of configuration settings to make it work. In the end we created a paging solution and saved large content in PDF form on S3 and served a redirect to that after polling the process that created the content. Arguably a better / more proper solution that I'll probably re-use another day for something else.

michaelthwaite avatar Sep 12 '19 14:09 michaelthwaite