# Buggy code
def execute(self):
    binary = self.require_single_root_target()
    if isinstance(binary, PythonBinary):
        # We can't throw if binary isn't a PythonBinary, because perhaps we were called on a
        # jvm_binary, in which case we have to no-op and let jvm_run do its thing.
        # TODO(benjy): Use MutexTask to coordinate this.

        pex = self.create_pex(binary.pexinfo)
        self.context.release_lock()
        with self.context.new_workunit(name="run", labels=[WorkUnitLabel.RUN]):
            args = []
            for arg in self.get_options().args:
                args.extend(safe_shlex_split(arg))
            args += self.get_passthru_args()
            po = pex.run(blocking=False, args=args)
            try:
                result = po.wait()
                if result != 0:
                    msg = "{interpreter} {entry_point} {args} ... exited non-zero ({code})".format(
                        interpreter=pex.interpreter.binary,
                        entry_point=binary.entry_point,
                        args=" ".join(args),
                        code=result,
                    )
                    raise TaskError(msg, exit_code=result)
            except KeyboardInterrupt:
                po.send_signal(signal.SIGINT)
                raise


# Traceback (KeyError)
Traceback (most recent call last):
File "/Users/mike/code/fr/source/.pants.d/run/py/CPython-2.7.10/f4fb689ebb3890a4f8f61526b64dbf8f00714968-DefaultFingerprintStrategy_4fa5ce70a419/.bootstrap/_pex/pex.py", line 360, in execute
self._wrap_coverage(self._wrap_profiling, self._execute)
File "/Users/mike/code/fr/source/.pants.d/run/py/CPython-2.7.10/f4fb689ebb3890a4f8f61526b64dbf8f00714968-DefaultFingerprintStrategy_4fa5ce70a419/.bootstrap/_pex/pex.py", line 288, in _wrap_coverage
runner(*args)
File "/Users/mike/code/fr/source/.pants.d/run/py/CPython-2.7.10/f4fb689ebb3890a4f8f61526b64dbf8f00714968-DefaultFingerprintStrategy_4fa5ce70a419/.bootstrap/_pex/pex.py", line 320, in _wrap_profiling
runner(*args)
File "/Users/mike/code/fr/source/.pants.d/run/py/CPython-2.7.10/f4fb689ebb3890a4f8f61526b64dbf8f00714968-DefaultFingerprintStrategy_4fa5ce70a419/.bootstrap/_pex/pex.py", line 403, in _execute
return self.execute_entry(self._pex_info.entry_point)
File "/Users/mike/code/fr/source/.pants.d/run/py/CPython-2.7.10/f4fb689ebb3890a4f8f61526b64dbf8f00714968-DefaultFingerprintStrategy_4fa5ce70a419/.bootstrap/_pex/pex.py", line 461, in execute_entry
return runner(entry_point)
File "/Users/mike/code/fr/source/.pants.d/run/py/CPython-2.7.10/f4fb689ebb3890a4f8f61526b64dbf8f00714968-DefaultFingerprintStrategy_4fa5ce70a419/.bootstrap/_pex/pex.py", line 466, in execute_module
runpy.run_module(module_name, run_name='__main__')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 180, in run_module
fname, loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/Users/mike/code/fr/source/.pants.d/pyprep/sources/f4fb689ebb3890a4f8f61526b64dbf8f00714968-DefaultFingerprintStrategy_898c4dfd47d2/env_var.py", line 3, in <module>
print(os.environ['SHELL'])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py", line 23, in __getitem__
raise KeyError(key)
KeyError: 'SHELL'

FAILURE: /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 env_var  ... exited non-zero (1)

# Fixed code
def execute(self):
    binary = self.require_single_root_target()
    if isinstance(binary, PythonBinary):
        # We can't throw if binary isn't a PythonBinary, because perhaps we were called on a
        # jvm_binary, in which case we have to no-op and let jvm_run do its thing.
        # TODO(benjy): Use MutexTask to coordinate this.

        pex = self.create_pex(binary.pexinfo)
        self.context.release_lock()
        with self.context.new_workunit(name="run", labels=[WorkUnitLabel.RUN]):
            args = []
            for arg in self.get_options().args:
                args.extend(safe_shlex_split(arg))
            args += self.get_passthru_args()
            po = pex.run(blocking=False, args=args, env=os.environ.copy())
            try:
                result = po.wait()
                if result != 0:
                    msg = "{interpreter} {entry_point} {args} ... exited non-zero ({code})".format(
                        interpreter=pex.interpreter.binary,
                        entry_point=binary.entry_point,
                        args=" ".join(args),
                        code=result,
                    )
                    raise TaskError(msg, exit_code=result)
            except KeyboardInterrupt:
                po.send_signal(signal.SIGINT)
                raise