# Buggy code
def spinner(message=None, enabled=True, json=False):
    """
    Args:
        message (str, optional):
            An optional message to prefix the spinner with.
            If given, ': ' are automatically added.
        enabled (bool):
            If False, usage is a no-op.
        json (bool):
           If True, will not output non-json to stdout.

    """
    sp = Spinner(enabled)
    exception_raised = False
    try:
        if message:
            if json:
                pass
            else:
                sys.stdout.write("%s: " % message)
                sys.stdout.flush()
        if not json:
            sp.start()
        yield
    except:
        exception_raised = True
        raise
    finally:
        if not json:
            sp.stop()
        if message:
            if json:
                pass
            else:
                if exception_raised:
                    sys.stdout.write("failed\n")
                else:
                    sys.stdout.write("done\n")
                sys.stdout.flush()


# Traceback (BrokenPipeError)
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

# Fixed code
def spinner(message=None, enabled=True, json=False):
    """
    Args:
        message (str, optional):
            An optional message to prefix the spinner with.
            If given, ': ' are automatically added.
        enabled (bool):
            If False, usage is a no-op.
        json (bool):
           If True, will not output non-json to stdout.

    """
    sp = Spinner(enabled)
    exception_raised = False
    try:
        if message:
            if json:
                pass
            else:
                sys.stdout.write("%s: " % message)
                sys.stdout.flush()
        if not json:
            sp.start()
        yield
    except:
        exception_raised = True
        raise
    finally:
        if not json:
            sp.stop()
        if message:
            if json:
                pass
            else:
                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