Repository: conda/conda

Issue:
Problem in conda list
Current conda install:

               platform : linux-32
          conda version : 4.3.30
       conda is private : False
      conda-env version : 4.3.30
    conda-build version : 3.0.27
         python version : 3.6.3.final.0
       requests version : 2.18.4
       root environment : /home/dhj/anaconda3-5.0.1  (writable)
    default environment : /home/dhj/anaconda3-5.0.1
       envs directories : /home/dhj/anaconda3-5.0.1/envs
                          /home/dhj/.conda/envs
          package cache : /home/dhj/anaconda3-5.0.1/pkgs
                          /home/dhj/.conda/pkgs
           channel URLs : https://repo.continuum.io/pkgs/main/linux-32
                          https://repo.continuum.io/pkgs/main/noarch
                          https://repo.continuum.io/pkgs/free/linux-32
                          https://repo.continuum.io/pkgs/free/noarch
                          https://repo.continuum.io/pkgs/r/linux-32
                          https://repo.continuum.io/pkgs/r/noarch
                          https://repo.continuum.io/pkgs/pro/linux-32
                          https://repo.continuum.io/pkgs/pro/noarch
            config file : None
             netrc file : None
           offline mode : False
             user-agent : conda/4.3.30 requests/2.18.4 CPython/3.6.3 Linux/4.10.0-40-generic debian/stretch/sid glibc/2.23    
                UID:GID : 1000:1000

`$ /home/dhj/anaconda3-5.0.1/bin/conda list`




    Traceback (most recent call last):
      File "/home/dhj/anaconda3-5.0.1/lib/python3.6/site-packages/conda/exceptions.py", line 640, in conda_exception_handler
        return_value = func(*args, **kwargs)
      File "/home/dhj/anaconda3-5.0.1/lib/python3.6/site-packages/conda/cli/main.py", line 140, in _main
        exit_code = args.func(args, p)
      File "/home/dhj/anaconda3-5.0.1/lib/python3.6/site-packages/conda/cli/main_list.py", line 242, in execute
        show_channel_urls=context.show_channel_urls)
      File "/home/dhj/anaconda3-5.0.1/lib/python3.6/site-packages/conda/cli/main_list.py", line 172, in print_packages
        installed = linked(prefix)
      File "/home/dhj/anaconda3-5.0.1/lib/python3.6/site-packages/conda/core/linked_data.py", line 123, in linked
        return set(linked_data(prefix, ignore_channels=ignore_channels).keys())
      File "/home/dhj/anaconda3-5.0.1/lib/python3.6/site-packages/conda/core/linked_data.py", line 115, in linked_data
        load_linked_data(prefix, dist_name, ignore_channels=ignore_channels)
      File "/home/dhj/anaconda3-5.0.1/lib/python3.6/site-packages/conda/core/linked_data.py", line 36, in load_linked_data
        rec = json.load(fi)
      File "/home/dhj/anaconda3-5.0.1/lib/python3.6/json/__init__.py", line 299, in load
        parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
      File "/home/dhj/anaconda3-5.0.1/lib/python3.6/json/__init__.py", line 354, in loads
        return _default_decoder.decode(s)
      File "/home/dhj/anaconda3-5.0.1/lib/python3.6/json/decoder.py", line 339, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/home/dhj/anaconda3-5.0.1/lib/python3.6/json/decoder.py", line 357, in raw_decode
        raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)



Discussion:

Resolving patch:
diff --git a/conda/core/linked_data.py b/conda/core/linked_data.py
--- a/conda/core/linked_data.py
+++ b/conda/core/linked_data.py
@@ -7,10 +7,11 @@
 
 from ..base.constants import CONDA_TARBALL_EXTENSION
 from ..base.context import context
-from ..common.compat import itervalues, with_metaclass
+from ..common.compat import JSONDecodeError, itervalues, with_metaclass
 from ..common.constants import NULL
 from ..common.serialize import json_load
-from ..exceptions import BasicClobberError, CondaDependencyError, maybe_raise
+from ..exceptions import BasicClobberError, CondaDependencyError, maybe_raise, \
+    CorruptedEnvironmentError
 from ..gateways.disk.create import write_as_json_to_file
 from ..gateways.disk.delete import rm_rf
 from ..models.dist import Dist
@@ -112,7 +113,11 @@ def _prefix_records(self):
     def _load_single_record(self, prefix_record_json_path):
         log.trace("loading prefix record %s", prefix_record_json_path)
         with open(prefix_record_json_path) as fh:
-            json_data = json_load(fh.read())
+            try:
+                json_data = json_load(fh.read())
+            except JSONDecodeError:
+                raise CorruptedEnvironmentError(self.prefix_path, prefix_record_json_path)
+
         prefix_record = PrefixRecord(**json_data)
         self.__prefix_records[prefix_record.name] = prefix_record
 
diff --git a/conda/exceptions.py b/conda/exceptions.py
--- a/conda/exceptions.py
+++ b/conda/exceptions.py
@@ -570,6 +570,23 @@ def __init__(self, expected_type, received_type, optional_message):
         super(CondaTypeError, self).__init__(msg)
 
 
+class CorruptedEnvironmentError(CondaError):
+    def __init__(self, environment_location, corrupted_file, **kwargs):
+        message = dals("""
+        The target environment has been corrupted. Corrupted environments most commonly
+        occur when the conda process is force-terminated while in an unlink-link
+        transaction.
+          environment location: %(environment_location)s
+          corrupted file: %(corrupted_file)s
+        """)
+        super(CorruptedEnvironmentError, self).__init__(
+            message,
+            environment_location=environment_location,
+            corrupted_file=corrupted_file,
+            **kwargs
+        )
+
+
 class CondaHistoryError(CondaError):
     def __init__(self, message):
         msg = '%s' % message


Test patch: