Repository: conda/conda

Issue:
BrokenPipeError
    "command": "conda create -p ./untitled -y python=3.6"
    "user_agent": "conda/4.4.2 requests/2.18.4 CPython/3.6.3 Linux/4.10.0-28-generic ubuntu/16.04 glibc/2.23"

```
Traceback (most recent call last):
  File \"/home/jon/anaconda3/lib/python3.6/site-packages/conda/exceptions.py\", line 722, in __call__
    return func(*args, **kwargs)
  File \"/home/jon/anaconda3/lib/python3.6/site-packages/conda/cli/main.py\", line 78, in _main
    exit_code = do_call(args, p)
  File \"/home/jon/anaconda3/lib/python3.6/site-packages/conda/cli/conda_argparse.py\", line 75, in do_call
    exit_code = getattr(module, func_name)(args, parser)
  File \"/home/jon/anaconda3/lib/python3.6/site-packages/conda/cli/main_create.py\", line 11, in execute
    install(args, parser, 'create')
  File \"/home/jon/anaconda3/lib/python3.6/site-packages/conda/cli/install.py\", line 239, in install
    handle_txn(progressive_fetch_extract, unlink_link_transaction, prefix, args, newenv)
  File \"/home/jon/anaconda3/lib/python3.6/site-packages/conda/cli/install.py\", line 265, in handle_txn
    progressive_fetch_extract.execute()
  File \"/home/jon/anaconda3/lib/python3.6/site-packages/conda/core/package_cache.py\", line 543, in execute
    exc = self._execute_actions(prec_or_spec, prec_actions)
  File \"/home/jon/anaconda3/lib/python3.6/site-packages/conda/core/package_cache.py\", line 595, in _execute_actions
    progress_bar.close()
  File \"/home/jon/anaconda3/lib/python3.6/site-packages/conda/common/io.py\", line 376, in close
    self.pbar.close()
  File \"/home/jon/anaconda3/lib/python3.6/site-packages/conda/_vendor/tqdm/_tqdm.py\", line 1054, in close
    self.bar_format, self.postfix, self.unit_divisor))
  File \"/home/jon/anaconda3/lib/python3.6/site-packages/conda/_vendor/tqdm/_tqdm.py\", line 201, in print_status
    fp_write('\\r' + s + (' ' * max(last_len[0] - len_s, 0)))
  File \"/home/jon/anaconda3/lib/python3.6/site-packages/conda/_vendor/tqdm/_tqdm.py\", line 195, in fp_write
    fp_flush()
BrokenPipeError: [Errno 32] Broken pipe

```


Discussion:

Resolving patch:
diff --git a/conda/common/io.py b/conda/common/io.py
--- a/conda/common/io.py
+++ b/conda/common/io.py
@@ -5,6 +5,7 @@
 from concurrent.futures import ThreadPoolExecutor
 from contextlib import contextmanager
 from enum import Enum
+from errno import EPIPE, ESHUTDOWN
 from functools import wraps
 from itertools import cycle
 import json
@@ -326,11 +327,17 @@ def spinner(message=None, enabled=True, json=False):
             if json:
                 pass
             else:
-                if exception_raised:
-                    sys.stdout.write("failed\n")
-                else:
-                    sys.stdout.write("done\n")
-                sys.stdout.flush()
+                try:
+                    if exception_raised:
+                        sys.stdout.write("failed\n")
+                    else:
+                        sys.stdout.write("done\n")
+                    sys.stdout.flush()
+                except (IOError, OSError) as e:
+                    # Ignore BrokenPipeError and errors related to stdout or stderr being
+                    # closed by a downstream program.
+                    if e.errno not in (EPIPE, ESHUTDOWN):
+                        raise
 
 
 class ProgressBar(object):
@@ -368,12 +375,18 @@ def finish(self):
         self.update_to(1)
 
     def close(self):
-        if self.json:
-            sys.stdout.write('{"fetch":"%s","finished":true,"maxval":1,"progress":1}\n\0'
-                             % self.description)
-            sys.stdout.flush()
-        elif self.enabled:
-            self.pbar.close()
+        try:
+            if self.json:
+                sys.stdout.write('{"fetch":"%s","finished":true,"maxval":1,"progress":1}\n\0'
+                                 % self.description)
+                sys.stdout.flush()
+            elif self.enabled:
+                self.pbar.close()
+        except (IOError, OSError) as e:
+            # Ignore BrokenPipeError and errors related to stdout or stderr being
+            # closed by a downstream program.
+            if e.errno not in (EPIPE, ESHUTDOWN):
+                raise
         self.enabled = False
 
 


Test patch: