On build error, can't enter twice in the shell

Summary

When building a BuildStream element and an error happen, we get offered to enter in a shell to debug.

If we enter this shell, exit it immediately, and re-enter it, we get a huge stack trace.

Steps to reproduce

  • Build an element that will fail
  • choose 'shell' at the prompt
  • exit
  • choose 'shell' again
  • crash

What is the current bug behavior?

We get a huge stack trace.

What is the expected correct behavior?

We should re-enter the shell

Relevant logs and/or screenshots

[--:--:--][        ][    main:core activity                 ] BUG

    Traceback (most recent call last):
      File "/opt/bb/lib/python3.7/asyncio/events.py", line 88, in _run
        self._context.run(self._callback, *self._args)
      File "/home/bschubert/buildstream/buildstream/src/buildstream/_scheduler/jobs/job.py", line 451, in _parent_child_completed
        self._scheduler.job_completed(self, status)
      File "/home/bschubert/buildstream/buildstream/src/buildstream/_scheduler/scheduler.py", line 334, in job_completed
        self._notify(notification)
      File "/home/bschubert/buildstream/buildstream/src/buildstream/_scheduler/scheduler.py", line 584, in _notify
        self._notifier()
      File "/home/bschubert/buildstream/buildstream/src/buildstream/_stream.py", line 1648, in _scheduler_notification_handler
        self._state.fail_task(notification.job_action, notification.full_name, notification.element)
      File "/home/bschubert/buildstream/buildstream/src/buildstream/_state.py", line 337, in fail_task
        cb(action_name, full_name, element)
      File "/home/bschubert/buildstream/buildstream/src/buildstream/_frontend/app.py", line 620, in _job_failed
        self._handle_failure(element, action_name, failure, full_name)
      File "/home/bschubert/buildstream/buildstream/src/buildstream/_frontend/app.py", line 690, in _handle_failure
        None, Scope.BUILD, prompt, isolate=True, usebuildtree="always", unique_id=unique_id
      File "/home/bschubert/buildstream/buildstream/src/buildstream/_stream.py", line 261, in shell
        scope, directory, mounts=mounts, isolate=isolate, prompt=prompt, command=command, usebuildtree=buildtree
      File "/home/bschubert/buildstream/buildstream/src/buildstream/element.py", line 1922, in _shell
        with self._prepare_sandbox(scope, directory, shell=True, usebuildtree=usebuildtree) as sandbox:
      File "/opt/bb/lib/python3.7/contextlib.py", line 112, in __enter__
        return next(self.gen)
      File "/home/bschubert/buildstream/buildstream/src/buildstream/element.py", line 1384, in _prepare_sandbox
        self.stage(sandbox)
      File "/home/bschubert/buildstream/buildstream/src/buildstream/buildelement.py", line 232, in stage
        self.stage_sources(sandbox, self.get_variable("build-root"))
      File "/home/bschubert/buildstream/buildstream/src/buildstream/element.py", line 880, in stage_sources
        assert self.__staged_sources_directory is None
    AssertionError