prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  attach_socket_import tests hang waiting for "process" event
  (https://github.com/microsoft/ptvsd/tree/dbg_adapter_refactor)
  
  When running multiple tests with `start_method` set to `attach_socket_cmdline` or `attach_socket_import`, subsequent tests fail because they cannot reuse the port number. It looks like ptvsd is not getting shut down properly, or the next test starts running before ptvsd exits. 
  
  Estimate is uncertain, because this needs to be investigated to determine the root cause.
  
  Only happens on Python 2.7.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `src/ptvsd/__init__.py`
  Content:
  ```
  1 # Copyright (c) Microsoft Corporation. All rights reserved.
  2 # Licensed under the MIT License. See LICENSE in the project root
  3 # for license information.
  4 
  5 from __future__ import absolute_import, print_function, unicode_literals
  6 
  7 """An implementation of the Debug Adapter Protocol (DAP) for Python.
  8 
  9 https://microsoft.github.io/debug-adapter-protocol/
  10 """
  11 
  12 __all__ = [
  13     "__version__",
  14     "attach",
  15     "break_into_debugger",
  16     "debug_this_thread",
  17     "enable_attach",
  18     "is_attached",
  19     "wait_for_attach",
  20 ]
  21 
  22 # Force absolute path on Python 2.
  23 from os import path
  24 __file__ = path.abspath(__file__)
  25 del path
  26 
  27 from ptvsd import _version
  28 __version__ = _version.get_versions()["version"]
  29 del _version
  30 
  31 from ptvsd.server.attach_server import (
  32     attach,
  33     break_into_debugger,
  34     debug_this_thread,
  35     enable_attach,
  36     is_attached,
  37     wait_for_attach,
  38 )
  39
  ```
  --- 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()
  
  ```
  
golden_diff: diff --git a/src/ptvsd/__init__.py b/src/ptvsd/__init__.py
  --- a/src/ptvsd/__init__.py
  +++ b/src/ptvsd/__init__.py
  @@ -24,6 +24,14 @@
   __file__ = path.abspath(__file__)
   del path
   
  +# Preload encodings that we're going to use to avoid import deadlocks on Python 2.
  +import codecs
  +codecs.lookup('ascii')
  +codecs.lookup('utf8')
  +codecs.lookup('utf-8')
  +codecs.lookup('latin1')
  +codecs.lookup('latin-1')
  +
   from ptvsd import _version
   __version__ = _version.get_versions()["version"]
   del _version
  
verification_info: {"golden_diff": "diff --git a/src/ptvsd/__init__.py b/src/ptvsd/__init__.py\n--- a/src/ptvsd/__init__.py\n+++ b/src/ptvsd/__init__.py\n@@ -24,6 +24,14 @@\n __file__ = path.abspath(__file__)\n del path\n \n+# Preload encodings that we're going to use to avoid import deadlocks on Python 2.\n+import codecs\n+codecs.lookup('ascii')\n+codecs.lookup('utf8')\n+codecs.lookup('utf-8')\n+codecs.lookup('latin1')\n+codecs.lookup('latin-1')\n+\n from ptvsd import _version\n __version__ = _version.get_versions()[\"version\"]\n del _version\n", "issue": "attach_socket_import tests hang waiting for \"process\" event\n(https://github.com/microsoft/ptvsd/tree/dbg_adapter_refactor)\r\n\r\nWhen running multiple tests with `start_method` set to `attach_socket_cmdline` or `attach_socket_import`, subsequent tests fail because they cannot reuse the port number. It looks like ptvsd is not getting shut down properly, or the next test starts running before ptvsd exits. \r\n\r\nEstimate is uncertain, because this needs to be investigated to determine the root cause.\r\n\r\nOnly happens on Python 2.7.\n", "before_files": [{"content": "# Copyright (c) Microsoft Corporation. All rights reserved.\n# Licensed under the MIT License. See LICENSE in the project root\n# for license information.\n\nfrom __future__ import absolute_import, print_function, unicode_literals\n\n\"\"\"An implementation of the Debug Adapter Protocol (DAP) for Python.\n\nhttps://microsoft.github.io/debug-adapter-protocol/\n\"\"\"\n\n__all__ = [\n    \"__version__\",\n    \"attach\",\n    \"break_into_debugger\",\n    \"debug_this_thread\",\n    \"enable_attach\",\n    \"is_attached\",\n    \"wait_for_attach\",\n]\n\n# Force absolute path on Python 2.\nfrom os import path\n__file__ = path.abspath(__file__)\ndel path\n\nfrom ptvsd import _version\n__version__ = _version.get_versions()[\"version\"]\ndel _version\n\nfrom ptvsd.server.attach_server import (\n    attach,\n    break_into_debugger,\n    debug_this_thread,\n    enable_attach,\n    is_attached,\n    wait_for_attach,\n)\n", "path": "src/ptvsd/__init__.py"}], "after_files": [{"content": "# Copyright (c) Microsoft Corporation. All rights reserved.\n# Licensed under the MIT License. See LICENSE in the project root\n# for license information.\n\nfrom __future__ import absolute_import, print_function, unicode_literals\n\n\"\"\"An implementation of the Debug Adapter Protocol (DAP) for Python.\n\nhttps://microsoft.github.io/debug-adapter-protocol/\n\"\"\"\n\n__all__ = [\n    \"__version__\",\n    \"attach\",\n    \"break_into_debugger\",\n    \"debug_this_thread\",\n    \"enable_attach\",\n    \"is_attached\",\n    \"wait_for_attach\",\n]\n\n# Force absolute path on Python 2.\nfrom os import path\n__file__ = path.abspath(__file__)\ndel path\n\n# Preload encodings that we're going to use to avoid import deadlocks on Python 2.\nimport codecs\ncodecs.lookup('ascii')\ncodecs.lookup('utf8')\ncodecs.lookup('utf-8')\ncodecs.lookup('latin1')\ncodecs.lookup('latin-1')\n\nfrom ptvsd import _version\n__version__ = _version.get_versions()[\"version\"]\ndel _version\n\nfrom ptvsd.server.attach_server import (\n    attach,\n    break_into_debugger,\n    debug_this_thread,\n    enable_attach,\n    is_attached,\n    wait_for_attach,\n)\n", "path": "src/ptvsd/__init__.py"}]}