From 3908192ce72871eaa6cd6eee6d036d56e0d00b39 Mon Sep 17 00:00:00 2001 From: Pavel Minaev Date: Fri, 20 Sep 2019 12:44:55 -0700 Subject: [PATCH] Work around import deadlock in enable_attach() on Python 2 (#1788). --- tests/DEBUGGEE_PYTHONPATH/debug_me/__init__.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/DEBUGGEE_PYTHONPATH/debug_me/__init__.py b/tests/DEBUGGEE_PYTHONPATH/debug_me/__init__.py index 8bd27f47..253a0821 100644 --- a/tests/DEBUGGEE_PYTHONPATH/debug_me/__init__.py +++ b/tests/DEBUGGEE_PYTHONPATH/debug_me/__init__.py @@ -20,7 +20,9 @@ both as global variables, specifically so that it is possible to write:: __all__ = ["ptvsd", "pydevd", "session_id"] +import imp import os +import sys # For `from debug_me import ...`. import ptvsd @@ -47,8 +49,16 @@ if _code: del os.environ["PTVSD_DEBUG_ME"] _code = compile(_code, "", "exec") - eval(_code, {}) + # On Python 2, imports use a global import lock, which deadlocks enable_attach() + # when it tries to import from a background thread. This works around that. + if sys.version_info < (3,): + imp.release_lock() + try: + eval(_code, {}) + finally: + if sys.version_info < (3,): + imp.acquire_lock() # For non-blocking communication between the test and the debuggee. The debuggee # can access this as a normal dict - scratchpad["foo"] etc. The test should assign