We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Bootstrap demotion is not robust
While working https://github.com/pantsbuild/pants/issues/6927 it was discovered that there were multiple `.bootstrap/` on the sys.path and only one was demoted, leading to pants, when running in a pex, picking .bootstrap.pex instead of the pex dist it in `.deps/`. In this case, Pants was purposefully duplicating pex path entries to work around a bug in the `coverage` library. PEX should be robust and demote all instances of it's `.bootstrap/` entry on the sys.path.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pex/bootstrap.py`
Content:
```
1 # coding=utf-8
2 # Copyright 2018 Pants project contributors (see CONTRIBUTORS.md).
3 # Licensed under the Apache License, Version 2.0 (see LICENSE).
4 
5 import os
6 
7 
8 class Bootstrap(object):
9   """Supports introspection of the PEX bootstrap code."""
10 
11   _INSTANCE = None
12 
13   @classmethod
14   def locate(cls):
15     """Locates the active PEX bootstrap.
16 
17     :rtype: :class:`Bootstrap`
18     """
19     if cls._INSTANCE is None:
20       bootstrap_path = __file__
21       module_import_path = __name__.split('.')
22 
23       # For example, our __file__ might be requests.pex/.bootstrap/pex/bootstrap.pyc and our import
24       # path pex.bootstrap; so we walk back through all the module components of our import path to
25       # find the base sys.path entry where we were found (requests.pex/.bootstrap in this example).
26       for _ in module_import_path:
27         bootstrap_path = os.path.dirname(bootstrap_path)
28 
29       cls._INSTANCE = cls(sys_path_entry=bootstrap_path)
30     return cls._INSTANCE
31 
32   def __init__(self, sys_path_entry):
33     self._sys_path_entry = sys_path_entry
34     self._realpath = os.path.realpath(self._sys_path_entry)
35 
36   def demote(self):
37     """Demote the bootstrap code to the end of the `sys.path` so it is found last.
38 
39     :return: The list of un-imported bootstrap modules.
40     :rtype: list of :class:`types.ModuleType`
41     """
42     import sys  # Grab a hold of `sys` early since we'll be un-importing our module in this process.
43 
44     unimported_modules = []
45     for name, module in reversed(sorted(sys.modules.items())):
46       if self.imported_from_bootstrap(module):
47         unimported_modules.append(sys.modules.pop(name))
48 
49     sys.path.remove(self._sys_path_entry)
50     sys.path.append(self._sys_path_entry)
51 
52     return unimported_modules
53 
54   def imported_from_bootstrap(self, module):
55     """Return ``True`` if the given ``module`` object was imported from bootstrap code.
56 
57     :param module: The module to check the provenance of.
58     :type module: :class:`types.ModuleType`
59     :rtype: bool
60     """
61 
62     # A vendored module.
63     path = getattr(module, '__file__', None)
64     if path and os.path.realpath(path).startswith(self._realpath):
65       return True
66 
67     # A vendored package.
68     path = getattr(module, '__path__', None)
69     if path and any(os.path.realpath(path_item).startswith(self._realpath)
70                     for path_item in path):
71       return True
72 
73     return False
74 
75   def __repr__(self):
76     return '{cls}(sys_path_entry={sys_path_entry!r})'.format(cls=type(self).__name__,
77                                                              sys_path_entry=self._sys_path_entry)
78
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pex/bootstrap.py b/pex/bootstrap.py
--- a/pex/bootstrap.py
+++ b/pex/bootstrap.py
@@ -46,7 +46,7 @@
       if self.imported_from_bootstrap(module):
         unimported_modules.append(sys.modules.pop(name))
 
-    sys.path.remove(self._sys_path_entry)
+    sys.path[:] = [path for path in sys.path if os.path.realpath(path) != self._realpath]
     sys.path.append(self._sys_path_entry)
 
     return unimported_modules

{"golden_diff": "diff --git a/pex/bootstrap.py b/pex/bootstrap.py\n--- a/pex/bootstrap.py\n+++ b/pex/bootstrap.py\n@@ -46,7 +46,7 @@\n       if self.imported_from_bootstrap(module):\n         unimported_modules.append(sys.modules.pop(name))\n \n-    sys.path.remove(self._sys_path_entry)\n+    sys.path[:] = [path for path in sys.path if os.path.realpath(path) != self._realpath]\n     sys.path.append(self._sys_path_entry)\n \n     return unimported_modules\n", "issue": "Bootstrap demotion is not robust\nWhile working https://github.com/pantsbuild/pants/issues/6927 it was discovered that there were multiple `.bootstrap/` on the sys.path and only one was demoted, leading to pants, when running in a pex, picking .bootstrap.pex instead of the pex dist it in `.deps/`. In this case, Pants was purposefully duplicating pex path entries to work around a bug in the `coverage` library. PEX should be robust and demote all instances of it's `.bootstrap/` entry on the sys.path.\n", "before_files": [{"content": "# coding=utf-8\n# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md).\n# Licensed under the Apache License, Version 2.0 (see LICENSE).\n\nimport os\n\n\nclass Bootstrap(object):\n  \"\"\"Supports introspection of the PEX bootstrap code.\"\"\"\n\n  _INSTANCE = None\n\n  @classmethod\n  def locate(cls):\n    \"\"\"Locates the active PEX bootstrap.\n\n    :rtype: :class:`Bootstrap`\n    \"\"\"\n    if cls._INSTANCE is None:\n      bootstrap_path = __file__\n      module_import_path = __name__.split('.')\n\n      # For example, our __file__ might be requests.pex/.bootstrap/pex/bootstrap.pyc and our import\n      # path pex.bootstrap; so we walk back through all the module components of our import path to\n      # find the base sys.path entry where we were found (requests.pex/.bootstrap in this example).\n      for _ in module_import_path:\n        bootstrap_path = os.path.dirname(bootstrap_path)\n\n      cls._INSTANCE = cls(sys_path_entry=bootstrap_path)\n    return cls._INSTANCE\n\n  def __init__(self, sys_path_entry):\n    self._sys_path_entry = sys_path_entry\n    self._realpath = os.path.realpath(self._sys_path_entry)\n\n  def demote(self):\n    \"\"\"Demote the bootstrap code to the end of the `sys.path` so it is found last.\n\n    :return: The list of un-imported bootstrap modules.\n    :rtype: list of :class:`types.ModuleType`\n    \"\"\"\n    import sys  # Grab a hold of `sys` early since we'll be un-importing our module in this process.\n\n    unimported_modules = []\n    for name, module in reversed(sorted(sys.modules.items())):\n      if self.imported_from_bootstrap(module):\n        unimported_modules.append(sys.modules.pop(name))\n\n    sys.path.remove(self._sys_path_entry)\n    sys.path.append(self._sys_path_entry)\n\n    return unimported_modules\n\n  def imported_from_bootstrap(self, module):\n    \"\"\"Return ``True`` if the given ``module`` object was imported from bootstrap code.\n\n    :param module: The module to check the provenance of.\n    :type module: :class:`types.ModuleType`\n    :rtype: bool\n    \"\"\"\n\n    # A vendored module.\n    path = getattr(module, '__file__', None)\n    if path and os.path.realpath(path).startswith(self._realpath):\n      return True\n\n    # A vendored package.\n    path = getattr(module, '__path__', None)\n    if path and any(os.path.realpath(path_item).startswith(self._realpath)\n                    for path_item in path):\n      return True\n\n    return False\n\n  def __repr__(self):\n    return '{cls}(sys_path_entry={sys_path_entry!r})'.format(cls=type(self).__name__,\n                                                             sys_path_entry=self._sys_path_entry)\n", "path": "pex/bootstrap.py"}], "after_files": [{"content": "# coding=utf-8\n# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md).\n# Licensed under the Apache License, Version 2.0 (see LICENSE).\n\nimport os\n\n\nclass Bootstrap(object):\n  \"\"\"Supports introspection of the PEX bootstrap code.\"\"\"\n\n  _INSTANCE = None\n\n  @classmethod\n  def locate(cls):\n    \"\"\"Locates the active PEX bootstrap.\n\n    :rtype: :class:`Bootstrap`\n    \"\"\"\n    if cls._INSTANCE is None:\n      bootstrap_path = __file__\n      module_import_path = __name__.split('.')\n\n      # For example, our __file__ might be requests.pex/.bootstrap/pex/bootstrap.pyc and our import\n      # path pex.bootstrap; so we walk back through all the module components of our import path to\n      # find the base sys.path entry where we were found (requests.pex/.bootstrap in this example).\n      for _ in module_import_path:\n        bootstrap_path = os.path.dirname(bootstrap_path)\n\n      cls._INSTANCE = cls(sys_path_entry=bootstrap_path)\n    return cls._INSTANCE\n\n  def __init__(self, sys_path_entry):\n    self._sys_path_entry = sys_path_entry\n    self._realpath = os.path.realpath(self._sys_path_entry)\n\n  def demote(self):\n    \"\"\"Demote the bootstrap code to the end of the `sys.path` so it is found last.\n\n    :return: The list of un-imported bootstrap modules.\n    :rtype: list of :class:`types.ModuleType`\n    \"\"\"\n    import sys  # Grab a hold of `sys` early since we'll be un-importing our module in this process.\n\n    unimported_modules = []\n    for name, module in reversed(sorted(sys.modules.items())):\n      if self.imported_from_bootstrap(module):\n        unimported_modules.append(sys.modules.pop(name))\n\n    sys.path[:] = [path for path in sys.path if os.path.realpath(path) != self._realpath]\n    sys.path.append(self._sys_path_entry)\n\n    return unimported_modules\n\n  def imported_from_bootstrap(self, module):\n    \"\"\"Return ``True`` if the given ``module`` object was imported from bootstrap code.\n\n    :param module: The module to check the provenance of.\n    :type module: :class:`types.ModuleType`\n    :rtype: bool\n    \"\"\"\n\n    # A vendored module.\n    path = getattr(module, '__file__', None)\n    if path and os.path.realpath(path).startswith(self._realpath):\n      return True\n\n    # A vendored package.\n    path = getattr(module, '__path__', None)\n    if path and any(os.path.realpath(path_item).startswith(self._realpath)\n                    for path_item in path):\n      return True\n\n    return False\n\n  def __repr__(self):\n    return '{cls}(sys_path_entry={sys_path_entry!r})'.format(cls=type(self).__name__,\n                                                             sys_path_entry=self._sys_path_entry)\n", "path": "pex/bootstrap.py"}]}