Skip to content

Add In_WSL function to check whether the host runs on WSL #12799

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 4 commits into from
Jun 26, 2025

Conversation

paolosalvatori
Copy link
Contributor

@paolosalvatori paolosalvatori commented Jun 25, 2025

Motivation

On Windows 11 + WSL, you have a layered setup:

  1. Windows 11 Host: Your primary operating system.
  2. WSL 2: A lightweight virtual machine running a Linux kernel. Docker Desktop runs its daemon inside this WSL 2 VM.
  3. Docker Container: Your Azurite container is running inside the Docker daemon's environment, which itself is in the WSL 2 VM.

The IP address of the Azurite container under NetworkSettings.Networks.bridge.IPAddress, let's say 172.17.02, is the IP address of your Azurite container within the Docker bridge network.

This IP is only directly routable from other containers on the same Docker bridge network or from the Docker host itself (which, in this case, is the WSL 2 VM). Now, usually Docker on Windows is installed on a separate distro. You can check this by running the following command from the Windows command-prompt:

wsl --list --running

This should return something like this:

Windows Subsystem for Linux Distributions:
Ubuntu-24.04 (Default)
docker-desktop

As you can see, there are two distros: Ubuntu-24.04 (Default) running WSL, and docker-desktop. So far so good? Perfect. When running localhost in host mode in the WSL 2 distro's shell and not inside another Docker container, cannot use the IP address of the Azurite container. Full stop.

So I extended the localstack.config class with a function to understand whether the host is running on WSL:

def is_wsl() -> bool:
    return platform.system().lower() == "linux" and os.environ.get("WSL_DISTRO_NAME") is not None

Changes

I extended the localstack.config class with a function to understand whether the host is running on WSL:

def is_wsl() -> bool:
    return platform.system().lower() == "linux" and os.environ.get("WSL_DISTRO_NAME") is not None

Testing

On WSL the is_wsl() returns true.

Introduced dynamic port allocation for Azurite containers using available TCP ports, replacing fixed port mappings. Added detection for WSL environments to adjust host settings for improved compatibility. Enhanced request proxying with detailed logging and error handling for robust communication.
@paolosalvatori paolosalvatori requested a review from thrau as a code owner June 25, 2025 16:07
@localstack-bot
Copy link
Collaborator

localstack-bot commented Jun 25, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

Copy link
Collaborator

@localstack-bot localstack-bot left a comment

Choose a reason for hiding this comment

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

Welcome to LocalStack! Thanks for raising your first Pull Request and landing in your contributions. Our team will reach out with any reviews or feedbacks that we have shortly. We recommend joining our Slack Community and share your PR on the #community channel to share your contributions with us. Please make sure you are following our contributing guidelines and our Code of Conduct.

@paolosalvatori
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

localstack-bot added a commit that referenced this pull request Jun 25, 2025
Copy link

github-actions bot commented Jun 25, 2025

Test Results - Preflight, Unit

21 727 tests  ±0   20 070 ✅ ±0   6m 14s ⏱️ +3s
     1 suites ±0    1 657 💤 ±0 
     1 files   ±0        0 ❌ ±0 

Results for commit e77b206. ± Comparison against base commit 3d56935.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Jun 25, 2025

S3 Image Test Results (AMD64 / ARM64)

    2 files    2 suites   8m 55s ⏱️
  512 tests 462 ✅  50 💤 0 ❌
1 024 runs  924 ✅ 100 💤 0 ❌

Results for commit e77b206.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Jun 25, 2025

LocalStack Community integration with Pro

    2 files  ±0      2 suites  ±0   1h 45m 50s ⏱️ + 1m 0s
4 896 tests +2  4 122 ✅ +2  774 💤 ±0  0 ❌ ±0 
4 898 runs  +2  4 122 ✅ +2  776 💤 ±0  0 ❌ ±0 

Results for commit e77b206. ± Comparison against base commit 3d56935.

♻️ This comment has been updated with latest results.

Copy link
Member

@thrau thrau left a comment

Choose a reason for hiding this comment

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

hey @paolosalvatori! thanks for being a WSL champion :-) we have few users on windows, but this may change with the growing azure team.

just two minor things:

  • do we need the solutions file checked into the repo? we normally don't check in environment-specific files
  • a more descriptive PR title for the commit history would be great ;-)

Copy link
Member

@alexrashed alexrashed left a comment

Choose a reason for hiding this comment

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

From your PR description, I can see that you are fighting with a pretty specific issue (running LocalStack in host mode - for development purposes). I am wondering if there is a more generic way to solve the Docker networking issue you might be facing.
Maybe @simonrw or @dfangl could have an idea here?

However, the implementation is looking good, and with Mac, Linux, and WSL, most host systems are covered, so it's also fair to keep the function as is.
The sln file should be removed though before merging this PR.

@paolosalvatori paolosalvatori changed the title Paolo in wsl Add In_WSL function to check whether the host runs on WSL Jun 26, 2025
@paolosalvatori
Copy link
Contributor Author

paolosalvatori commented Jun 26, 2025

hey @paolosalvatori! thanks for being a WSL champion :-) we have few users on windows, but this may change with the growing azure team.

just two minor things:

  • do we need the solutions file checked into the repo? we normally don't check in environment-specific files
  • a more descriptive PR title for the commit history would be great ;-)

I think I filed the PR from PyCharm to make an experiment, and I forgot to change the title to something meaningful. I think PyCharm turned the name of the branch paolo_in_wsl into the PR name 😄 I usually use very descriptive titles in my PRs.

Regarding the .sln file, I had the same issue on the other PR I opened on localstack-ext: I originally followed the instructions from Getting Started – Setting up a Development Environment for LocalStack Core on Notion and initially used Visual studio Code, then I switched to PyCharm. The .sln file was added by Visual studio Code. I removed the .sln file from the PR and added *.sln to .gitignore.

Copy link
Member

@alexrashed alexrashed left a comment

Choose a reason for hiding this comment

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

Thanks for jumping on the comments and answering the questions! The code is simple and lookin' good :)

@paolosalvatori
Copy link
Contributor Author

paolosalvatori commented Jun 26, 2025

Thanks @alexrashed 🙂 @thrau when you have time, please give your approval, so I can unblock the other PR on localstack-ext repo as linting test fails saying that is_in_wsl attribute is not in localstack.config module 🙂 Thanks! 🙏

@thomaschaaf
Copy link
Contributor

@paolosalvatori I believe you mentioned the wrong person on your request. 🙃

@paolosalvatori
Copy link
Contributor Author

paolosalvatori commented Jun 26, 2025

@paolosalvatori I believe you mentioned the wrong person on your request. 🙃

Yep, wrong Thomas 🙂 I trusted GitHub when it proposed your alias instead of Thomas Rausch one 🙂

@alexrashed
Copy link
Member

@paolosalvatori You already have the approval from my side (@thrau needs to approve still though).
But you also do have linting errors here in the project.
Please make sure to fix those and configure the pre-commit hook (to check this before pushing a commit) to avoid issues like this in the future.

@paolosalvatori paolosalvatori added semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases labels Jun 26, 2025
@paolosalvatori
Copy link
Contributor Author

@alexrashed thanks for the heads up. I ran pre-commit install on both localstack and localstack-ext projects and I fixed the linting issue affecting this PR. I know that you approved the PR 🙏 but Thomas approval is pending. He requested a change, so the PR cannot be merged until he approves it.

Copy link

Test Results (amd64) - Acceptance

7 tests  ±0   5 ✅ ±0   3m 8s ⏱️ ±0s
1 suites ±0   2 💤 ±0 
1 files   ±0   0 ❌ ±0 

Results for commit e77b206. ± Comparison against base commit 3d56935.

Copy link

Test Results (amd64) - Integration, Bootstrap

    5 files      5 suites   2h 22m 52s ⏱️
5 253 tests 4 327 ✅ 926 💤 0 ❌
5 259 runs  4 327 ✅ 932 💤 0 ❌

Results for commit e77b206.

@paolosalvatori paolosalvatori added this to the 4.6 milestone Jun 26, 2025
Copy link
Member

@thrau thrau left a comment

Choose a reason for hiding this comment

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

LGTM!

@paolosalvatori paolosalvatori merged commit 019d5ec into master Jun 26, 2025
49 of 50 checks passed
@paolosalvatori paolosalvatori deleted the paolo_in_wsl branch June 26, 2025 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants