Repository: conda/conda

Issue:
conda-env may update conda while it's still in use
 (cc @levy5674, @patricksnape)
Issue reported in https://github.com/conda/conda/issues/6606#issuecomment-355149329:
`conda-env` can update `conda` (either explicitly or by `auto_update_conda = True`) while it's still using it and thus makes the current process get into an inconsistent state (using parts of the old as well as updated `conda`).
`conda-env` loops over the different dependency types in https://github.com/conda/conda/blob/4.4.6/conda_env/cli/main_update.py#L104. If it updates `conda`, other subsequent updates, e.g., using `conda_env.installers.pip`, may already introduce parts of the updated `conda` into the current process, see:
```yaml
# environment.yaml
dependencies:
  - conda
  - pip:
    - bumpversion
```
```bash
(ins)$ cat environment.yml 
dependencies:
  - conda
  - pip:
    - bumpversion
(ins)$ bash Miniconda3-4.3.31-Linux-x86_64.sh -bp ./mc
PREFIX=/home/maba/code/conda/mc
installing: python-3.6.3-h6c0c0dc_5 ...
Python 3.6.3 :: Anaconda, Inc.
installing: ca-certificates-2017.08.26-h1d4fec5_0 ...
installing: conda-env-2.6.0-h36134e3_1 ...
installing: libgcc-ng-7.2.0-h7cc24e2_2 ...
installing: libstdcxx-ng-7.2.0-h7a57d05_2 ...
installing: libffi-3.2.1-hd88cf55_4 ...
installing: ncurses-6.0-h9df7e31_2 ...
installing: openssl-1.0.2n-hb7f436b_0 ...
installing: tk-8.6.7-hc745277_3 ...
installing: xz-5.2.3-h55aa19d_2 ...
installing: yaml-0.1.7-had09818_2 ...
installing: zlib-1.2.11-ha838bed_2 ...
installing: libedit-3.1-heed3624_0 ...
installing: readline-7.0-ha6073c6_4 ...
installing: sqlite-3.20.1-hb898158_2 ...
installing: asn1crypto-0.23.0-py36h4639342_0 ...
installing: certifi-2017.11.5-py36hf29ccca_0 ...
installing: chardet-3.0.4-py36h0f667ec_1 ...
installing: idna-2.6-py36h82fb2a8_1 ...
installing: pycosat-0.6.3-py36h0a5515d_0 ...
installing: pycparser-2.18-py36hf9f622e_1 ...
installing: pysocks-1.6.7-py36hd97a5b1_1 ...
installing: ruamel_yaml-0.11.14-py36ha2fb22d_2 ...
installing: six-1.11.0-py36h372c433_1 ...
installing: cffi-1.11.2-py36h2825082_0 ...
installing: setuptools-36.5.0-py36he42e2e1_0 ...
installing: cryptography-2.1.4-py36hd09be54_0 ...
installing: wheel-0.30.0-py36hfd4bba0_1 ...
installing: pip-9.0.1-py36h6c6f9ce_4 ...
installing: pyopenssl-17.5.0-py36h20ba746_0 ...
installing: urllib3-1.22-py36hbe7ace6_0 ...
installing: requests-2.18.4-py36he2e5f8d_1 ...
installing: conda-4.3.31-py36_0 ...
installation finished.
(ins)$ source mc/bin/activate 
(ins)(root) $ conda env update -n root
Fetching package metadata ...........
Solving package specifications: .
conda-4.4.6-py 100% |################################################################################################################################################################################| Time: 0:00:01 669.69 kB/s
An unexpected error has occurred.
Please consider posting the following information to the
conda GitHub issue tracker at:

    https://github.com/conda/conda/issues



Traceback (most recent call last):
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda/exceptions.py", line 640, in conda_exception_handler
    original data Length: %(original_data_length)d
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda_env/cli/main_update.py", line 106, in execute
    installer = get_installer(installer_type)
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda_env/installers/pip.py", line 40, in _pip_install_via_requirements
    args, pip_version = pip_args(prefix)
ValueError: too many values to unpack (expected 2)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/maba/code/conda/mc/bin/conda-env", line 11, in <module>
    sys.exit(main())
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda_env/cli/main.py", line 76, in main
    
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda/exceptions.py", line 644, in conda_exception_handler
    'path': path,
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda/exceptions.py", line 634, in handle_exception
    def __init__(self, path, placeholder, new_prefix, original_data_length, new_data_length):
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda/exceptions.py", line 595, in print_unexpected_error_message
    
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda/cli/main_info.py", line 19, in <module>
    from .common import print_envs_list, stdout_json
ImportError: cannot import name 'print_envs_list'
```
This of course affects `conda 4.4` as well:
<details><summary>
logs for conda 4.4.x (using 4.4.0rc2 to let it trip at the same changed import as 4.3.31 did)
</summary><p>

