Fix AttributeError when port is already in use#8675
Open
dcabib wants to merge 1 commit intoaws:developfrom
Open
Fix AttributeError when port is already in use#8675dcabib wants to merge 1 commit intoaws:developfrom
dcabib wants to merge 1 commit intoaws:developfrom
Conversation
This commit fixes two related issues when Docker port binding fails: 1. AttributeError on ex.explanation.decode() - In docker-py >= 7.0, the explanation property is already a str, not bytes. Calling .decode() on a string raises AttributeError, completely masking the actual "port already in use" error. 2. Case sensitivity in error detection - The code checked for "Ports are not available" (capital P) but Docker may return "ports are not available" (lowercase p), causing the condition to fail and miss the specific error handling. Changes: - Added safe_decode_docker_message() function in utils.py that handles both bytes (docker-py < 7.0) and str (docker-py >= 7.0) - Updated container.py to use case-insensitive check (.lower()) - Added comprehensive unit tests covering both scenarios This ensures users see the helpful port error message instead of a cryptic AttributeError, regardless of their docker-py version. Fixes aws#7244 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Issue
Fixes #7244
Problem
When SAM CLI attempts to bind to a port that's already in use, users see a cryptic
AttributeError: 'str' object has no attribute 'decode'instead of a helpful error message about which port is occupied.This occurs because:
APIError.explanationis astr(notbytes).decode()on it, causing AttributeErrorSolution
safe_decode_docker_message()function that handles bothbytes(docker-py < 7.0) andstr(docker-py >= 7.0).lower())Testing
samdev local invokeon occupied portResult
Users now see the actual port error message:
Instead of:
Checklist
make test)make lint)make black-check)