# Buggy code
def reproduce(dvc_dir, cwd=None, **kwargs):
    """Run dvc repro and return the result."""
    from dvc.repo import Repo
    from dvc.repo.experiments import hash_exp

    unchanged = []

    def filter_pipeline(stages):
        unchanged.extend(
            [stage for stage in stages if isinstance(stage, PipelineStage)]
        )

    if cwd:
        old_cwd = os.getcwd()
        os.chdir(cwd)
    else:
        old_cwd = None
        cwd = os.getcwd()

    try:
        logger.debug("Running repro in '%s'", cwd)
        dvc = Repo(dvc_dir)

        # NOTE: for checkpoint experiments we handle persist outs slightly
        # differently than normal:
        #
        # - checkpoint out may not yet exist if this is the first time this
        #   experiment has been run, this is not an error condition for
        #   experiments
        # - at the start of a repro run, we need to remove the persist out
        #   and restore it to its last known (committed) state (which may
        #   be removed/does not yet exist) so that our executor workspace
        #   is not polluted with the (persistent) out from an unrelated
        #   experiment run
        checkpoint = kwargs.pop("checkpoint", False)
        dvc.checkout(allow_missing=checkpoint, force=checkpoint)
        stages = dvc.reproduce(
            on_unchanged=filter_pipeline,
            allow_missing=checkpoint,
            **kwargs,
        )
    finally:
        if old_cwd is not None:
            os.chdir(old_cwd)

    # ideally we would return stages here like a normal repro() call, but
    # stages is not currently picklable and cannot be returned across
    # multiprocessing calls
    return hash_exp(stages + unchanged)


# Traceback (git.exc.GitCommandError)
❯ dvc exp run --continue -v
...
2020-10-23 16:36:29,034 DEBUG: Commit to current experiment branch
2020-10-23 16:36:29,118 DEBUG: Checking out experiment commit 'd5dbcff520d30b5d92a863049f7259044a145a28'
2020-10-23 16:36:29,148 DEBUG: Patching local workspace
2020-10-23 16:36:29,154 DEBUG: Removing '/var/folders/s_/j8cg24s945dcvlh77c677f080000gn/T/tmpj3mfqijf'
2020-10-23 16:36:29,155 DEBUG: fetched: [(60,)]
2020-10-23 16:36:29,156 ERROR: failed to apply experiment changes.
------------------------------------------------------------
Traceback (most recent call last):
File "/Users/pmrowla/git/dvc/dvc/repo/experiments/__init__.py", line 770, in checkout_exp
self.repo.scm.repo.git.apply(tmp, reverse=True)
File "/Users/pmrowla/.virtualenvs/dvc/lib/python3.8/site-packages/git/cmd.py", line 542, in <lambda>
return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
File "/Users/pmrowla/.virtualenvs/dvc/lib/python3.8/site-packages/git/cmd.py", line 1005, in _call_process
return self.execute(call, **exec_kwargs)
File "/Users/pmrowla/.virtualenvs/dvc/lib/python3.8/site-packages/git/cmd.py", line 822, in execute
raise GitCommandError(command, status, stderr_value, stdout_value)
git.exc.GitCommandError: Cmd('git') failed due to: exit code(1)
cmdline: git apply --reverse /var/folders/s_/j8cg24s945dcvlh77c677f080000gn/T/tmpj3mfqijf
stderr: 'error: dvc.lock: already exists in working directory'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/pmrowla/git/dvc/dvc/command/experiments.py", line 456, in run
self.repo.experiments.run(
File "/Users/pmrowla/git/dvc/dvc/repo/experiments/__init__.py", line 881, in run
return run(self.repo, *args, **kwargs)
File "/Users/pmrowla/git/dvc/dvc/repo/__init__.py", line 51, in wrapper
return f(repo, *args, **kwargs)
File "/Users/pmrowla/git/dvc/dvc/repo/experiments/run.py", line 62, in run
return repo.experiments.reproduce_one(
File "/Users/pmrowla/git/dvc/dvc/repo/experiments/__init__.py", line 398, in reproduce_one
self.checkout_exp(exp_rev, allow_missing=checkpoint)
File "/Users/pmrowla/git/dvc/dvc/repo/experiments/__init__.py", line 38, in wrapper
return f(exp, *args, **kwargs)
File "/Users/pmrowla/git/dvc/dvc/repo/experiments/__init__.py", line 775, in checkout_exp
raise DvcException("failed to apply experiment changes.")
dvc.exceptions.DvcException: failed to apply experiment changes.
------------------------------------------------------------

# Fixed code
def reproduce(dvc_dir, cwd=None, **kwargs):
    """Run dvc repro and return the result."""
    from dvc.repo import Repo
    from dvc.repo.experiments import hash_exp

    unchanged = []

    def filter_pipeline(stages):
        unchanged.extend(
            [stage for stage in stages if isinstance(stage, PipelineStage)]
        )

    if cwd:
        old_cwd = os.getcwd()
        os.chdir(cwd)
    else:
        old_cwd = None
        cwd = os.getcwd()

    try:
        logger.debug("Running repro in '%s'", cwd)
        dvc = Repo(dvc_dir)

        # NOTE: for checkpoint experiments we handle persist outs slightly
        # differently than normal:
        #
        # - checkpoint out may not yet exist if this is the first time this
        #   experiment has been run, this is not an error condition for
        #   experiments
        # - at the start of a repro run, we need to remove the persist out
        #   and restore it to its last known (committed) state (which may
        #   be removed/does not yet exist) so that our executor workspace
        #   is not polluted with the (persistent) out from an unrelated
        #   experiment run
        checkpoint = kwargs.pop("checkpoint", False)
        dvc.checkout(allow_missing=checkpoint, force=checkpoint, quiet=checkpoint)
        stages = dvc.reproduce(
            on_unchanged=filter_pipeline,
            allow_missing=checkpoint,
            **kwargs,
        )
    finally:
        if old_cwd is not None:
            os.chdir(old_cwd)

    # ideally we would return stages here like a normal repro() call, but
    # stages is not currently picklable and cannot be returned across
    # multiprocessing calls
    return hash_exp(stages + unchanged)