Repository: microsoft/ptvsd

Issue:
Flask fails to start on macOS with AttributeError('_ep_map')
## Environment data

- PTVSD version: master
- OS and version: macOS
- Python version (& distribution if applicable, e.g. Anaconda): 3.7
- Using VS Code or Visual Studio: either

## Actual behavior

Flask server fails to start with Attribute error (see logs below).

## Expected  behavior

Should start without errors.

## Steps to reproduce:
1. Run the following test to repro:
`-m tests -v tests.system_tests.test_web_frameworks.FlaskLaunchFileTests.test_with_handled_exceptions`

### Logs
```
Event(seq=0, event='output', body={'category': 'telemetry', 'output': 'ptvsd', 'data': {'version': '4.1.1a10+23.gd7f2936'}})
Response(seq=1, request_seq=1000, command='initialize', success=True, message='', body={'supportsConditionalBreakpoints': True, 'supportsConfigurationDoneRequest': True, 'supportsDebuggerProperties': True, 'supportsEvaluateForHovers': True, 'supportsExceptionInfoRequest': True, 'supportsExceptionOptions': True, 'supportsHitConditionalBreakpoints': True, 'supportsLogPoints': True, 'supportsModulesRequest': True, 'supportsSetExpression': True, 'supportsSetVariable': True, 'supportsValueFormattingOptions': True, 'supportTerminateDebuggee': True, 'exceptionBreakpointFilters': [{'filter': 'raised', 'label': 'Raised Exceptions', 'default': False}, {'filter': 'uncaught', 'label': 'Uncaught Exceptions', 'default': True}]})
Event(seq=2, event='initialized', body={})
Response(seq=3, request_seq=1001, command='launch', success=True, message='', body={})
Response(seq=4, request_seq=1002, command='setExceptionBreakpoints', success=True, message='', body={})
Response(seq=5, request_seq=1003, command='configurationDone', success=True, message='', body={})
Event(seq=6, event='process', body={'name': 'flask:', 'systemProcessId': 1223, 'isLocalProcess': True, 'startMethod': 'launch'})
Event(seq=7, event='thread', body={'reason': 'started', 'threadId': 1})
Event(seq=8, event='stopped', body={'reason': 'exception', 'threadId': 1, 'text': 'AttributeError', 'description': "AttributeError('_ep_map')"})
```


Discussion:
Seems to be an issue with justmy code.
We have errors in non-user code that are being captured.
I see. I think I found the issue, I have a fix in my private branch. Will do a PR in the morning.

Resolving patch:
diff --git a/ptvsd/wrapper.py b/ptvsd/wrapper.py
--- a/ptvsd/wrapper.py
+++ b/ptvsd/wrapper.py
@@ -1397,13 +1397,13 @@ def _apply_code_stepping_settings(self):
             vendored_pydevd = os.path.sep + \
                               os.path.join('ptvsd', '_vendored', 'pydevd')
             ptvsd_path = os.path.sep + 'ptvsd'
+            site_path = os.path.sep + 'site-packages'
 
             project_dirs = []
             for path in sys.path + [os.getcwd()]:
                 is_stdlib = False
                 norm_path = os.path.normcase(path)
-                if path.endswith(ptvsd_path) or \
-                   path.endswith(vendored_pydevd):
+                if path.endswith((ptvsd_path, vendored_pydevd, site_path)):
                     is_stdlib = True
                 else:
                     for prefix in STDLIB_PATH_PREFIXES:
@@ -2301,7 +2301,8 @@ def on_pydevd_thread_suspend(self, seq, args):
         xframe = xframes[0]
         filepath = unquote(xframe['file'])
         if reason in STEP_REASONS or reason in EXCEPTION_REASONS:
-            if not self._should_debug(filepath):
+            if self.internals_filter.is_internal_path(filepath) or \
+                not self._should_debug(filepath):
                 self.pydevd_notify(pydevd_comm.CMD_THREAD_RUN, pyd_tid)
                 return
 


Test patch:
diff --git a/tests/highlevel/__init__.py b/tests/highlevel/__init__.py
--- a/tests/highlevel/__init__.py
+++ b/tests/highlevel/__init__.py
@@ -908,7 +908,7 @@ def assert_vsc_failure(self, received, expected, req):
         expected = list(self.vsc.protocol.parse_each(expected))
         self.assertEqual(received[:-1], expected)
 
-        failure = received[-1]
+        failure = received[-1] if len(received) > 0 else []
         expected = self.vsc.protocol.parse(
             self.fix.vsc_msgs.new_failure(req, failure.message))
         self.assertEqual(failure, expected)
diff --git a/tests/highlevel/test_lifecycle.py b/tests/highlevel/test_lifecycle.py
--- a/tests/highlevel/test_lifecycle.py
+++ b/tests/highlevel/test_lifecycle.py
@@ -30,13 +30,13 @@ def _get_project_dirs():
     vendored_pydevd = os.path.sep + \
                       os.path.join('ptvsd', '_vendored', 'pydevd')
     ptvsd_path = os.path.sep + 'ptvsd'
+    site_path = os.path.sep + 'site-packages'
 
     project_dirs = []
     for path in sys.path + [os.getcwd()]:
         is_stdlib = False
         norm_path = os.path.normcase(path)
-        if path.endswith(ptvsd_path) or \
-            path.endswith(vendored_pydevd):
+        if path.endswith((ptvsd_path, vendored_pydevd, site_path)):
             is_stdlib = True
         else:
             for prefix in ptvsd.wrapper.STDLIB_PATH_PREFIXES:
diff --git a/tests/system_tests/test_web_frameworks.py b/tests/system_tests/test_web_frameworks.py
--- a/tests/system_tests/test_web_frameworks.py
+++ b/tests/system_tests/test_web_frameworks.py
@@ -378,7 +378,7 @@ def test_with_route_break_points(self):
                     'FLASK_ENV': 'development',
                     'FLASK_DEBUG': '0',
                     'LC_ALL': 'C.UTF-8',
-                    'LANG': 'C.UTF-8'
+                    'LANG': 'C.UTF-8',
                 },
                 cwd=cwd),
             framework='Jinja',
@@ -399,7 +399,7 @@ def test_with_template_break_points(self):
                     'FLASK_ENV': 'development',
                     'FLASK_DEBUG': '0',
                     'LC_ALL': 'C.UTF-8',
-                    'LANG': 'C.UTF-8'
+                    'LANG': 'C.UTF-8',
                 },
                 cwd=cwd),
             framework='Jinja',
@@ -418,7 +418,7 @@ def test_with_handled_exceptions(self):
                     'FLASK_ENV': 'development',
                     'FLASK_DEBUG': '0',
                     'LC_ALL': 'C.UTF-8',
-                    'LANG': 'C.UTF-8'
+                    'LANG': 'C.UTF-8',
                 },
                 cwd=cwd), 'Jinja', filename)
 
@@ -434,7 +434,7 @@ def test_with_unhandled_exceptions(self):
                     'FLASK_ENV': 'development',
                     'FLASK_DEBUG': '0',
                     'LC_ALL': 'C.UTF-8',
-                    'LANG': 'C.UTF-8'
+                    'LANG': 'C.UTF-8',
                 },
                 cwd=cwd), 'Jinja', filename)