Skip to content

Docker: Improve error messages around port-bound check #12477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025

Conversation

bblommers
Copy link
Contributor

@bblommers bblommers commented Apr 3, 2025

Motivation

Minor improvements around the logging when checking if a port is available.

An ealier version of this PR also changed the image to use bash instead, but some benchmarks showed that using a smaller image does not improve the performance. This method always takes around 35ms, regardless of the exact image used.

For reference, this was the original description:

To check if a certain port is available, we currently start a second LocalStack instance with those ports reserved, and check if it fails. Starting LocalStack is quite resource-intensive - using a smaller image (like bash) is much faster. (This is especially important when calling this method multiple times in parallel.)

@bblommers bblommers added the semver: patch Non-breaking changes which can be included in patch releases label Apr 3, 2025
@bblommers bblommers added this to the Playground milestone Apr 3, 2025
ports=port_mappings,
remove=True,
)
except Exception as e:
if "port is already allocated" not in str(e) and "address already in use" not in str(e):
if "address already in use" not in str(e):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exact error message probably changed at some point, so if a port is already in use, it does not say port is already allocated.

The full message is:

Internal Server Error ("failed to set up container networking: driver failed programming external connectivity on endpoint zealous_mayer (...): failed to bind host port for 0.0.0.0:6443:172.17.0.4:6443/tcp: address already in use")

Simplifying the check should ensure that we only see this message when necessary

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message still exists in the docker source code: https://github.com/moby/moby/blob/11bf52e9e331daa235d23b6cf92298481083128d/libnetwork/portallocator/portallocator.go#L36 Not sure when either is called, but I think it might make sense to keep it here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you're right - it does make more sense to keep it. I've reverted that change 👍

LOG.warning(
"Unexpected error when attempting to determine container port status: %s", e
"Unexpected error when attempting to determine container port status", exc_info=e
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do see the message, it would be nice to see the full stacktrace to debug what's going on

)
return False
# TODO(srw): sometimes the command output from the docker container is "None", particularly when this function is
# invoked multiple times consecutively. Work out why.
if to_str(result[0] or "").strip() != "test123":
LOG.warning(
"Unexpected output when attempting to determine container port status: %s", result[0]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

result is a tuple of (stdout, stderr) - it makes sense to log both if anything goes wrong

Copy link

github-actions bot commented Apr 3, 2025

LocalStack Community integration with Pro

    2 files  ±0      2 suites  ±0   1h 54m 15s ⏱️ -29s
4 323 tests ±0  3 979 ✅ ±0  344 💤 ±0  0 ❌ ±0 
4 325 runs  ±0  3 979 ✅ ±0  346 💤 ±0  0 ❌ ±0 

Results for commit 61f7ec0. ± Comparison against base commit 8999cc4.

♻️ This comment has been updated with latest results.

@bblommers bblommers force-pushed the docker-simplify-port-check branch from 34e7415 to d20190d Compare April 4, 2025 09:00
@bblommers bblommers changed the title Docker: Simplify port-bound check Docker: Improve error messages around port-bound check Apr 4, 2025
@bblommers bblommers marked this pull request as ready for review April 4, 2025 10:49
ports=port_mappings,
remove=True,
)
except Exception as e:
if "port is already allocated" not in str(e) and "address already in use" not in str(e):
if "address already in use" not in str(e):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message still exists in the docker source code: https://github.com/moby/moby/blob/11bf52e9e331daa235d23b6cf92298481083128d/libnetwork/portallocator/portallocator.go#L36 Not sure when either is called, but I think it might make sense to keep it here

LOG.warning(
"Unexpected error when attempting to determine container port status: %s", e
"Unexpected error when attempting to determine container port status", exc_info=e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Unexpected error when attempting to determine container port status", exc_info=e
"Unexpected error when attempting to determine container port status: e", exc_info=LOG.isEnabledFor(logging.DEBUG)

This will only print the stack trace when debug logging is enabled, not for every call, which would reduce the logging in higher log levels. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it was a regular scenario, then yes, it makes sense to only show this on Debug. But this is an unhandled exception, and something happened here that we didn't expect (or know how to handle). So IMO showing a stacktrace at any log level makes it clearer to the user that this is an actual error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid point, fine for me!

@bblommers bblommers force-pushed the docker-simplify-port-check branch from d20190d to 61f7ec0 Compare April 4, 2025 11:31
Copy link
Member

@dfangl dfangl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@bblommers bblommers merged commit ac76ec1 into master Apr 4, 2025
31 checks passed
@bblommers bblommers deleted the docker-simplify-port-check branch April 4, 2025 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver: patch Non-breaking changes which can be included in patch releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants