prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Provide a public API to register threads not created by Python for debugging
  This can be done by simply wrapping `pydevd.settrace(suspend=False)`
  
  --- 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/attach_server.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 ptvsd._remote import (
  6     attach as ptvsd_attach,
  7     enable_attach as ptvsd_enable_attach,
  8     _pydevd_settrace,
  9 )
  10 from ptvsd.wrapper import debugger_attached
  11 import sys
  12 from _pydevd_bundle.pydevd_constants import get_global_debugger
  13 from pydevd_file_utils import get_abs_path_real_path_and_base_from_frame
  14 
  15 WAIT_TIMEOUT = 1.0
  16 
  17 DEFAULT_HOST = '0.0.0.0'
  18 DEFAULT_PORT = 5678
  19 
  20 _pending_threads = set()
  21 
  22 
  23 def wait_for_attach(timeout=None):
  24     """If a remote debugger is attached, returns immediately. Otherwise,
  25     blocks until a remote debugger attaches to this process, or until the
  26     optional timeout occurs.
  27 
  28     Parameters
  29     ----------
  30     timeout : float, optional
  31         The timeout for the operation in seconds (or fractions thereof).
  32     """
  33     debugger_attached.wait(timeout)
  34 
  35 
  36 def enable_attach(address=(DEFAULT_HOST, DEFAULT_PORT), redirect_output=True):
  37     """Enables a client to attach to this process remotely to debug Python code.
  38 
  39     Parameters
  40     ----------
  41     address : (str, int), optional
  42         Specifies the interface and port on which the debugging server should
  43         listen for TCP connections. It is in the same format as used for
  44         regular sockets of the `socket.AF_INET` family, i.e. a tuple of
  45         ``(hostname, port)``. On client side, the server is identified by the
  46         Qualifier string in the usual ``'hostname:port'`` format, e.g.:
  47         ``'myhost.cloudapp.net:5678'``. Default is ``('0.0.0.0', 5678)``.
  48     redirect_output : bool, optional
  49         Specifies whether any output (on both `stdout` and `stderr`) produced
  50         by this program should be sent to the debugger. Default is ``True``.
  51 
  52     Notes
  53     -----
  54     This function returns immediately after setting up the debugging server,
  55     and does not block program execution. If you need to block until debugger
  56     is attached, call `ptvsd.wait_for_attach`. The debugger can be detached
  57     and re-attached multiple times after `enable_attach` is called.
  58 
  59     Only the thread on which this function is called, and any threads that are
  60     created after it returns, will be visible in the debugger once it is
  61     attached. Any threads that are already running before this function is
  62     called will not be visible.
  63     """
  64     if is_attached():
  65         return
  66     debugger_attached.clear()
  67 
  68     # Ensure port is int
  69     port = address[1]
  70     address = (address[0], port if type(port) is int else int(port))
  71 
  72     ptvsd_enable_attach(
  73         address,
  74         redirect_output=redirect_output,
  75     )
  76 
  77 
  78 def attach(address, redirect_output=True):
  79     """Attaches this process to the debugger listening on a given address.
  80 
  81     Parameters
  82     ----------
  83     address : (str, int), optional
  84         Specifies the interface and port on which the debugger is listening
  85         for TCP connections. It is in the same format as used for
  86         regular sockets of the `socket.AF_INET` family, i.e. a tuple of
  87         ``(hostname, port)``.
  88     redirect_output : bool, optional
  89         Specifies whether any output (on both `stdout` and `stderr`) produced
  90         by this program should be sent to the debugger. Default is ``True``.
  91     """
  92     if is_attached():
  93         return
  94     debugger_attached.clear()
  95 
  96     # Ensure port is int
  97     port = address[1]
  98     address = (address[0], port if type(port) is int else int(port))
  99 
  100     ptvsd_attach(address, redirect_output=redirect_output)
  101 
  102 # TODO: Add disable_attach()?
  103 
  104 
  105 def is_attached():
  106     """Returns ``True`` if debugger is attached, ``False`` otherwise."""
  107     return debugger_attached.isSet()
  108 
  109 
  110 def break_into_debugger():
  111     """If a remote debugger is attached, pauses execution of all threads,
  112     and breaks into the debugger with current thread as active.
  113     """
  114     if not is_attached():
  115         return
  116 
  117     # Get the first frame in the stack that's not an internal frame.
  118     global_debugger = get_global_debugger()
  119     stop_at_frame = sys._getframe().f_back
  120     while stop_at_frame is not None and global_debugger.get_file_type(
  121             get_abs_path_real_path_and_base_from_frame(stop_at_frame)) == global_debugger.PYDEV_FILE:
  122         stop_at_frame = stop_at_frame.f_back
  123 
  124     _pydevd_settrace(
  125         suspend=True,
  126         trace_only_current_thread=True,
  127         patch_multiprocessing=False,
  128         stop_at_frame=stop_at_frame,
  129     )
  130     stop_at_frame = None
  131
  ```
  
  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 __all__ = [
  6     '__version__', '__author__',
  7     'enable_attach', 'wait_for_attach', 'break_into_debugger', 'is_attached',
  8 ]
  9 
  10 
  11 # "force_pydevd" must be imported first to ensure (via side effects)
  12 # that the ptvsd-vendored copy of pydevd gets used.
  13 from ._vendored import force_pydevd
  14 from ptvsd.version import __version__, __author__
  15 from ptvsd.attach_server import (
  16     enable_attach, wait_for_attach, break_into_debugger, is_attached,
  17 )
  18 del force_pydevd
  19
  ```
  --- 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
  @@ -12,7 +12,12 @@
   # that the ptvsd-vendored copy of pydevd gets used.
   from ._vendored import force_pydevd
   from ptvsd.version import __version__, __author__
  -from ptvsd.attach_server import (
  -    enable_attach, wait_for_attach, break_into_debugger, is_attached,
  +from ptvsd.attach_server import (  # noqa
  +    attach,
  +    break_into_debugger,
  +    debug_this_thread,
  +    enable_attach,
  +    is_attached,
  +    wait_for_attach,
   )
   del force_pydevd
  diff --git a/src/ptvsd/attach_server.py b/src/ptvsd/attach_server.py
  --- a/src/ptvsd/attach_server.py
  +++ b/src/ptvsd/attach_server.py
  @@ -9,6 +9,8 @@
   )
   from ptvsd.wrapper import debugger_attached
   import sys
  +
  +import pydevd
   from _pydevd_bundle.pydevd_constants import get_global_debugger
   from pydevd_file_utils import get_abs_path_real_path_and_base_from_frame
   
  @@ -128,3 +130,7 @@
           stop_at_frame=stop_at_frame,
       )
       stop_at_frame = None
  +
  +
  +def debug_this_thread():
  +    pydevd.settrace(suspend=False)
  
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@@ -12,7 +12,12 @@\n # that the ptvsd-vendored copy of pydevd gets used.\n from ._vendored import force_pydevd\n from ptvsd.version import __version__, __author__\n-from ptvsd.attach_server import (\n-    enable_attach, wait_for_attach, break_into_debugger, is_attached,\n+from ptvsd.attach_server import (  # noqa\n+    attach,\n+    break_into_debugger,\n+    debug_this_thread,\n+    enable_attach,\n+    is_attached,\n+    wait_for_attach,\n )\n del force_pydevd\ndiff --git a/src/ptvsd/attach_server.py b/src/ptvsd/attach_server.py\n--- a/src/ptvsd/attach_server.py\n+++ b/src/ptvsd/attach_server.py\n@@ -9,6 +9,8 @@\n )\n from ptvsd.wrapper import debugger_attached\n import sys\n+\n+import pydevd\n from _pydevd_bundle.pydevd_constants import get_global_debugger\n from pydevd_file_utils import get_abs_path_real_path_and_base_from_frame\n \n@@ -128,3 +130,7 @@\n         stop_at_frame=stop_at_frame,\n     )\n     stop_at_frame = None\n+\n+\n+def debug_this_thread():\n+    pydevd.settrace(suspend=False)\n", "issue": "Provide a public API to register threads not created by Python for debugging\nThis can be done by simply wrapping `pydevd.settrace(suspend=False)`\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 ptvsd._remote import (\n    attach as ptvsd_attach,\n    enable_attach as ptvsd_enable_attach,\n    _pydevd_settrace,\n)\nfrom ptvsd.wrapper import debugger_attached\nimport sys\nfrom _pydevd_bundle.pydevd_constants import get_global_debugger\nfrom pydevd_file_utils import get_abs_path_real_path_and_base_from_frame\n\nWAIT_TIMEOUT = 1.0\n\nDEFAULT_HOST = '0.0.0.0'\nDEFAULT_PORT = 5678\n\n_pending_threads = set()\n\n\ndef wait_for_attach(timeout=None):\n    \"\"\"If a remote debugger is attached, returns immediately. Otherwise,\n    blocks until a remote debugger attaches to this process, or until the\n    optional timeout occurs.\n\n    Parameters\n    ----------\n    timeout : float, optional\n        The timeout for the operation in seconds (or fractions thereof).\n    \"\"\"\n    debugger_attached.wait(timeout)\n\n\ndef enable_attach(address=(DEFAULT_HOST, DEFAULT_PORT), redirect_output=True):\n    \"\"\"Enables a client to attach to this process remotely to debug Python code.\n\n    Parameters\n    ----------\n    address : (str, int), optional\n        Specifies the interface and port on which the debugging server should\n        listen for TCP connections. It is in the same format as used for\n        regular sockets of the `socket.AF_INET` family, i.e. a tuple of\n        ``(hostname, port)``. On client side, the server is identified by the\n        Qualifier string in the usual ``'hostname:port'`` format, e.g.:\n        ``'myhost.cloudapp.net:5678'``. Default is ``('0.0.0.0', 5678)``.\n    redirect_output : bool, optional\n        Specifies whether any output (on both `stdout` and `stderr`) produced\n        by this program should be sent to the debugger. Default is ``True``.\n\n    Notes\n    -----\n    This function returns immediately after setting up the debugging server,\n    and does not block program execution. If you need to block until debugger\n    is attached, call `ptvsd.wait_for_attach`. The debugger can be detached\n    and re-attached multiple times after `enable_attach` is called.\n\n    Only the thread on which this function is called, and any threads that are\n    created after it returns, will be visible in the debugger once it is\n    attached. Any threads that are already running before this function is\n    called will not be visible.\n    \"\"\"\n    if is_attached():\n        return\n    debugger_attached.clear()\n\n    # Ensure port is int\n    port = address[1]\n    address = (address[0], port if type(port) is int else int(port))\n\n    ptvsd_enable_attach(\n        address,\n        redirect_output=redirect_output,\n    )\n\n\ndef attach(address, redirect_output=True):\n    \"\"\"Attaches this process to the debugger listening on a given address.\n\n    Parameters\n    ----------\n    address : (str, int), optional\n        Specifies the interface and port on which the debugger is listening\n        for TCP connections. It is in the same format as used for\n        regular sockets of the `socket.AF_INET` family, i.e. a tuple of\n        ``(hostname, port)``.\n    redirect_output : bool, optional\n        Specifies whether any output (on both `stdout` and `stderr`) produced\n        by this program should be sent to the debugger. Default is ``True``.\n    \"\"\"\n    if is_attached():\n        return\n    debugger_attached.clear()\n\n    # Ensure port is int\n    port = address[1]\n    address = (address[0], port if type(port) is int else int(port))\n\n    ptvsd_attach(address, redirect_output=redirect_output)\n\n# TODO: Add disable_attach()?\n\n\ndef is_attached():\n    \"\"\"Returns ``True`` if debugger is attached, ``False`` otherwise.\"\"\"\n    return debugger_attached.isSet()\n\n\ndef break_into_debugger():\n    \"\"\"If a remote debugger is attached, pauses execution of all threads,\n    and breaks into the debugger with current thread as active.\n    \"\"\"\n    if not is_attached():\n        return\n\n    # Get the first frame in the stack that's not an internal frame.\n    global_debugger = get_global_debugger()\n    stop_at_frame = sys._getframe().f_back\n    while stop_at_frame is not None and global_debugger.get_file_type(\n            get_abs_path_real_path_and_base_from_frame(stop_at_frame)) == global_debugger.PYDEV_FILE:\n        stop_at_frame = stop_at_frame.f_back\n\n    _pydevd_settrace(\n        suspend=True,\n        trace_only_current_thread=True,\n        patch_multiprocessing=False,\n        stop_at_frame=stop_at_frame,\n    )\n    stop_at_frame = None\n", "path": "src/ptvsd/attach_server.py"}, {"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\n__all__ = [\n    '__version__', '__author__',\n    'enable_attach', 'wait_for_attach', 'break_into_debugger', 'is_attached',\n]\n\n\n# \"force_pydevd\" must be imported first to ensure (via side effects)\n# that the ptvsd-vendored copy of pydevd gets used.\nfrom ._vendored import force_pydevd\nfrom ptvsd.version import __version__, __author__\nfrom ptvsd.attach_server import (\n    enable_attach, wait_for_attach, break_into_debugger, is_attached,\n)\ndel force_pydevd\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 ptvsd._remote import (\n    attach as ptvsd_attach,\n    enable_attach as ptvsd_enable_attach,\n    _pydevd_settrace,\n)\nfrom ptvsd.wrapper import debugger_attached\nimport sys\n\nimport pydevd\nfrom _pydevd_bundle.pydevd_constants import get_global_debugger\nfrom pydevd_file_utils import get_abs_path_real_path_and_base_from_frame\n\nWAIT_TIMEOUT = 1.0\n\nDEFAULT_HOST = '0.0.0.0'\nDEFAULT_PORT = 5678\n\n_pending_threads = set()\n\n\ndef wait_for_attach(timeout=None):\n    \"\"\"If a remote debugger is attached, returns immediately. Otherwise,\n    blocks until a remote debugger attaches to this process, or until the\n    optional timeout occurs.\n\n    Parameters\n    ----------\n    timeout : float, optional\n        The timeout for the operation in seconds (or fractions thereof).\n    \"\"\"\n    debugger_attached.wait(timeout)\n\n\ndef enable_attach(address=(DEFAULT_HOST, DEFAULT_PORT), redirect_output=True):\n    \"\"\"Enables a client to attach to this process remotely to debug Python code.\n\n    Parameters\n    ----------\n    address : (str, int), optional\n        Specifies the interface and port on which the debugging server should\n        listen for TCP connections. It is in the same format as used for\n        regular sockets of the `socket.AF_INET` family, i.e. a tuple of\n        ``(hostname, port)``. On client side, the server is identified by the\n        Qualifier string in the usual ``'hostname:port'`` format, e.g.:\n        ``'myhost.cloudapp.net:5678'``. Default is ``('0.0.0.0', 5678)``.\n    redirect_output : bool, optional\n        Specifies whether any output (on both `stdout` and `stderr`) produced\n        by this program should be sent to the debugger. Default is ``True``.\n\n    Notes\n    -----\n    This function returns immediately after setting up the debugging server,\n    and does not block program execution. If you need to block until debugger\n    is attached, call `ptvsd.wait_for_attach`. The debugger can be detached\n    and re-attached multiple times after `enable_attach` is called.\n\n    Only the thread on which this function is called, and any threads that are\n    created after it returns, will be visible in the debugger once it is\n    attached. Any threads that are already running before this function is\n    called will not be visible.\n    \"\"\"\n    if is_attached():\n        return\n    debugger_attached.clear()\n\n    # Ensure port is int\n    port = address[1]\n    address = (address[0], port if type(port) is int else int(port))\n\n    ptvsd_enable_attach(\n        address,\n        redirect_output=redirect_output,\n    )\n\n\ndef attach(address, redirect_output=True):\n    \"\"\"Attaches this process to the debugger listening on a given address.\n\n    Parameters\n    ----------\n    address : (str, int), optional\n        Specifies the interface and port on which the debugger is listening\n        for TCP connections. It is in the same format as used for\n        regular sockets of the `socket.AF_INET` family, i.e. a tuple of\n        ``(hostname, port)``.\n    redirect_output : bool, optional\n        Specifies whether any output (on both `stdout` and `stderr`) produced\n        by this program should be sent to the debugger. Default is ``True``.\n    \"\"\"\n    if is_attached():\n        return\n    debugger_attached.clear()\n\n    # Ensure port is int\n    port = address[1]\n    address = (address[0], port if type(port) is int else int(port))\n\n    ptvsd_attach(address, redirect_output=redirect_output)\n\n# TODO: Add disable_attach()?\n\n\ndef is_attached():\n    \"\"\"Returns ``True`` if debugger is attached, ``False`` otherwise.\"\"\"\n    return debugger_attached.isSet()\n\n\ndef break_into_debugger():\n    \"\"\"If a remote debugger is attached, pauses execution of all threads,\n    and breaks into the debugger with current thread as active.\n    \"\"\"\n    if not is_attached():\n        return\n\n    # Get the first frame in the stack that's not an internal frame.\n    global_debugger = get_global_debugger()\n    stop_at_frame = sys._getframe().f_back\n    while stop_at_frame is not None and global_debugger.get_file_type(\n            get_abs_path_real_path_and_base_from_frame(stop_at_frame)) == global_debugger.PYDEV_FILE:\n        stop_at_frame = stop_at_frame.f_back\n\n    _pydevd_settrace(\n        suspend=True,\n        trace_only_current_thread=True,\n        patch_multiprocessing=False,\n        stop_at_frame=stop_at_frame,\n    )\n    stop_at_frame = None\n\n\ndef debug_this_thread():\n    pydevd.settrace(suspend=False)\n", "path": "src/ptvsd/attach_server.py"}, {"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\n__all__ = [\n    '__version__', '__author__',\n    'enable_attach', 'wait_for_attach', 'break_into_debugger', 'is_attached',\n]\n\n\n# \"force_pydevd\" must be imported first to ensure (via side effects)\n# that the ptvsd-vendored copy of pydevd gets used.\nfrom ._vendored import force_pydevd\nfrom ptvsd.version import __version__, __author__\nfrom ptvsd.attach_server import (  # noqa\n    attach,\n    break_into_debugger,\n    debug_this_thread,\n    enable_attach,\n    is_attached,\n    wait_for_attach,\n)\ndel force_pydevd\n", "path": "src/ptvsd/__init__.py"}]}