question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug .azure folder mounted from WSL2 creates lock errors with many requests

See original GitHub issue

Describe the bug

When running terraform inside WSL2 together with a .azure folder mounted inside windows seems like we get file lock issues.

│ Error: getting authenticated object ID: Error parsing json result from the Azure CLI: Error waiting for the Azure CLI: exit status 1: ERROR: The command failed with an unexpected error. Here is the traceback:
│ ERROR: [Errno 2] No such file or directory
│ Traceback (most recent call last):
│ File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/command_operation.py", line 352, in handler
│ client = self.client_factory(self.cli_ctx) if self.client_factory else None
│ TypeError: get_graph_client_signed_in_users() missing 1 required positional argument: '_'
│
│ During handling of the above exception, another exception occurred:
│
│ Traceback (most recent call last):
│ File "/opt/az/lib/python3.6/site-packages/knack/cli.py", line 231, in invoke
│ cmd_result = self.invocation.execute(args)
│ File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 657, in execute
│ raise ex
│ File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 720, in _run_jobs_serially
│ results.append(self._run_job(expanded_arg, cmd_copy))
│ File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 712, in _run_job
│ return cmd_copy.exception_handler(ex)
│ File "/opt/az/lib/python3.6/site-packages/azure/cli/command_modules/role/commands.py", line 69, in graph_err_handler
│ raise ex
│ File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 691, in _run_job
│ result = cmd_copy(params)
│ File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 328, in __call__
│ return self.handler(*args, **kwargs)
│ File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/command_operation.py", line 354, in handler
│ client = self.client_factory(self.cli_ctx, command_args) if self.client_factory else None
│ File "/opt/az/lib/python3.6/site-packages/azure/cli/command_modules/role/commands.py", line 89, in get_graph_client_signed_in_users
│ return _graph_client_factory(cli_ctx).signed_in_user
│ File "/opt/az/lib/python3.6/site-packages/azure/cli/command_modules/role/_client_factory.py", line 25, in _graph_client_factory
│ resource=cli_ctx.cloud.endpoints.active_directory_graph_resource_id)
│ File "/opt/az/lib/python3.6/site-packages/azure/cli/core/_profile.py", line 335, in get_login_credentials
│ credential = self._create_credential(account, client_id=client_id)
│ File "/opt/az/lib/python3.6/site-packages/azure/cli/core/_profile.py", line 588, in _create_credential
│ return identity.get_user_credential(username_or_sp_id)
│ File "/opt/az/lib/python3.6/site-packages/azure/cli/core/auth/identity.py", line 182, in get_user_credential
│ return UserCredential(self.client_id, username, **self._msal_app_kwargs)
│ File "/opt/az/lib/python3.6/site-packages/azure/cli/core/auth/msal_authentication.py", line 41, in __init__
│ accounts = self.get_accounts(username)
│ File "/opt/az/lib/python3.6/site-packages/msal/application.py", line 872, in get_accounts
│ accounts = self._find_msal_accounts(environment=self.authority.instance)
│ File "/opt/az/lib/python3.6/site-packages/msal/application.py", line 912, in _find_msal_accounts
│ query={​​"environment": environment}​​)
│ File "/opt/az/lib/python3.6/site-packages/msal_extensions/token_cache.py", line 53, in find
│ with CrossPlatLock(self._lock_location):
│ File "/opt/az/lib/python3.6/site-packages/msal_extensions/cache_lock.py", line 29, in __enter__
│ file_handle = self._lock.__enter__()
│ File "/opt/az/lib/python3.6/site-packages/portalocker/utils.py", line 199, in __enter__
│ return self.acquire()
│ File "/opt/az/lib/python3.6/site-packages/portalocker/utils.py", line 161, in acquire
│ fh = self._prepare_fh(fh)
│ File "/opt/az/lib/python3.6/site-packages/portalocker/utils.py", line 194, in _prepare_fh
│ fh.truncate(0)
│ FileNotFoundError: [Errno 2] No such file or directory
│ To open an issue, please run: 'az feedback'
│
│ with provider["registry.terraform.io/hashicorp/azuread"],
│ on main.tf line 19, in provider "azuread":
│ 19: provider "azuread" {​​}​​

The terraform module that we are using uses both azurerm and azuread. I think the switch between the modules is to quick and WSL2 don’t have enough time to release the azure cli json file that it uses.

To Reproduce Run lots of azure cli requests inside of WSL2 with a .azure folder mounted to windows. I don’t have a reliable way of creating this issue.

Expected behavior To not get the error.

Have some try logic that makes sure that this doesn’t happen. Probably a simple retry to get the lock file will solve the problem to give WSL2/windows time to release the file lock. But I’m just guessing now.

Environment summary

Linux-5.10.60.1-microsoft-standard-WSL2-x86_64-with-debian-buster-sid, Ubuntu 18.04.5 LTS Python 3.6.10 Installer: DEB

azure-cli 2.32.0

Additional context

I have tried to explain this issue in our external docs: https://github.com/XenitAB/xenitab.github.io/pull/80

*Workaround

Found a workaround which is to not to store .azure inside windows. Assuming that pwd isn’t a windows folder.

export AZURE_CONFIG_DIR=$(pwd)/.azure
# Store Azure CLI configuration here
[ -d $(pwd)/.azure ] || mkdir $(pwd)/.azure
# login to azure agaiin
az login

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jiaslicommented, Jan 17, 2022

This is weird, as the file content is indeed wrong. Here is my L53:

$ cat /opt/az/lib/python3.6/site-packages/msal_extensions/token_cache.py | sed -n 53p
    def _reload_if_necessary(self):

Could you also check

$ ls --directory /opt/az/lib/python3.6/site-packages/azure_cli*
/opt/az/lib/python3.6/site-packages/azure_cli-2.32.0-py3.6.egg-info
/opt/az/lib/python3.6/site-packages/azure_cli_core-2.32.0-py3.6.egg-info
/opt/az/lib/python3.6/site-packages/azure_cli_telemetry-1.0.6-py3.6.egg-info

If everything is correct, could you follow https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-linux?pivots=apt and reinstall?

We do see corrupted/outdated files after upgrading with MSI on Windows (https://github.com/Azure/azure-cli/issues/20990), but upgrading with apt-get resulting in outdated file is something we saw for the first time. 🤔

0reactions
jiaslicommented, Jan 19, 2022

I tried to decompile the pyc file on my WSL and the content is also correct:

$ pip3 install uncompyle6

$ uncompyle6 /opt/az/lib/python3.6/site-packages/msal_extensions/__pycache__/token_cache.cpython-36.pyc
...
    def find(self, credential_type, **kwargs):
        retry = 3
        for attempt in range(1, retry + 1):
            try:
                self._reload_if_necessary()
            except Exception:
                if attempt < retry:
                    logger.debug('Unable to load token cache file in No. %d attempt', attempt)
                    time.sleep(0.5)
                else:
                    raise
            else:
                return (super(PersistedTokenCache, self).find)(credential_type, **kwargs)

        return []
Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting Windows Subsystem for Linux | Microsoft Learn
Older CPUs (such as the Intel Core 2 Duo) will not be able to run WSL2, even if the Virtual Machine Platform is...
Read more >
Windows10 WSL2 Ubuntu / Debian # no network [closed]
I found creating the /etc/wsl.conf didn't work, but left it anyway: ... I had the same error when I applied Windows NTFS drive...
Read more >
How to Fix "Read-only file system" error when I run something ...
I suggest to boot a live USB/DVD, open GParted, and use the check/repair feature. This is the command to enable writing: sudo mount...
Read more >
Using Dev Containers in WSL 2 - Visual Studio Code
Now, both Windows WSL 2 and Docker support are in stable GA releases! ... as accessing files, requesting memory, and creating processes.
Read more >
Installing Docker Desktop for Windows and WSL 2
Error installing the WSL 2 update if you haven't restarted ... With Docker Desktop installed, you now have the docker command available in ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found