fix: Container clean up and load distribution #8389
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Pull Request Summary
Which issue(s) does this change fix?
This change fixes two critical issues in AWS SAM CLI's
--warm-containers LAZYmode:Issue 1: Incomplete Container Cleanup
Dict[str, Container]which overwrites entries, so only the last container per function was trackedIssue 2: Inconsistent Container Reuse
Why is this change necessary?
Resource Management: Orphaned containers consume system resources (memory, CPU, disk) and can accumulate over development sessions.
Performance & Accuracy: Users may notice the difference in invoke speed between the very first invokes (parallel with container scale up) and the subsequent invokes (use only one container and request get queued despite other running untracked containers available)
Developer Experience: The fix improve simulation of Lambda horizontal scaling with concurrent requests. Local testing already reflect this behavior but with inaccurate load distribution due to bug.
How does it address the issue?
Container Tracking (Issue 1)
Dict[str, Container]toDict[str, List[Container]]usingdefaultdict(list)clean_running_containers_and_related_resources()to handle multiple containers per function with error handlingContainer Selection (Issue 2)
has_capacity(),_mark_busy(), and_mark_idle()methods with thread safety._find_available_container()to check all containers for capacity using first-available strategy_acquire_container()to handle container reuse and creation with thread-safe lockingThread Safety and Error Handling
_container_lockfor runtime operations,_busy_lockfor container state.What side effects does this change have?
Positive Side Effects
EAGERandLAZYmode.Potential Considerations
Backward Compatibility
Mandatory Checklist
PRs will only be reviewed after checklist is complete
Containercapacity signalingWarmLambdaRuntimecontainer managementstart-api(LAZY and EAGER modes)start-lambda(LAZY and EAGER modes)make prpassesmake update-reproducible-reqsif dependencies were changedBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.