Skip to content

Commit

Permalink
Add AWS_LAMBDA_DOTNET_DISABLE_MEMORY_LIMIT_CHECK env variable to turn…
Browse files Browse the repository at this point in the history
… off the .NET 8 memory adjustment code
  • Loading branch information
normj committed Oct 19, 2023
1 parent 3dea2c0 commit 7f98516
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net5.0;net6.0;net8.0</TargetFrameworks>
<VersionPrefix>1.9.0</VersionPrefix>
<VersionPrefix>1.9.1</VersionPrefix>
<Description>Provides a bootstrap and Lambda Runtime API Client to help you to develop custom .NET Core Lambda Runtimes.</Description>
<AssemblyTitle>Amazon.Lambda.RuntimeSupport</AssemblyTitle>
<AssemblyName>Amazon.Lambda.RuntimeSupport</AssemblyName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ internal class Constants
// variable should never be set when function is deployed to Lambda.
internal const string ENVIRONMENT_VARIABLE_AWS_LAMBDA_DOTNET_DEBUG_RUN_ONCE = "AWS_LAMBDA_DOTNET_DEBUG_RUN_ONCE";

internal const string ENVIRONMENT_VARIABLE_DISABLE_HEAP_MEMORY_LIMIT = "AWS_LAMBDA_DOTNET_DISABLE_MEMORY_LIMIT_CHECK";

internal const string ENVIRONMENT_VARIABLE_AWS_LAMBDA_DOTNET_PREJIT = "AWS_LAMBDA_DOTNET_PREJIT";
internal const string ENVIRONMENT_VARIABLE_AWS_LAMBDA_INITIALIZATION_TYPE = "AWS_LAMBDA_INITIALIZATION_TYPE";
internal const string ENVIRONMENT_VARIABLE_LANG = "LANG";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ private void AdjustMemorySettings()
{
try
{
// Check the environment variable to see if the user has opted out of RuntimeSupport adjusting
// the heap memory limit to match the Lambda configured environment. This can be useful for situations
// where the Lambda environment is being emulated for testing and more then just single Lambda function
// is running in the process. For example running a test runner over a series of Lambda emulated invocations.
var value = Environment.GetEnvironmentVariable(Constants.ENVIRONMENT_VARIABLE_DISABLE_HEAP_MEMORY_LIMIT);
if (string.Equals(value, "true", StringComparison.OrdinalIgnoreCase))
return;


int lambdaMemoryInMb;
if (!int.TryParse(Environment.GetEnvironmentVariable(LambdaEnvironment.EnvVarFunctionMemorySize), out lambdaMemoryInMb))
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ protected virtual async Task TestAllHandlersAsync()
if(_targetFramework == TargetFramework.NET8)
{
await RunMaxHeapMemoryCheck(lambdaClient, "GetTotalAvailableMemoryBytes");
await RunWithoutMaxHeapMemoryCheck(lambdaClient, "GetTotalAvailableMemoryBytes");
await RunMaxHeapMemoryCheckWithCustomMemorySettings(lambdaClient, "GetTotalAvailableMemoryBytes");
}

Expand Down Expand Up @@ -151,6 +152,22 @@ private async Task RunMaxHeapMemoryCheck(AmazonLambdaClient lambdaClient, string
}
}

private async Task RunWithoutMaxHeapMemoryCheck(AmazonLambdaClient lambdaClient, string handler)
{
await UpdateHandlerAsync(lambdaClient, handler, new Dictionary<string, string> { { "AWS_LAMBDA_DOTNET_DISABLE_MEMORY_LIMIT_CHECK", "true" } });
var invokeResponse = await InvokeFunctionAsync(lambdaClient, JsonConvert.SerializeObject(""));
using (var responseStream = invokeResponse.Payload)
using (var sr = new StreamReader(responseStream))
{
string payloadStr = (await sr.ReadToEndAsync()).Replace("\"", "");
// Function payload response will have format {Handler}-{MemorySize}.
// To check memory split on the - and grab the second token representing the memory size.
var tokens = payloadStr.Split('-');
var memory = long.Parse(tokens[1]);
Assert.False(memory <= BaseCustomRuntimeTest.FUNCTION_MEMORY_MB * 1048576);
}
}

private async Task RunMaxHeapMemoryCheckWithCustomMemorySettings(AmazonLambdaClient lambdaClient, string handler)
{
// Set the .NET GC environment variable to say there is 256 MB of memory. The function is deployed with 512 but since the user set
Expand Down

0 comments on commit 7f98516

Please sign in to comment.