```bash
(ins)$ cat environment.yml 
dependencies:
  - conda
  - pip:
    - bumpversion
(ins)$ bash Miniconda3-4.3.31-Linux-x86_64.sh -bp ./mc
PREFIX=/home/maba/code/conda/mc
installing: python-3.6.3-h6c0c0dc_5 ...
Python 3.6.3 :: Anaconda, Inc.
installing: ca-certificates-2017.08.26-h1d4fec5_0 ...
installing: conda-env-2.6.0-h36134e3_1 ...
installing: libgcc-ng-7.2.0-h7cc24e2_2 ...
installing: libstdcxx-ng-7.2.0-h7a57d05_2 ...
installing: libffi-3.2.1-hd88cf55_4 ...
installing: ncurses-6.0-h9df7e31_2 ...
installing: openssl-1.0.2n-hb7f436b_0 ...
installing: tk-8.6.7-hc745277_3 ...
installing: xz-5.2.3-h55aa19d_2 ...
installing: yaml-0.1.7-had09818_2 ...
installing: zlib-1.2.11-ha838bed_2 ...
installing: libedit-3.1-heed3624_0 ...
installing: readline-7.0-ha6073c6_4 ...
installing: sqlite-3.20.1-hb898158_2 ...
installing: asn1crypto-0.23.0-py36h4639342_0 ...
installing: certifi-2017.11.5-py36hf29ccca_0 ...
installing: chardet-3.0.4-py36h0f667ec_1 ...
installing: idna-2.6-py36h82fb2a8_1 ...
installing: pycosat-0.6.3-py36h0a5515d_0 ...
installing: pycparser-2.18-py36hf9f622e_1 ...
installing: pysocks-1.6.7-py36hd97a5b1_1 ...
installing: ruamel_yaml-0.11.14-py36ha2fb22d_2 ...
installing: six-1.11.0-py36h372c433_1 ...
installing: cffi-1.11.2-py36h2825082_0 ...
installing: setuptools-36.5.0-py36he42e2e1_0 ...
installing: cryptography-2.1.4-py36hd09be54_0 ...
installing: wheel-0.30.0-py36hfd4bba0_1 ...
installing: pip-9.0.1-py36h6c6f9ce_4 ...
installing: pyopenssl-17.5.0-py36h20ba746_0 ...
installing: urllib3-1.22-py36hbe7ace6_0 ...
installing: requests-2.18.4-py36he2e5f8d_1 ...
installing: conda-4.3.31-py36_0 ...
installation finished.
(ins)$ source mc/bin/activate 
(ins)(root) $ conda install -yc conda-canary conda=4.4.0rc2
Fetching package metadata .............
Solving package specifications: .

Package plan for installation in environment /home/maba/code/conda/mc:

The following packages will be UPDATED:

    conda: 4.3.31-py36_0 --> 4.4.0rc2-py36h7fbbb7a_0 conda-canary

conda-4.4.0rc2 100% |################################################################################################################################################################################| Time: 0:00:00 707.92 kB/s
(ins)(root) $ conda env update -n root
Solving environment: done

Downloading and Extracting Packages
conda 4.4.6: ########################################################################################################################################################################################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Traceback (most recent call last):
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda/exceptions.py", line 683, in __call__
    elif isinstance(error, SafetyError):
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda_env/cli/main.py", line 74, in do_call
    exit_code = getattr(module, func_name)(args, parser)
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda_env/cli/main_update.py", line 107, in execute
    installer.install(prefix, specs, args, env)
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda_env/installers/pip.py", line 40, in _pip_install_via_requirements
    args, pip_version = pip_args(prefix)
ValueError: too many values to unpack (expected 2)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/maba/code/conda/mc/bin/conda-env", line 11, in <module>
    sys.exit(main())
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda_env/cli/main.py", line 84, in main
    return conda_exception_handler(do_call, args, parser)
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda/exceptions.py", line 907, in conda_exception_handler
    url = response.headers['Location']
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda/exceptions.py", line 686, in __call__
    elif context.safety_checks == SafetyChecks.warn:
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda/exceptions.py", line 720, in handle_exception
    class ExceptionHandler(object):
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda/exceptions.py", line 732, in handle_unexpected_exception
    return sys.stdout if context.json else sys.stderr
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda/exceptions.py", line 783, in print_error_report
    command = ' '.join(ensure_text_type(s) for s in sys.argv)
  File "/home/maba/code/conda/mc/lib/python3.6/site-packages/conda/cli/main_info.py", line 19, in <module>
    from .common import print_envs_list, stdout_json
ImportError: cannot import name 'print_envs_list'
```
</p></details>


Discussion:
Ok that's just awful.
Thanks for elucidating what's actually happening @mbargull 
I'm facing the same problem. Is there a workaround for this?
  
You can work around this issue by updating `conda` separately, before updating the rest of the environment:

```sh
conda update -y conda
conda env update -n root
```
YMMV, but this may help as a work-around:
```
conda config --set auto_update_conda False
```
It prevents `conda` from automatically updating itself when creating environments.

Resolving patch:
diff --git a/conda_env/cli/main.py b/conda_env/cli/main.py
--- a/conda_env/cli/main.py
+++ b/conda_env/cli/main.py
@@ -79,6 +79,7 @@ def main():
     initialize_logging()
     parser = create_parser()
     args = parser.parse_args()
+    os.environ["CONDA_AUTO_UPDATE_CONDA"] = "false"
     context.__init__(argparse_args=args)
     init_loggers(context)
     return conda_exception_handler(do_call, args, parser)


Test patch: