Repository: microsoft/ptvsd

Issue:
pydevd errors (NoneType not attribute add_command; PyDB no attribute _py_db_command_thread_event)
## Environment data

- PTVSD version: 4.3.1
- OS and version: Linux/4.19.66-1-MANJARO manjaro/18.0.4
- Python version (& distribution if applicable, e.g. Anaconda): 3.7.4
- Using VS Code or Visual Studio: None

## Actual behavior
```
File "<stdin>", line 1, in <module>
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/attach_server.py", line 101, in enable_attach
    ptvsd_enable_attach(address)
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_remote.py", line 79, in enable_attach
    patch_multiprocessing=ptvsd.options.multiprocess)
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 2174, in settrace
    wait_for_ready_to_run,
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 2223, in _locked_settrace
    debugger = PyDB()
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 395, in __init__
    self._files_filtering = FilesFiltering()
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/_pydevd_bundle/pydevd_filtering.py", line 125, in __init__
    library_roots = self._get_default_library_roots()
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/_pydevd_bundle/pydevd_filtering.py", line 161, in _get_default_library_roots
    roots.append(sysconfig.get_path(path_name))
  File "/home/nuno/miniconda3/lib/python3.7/sysconfig.py", line 521, in get_path
    return get_paths(scheme, vars, expand)[name]
  File "/home/nuno/miniconda3/lib/python3.7/sysconfig.py", line 511, in get_paths
    return _expand_vars(scheme, vars)
  File "/home/nuno/miniconda3/lib/python3.7/sysconfig.py", line 172, in _expand_vars
    _extend_dict(vars, get_config_vars())
  File "/home/nuno/miniconda3/lib/python3.7/sysconfig.py", line 559, in get_config_vars
    _init_posix(_CONFIG_VARS)
  File "/home/nuno/miniconda3/lib/python3.7/sysconfig.py", line 430, in _init_posix
    _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
  File "/home/nuno/miniconda3/lib/python3.7/_sysconfigdata_m_linux_x86_64-linux-gnu.py", line 700, in <module>
    GCC_VERSION = check_output([GCC, '--version'], stderr=err).decode('utf-8').splitlines()[0].split(' ')[-1]
  File "/home/nuno/miniconda3/lib/python3.7/subprocess.py", line 395, in check_output
    **kwargs).stdout
  File "/home/nuno/miniconda3/lib/python3.7/subprocess.py", line 472, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/home/nuno/miniconda3/lib/python3.7/subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "/home/nuno/miniconda3/lib/python3.7/subprocess.py", line 1453, in _execute_child
    restore_signals, start_new_session, preexec_fn)
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/_pydev_bundle/pydev_monkey.py", line 447, in new_fork_exec
    send_process_created_message()
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/_pydev_bundle/pydev_monkey.py", line 540, in send_process_created_message
    debugger.send_process_created_message()
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 1519, in send_process_created_message
    self.writer.add_command(cmd)
AttributeError: 'NoneType' object has no attribute 'add_command'
```
```
File "<stdin>", line 1, in <module>
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/attach_server.py", line 101, in enable_attach
    ptvsd_enable_attach(address)
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_remote.py", line 79, in enable_attach
    patch_multiprocessing=ptvsd.options.multiprocess)
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 2174, in settrace
    wait_for_ready_to_run,
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 2253, in _locked_settrace
    debugger.start_auxiliary_daemon_threads()
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 1783, in start_auxiliary_daemon_threads
    self._create_pydb_command_thread()
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 1771, in _create_pydb_command_thread
    new_pydb_command_thread = self.py_db_command_thread = PyDBCommandThread(self)
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 152, in __init__
    self._py_db_command_thread_event = py_db._py_db_command_thread_event
AttributeError: 'PyDB' object has no attribute '_py_db_command_thread_event'
```
```
File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 672, in get_file_type
    return _cache_file_type[cache_key]
KeyError: (2117, '/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py', <code object settrace at 0x7f20b6e431e0, file "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 2117>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/attach_server.py", line 101, in enable_attach
    ptvsd_enable_attach(address)
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_remote.py", line 79, in enable_attach
    patch_multiprocessing=ptvsd.options.multiprocess)
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 2174, in settrace
    wait_for_ready_to_run,
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 2279, in _locked_settrace
    debugger.set_trace_for_frame_and_parents(get_frame().f_back)
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 1748, in set_trace_for_frame_and_parents
    file_type = self.get_file_type(frame)
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 700, in get_file_type
    file_type = self._internal_get_file_type(abs_real_path_and_basename)
  File "/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 617, in _internal_get_file_type
    return self._dont_trace_get_file_type(basename)
AttributeError: 'PyDB' object has no attribute '_dont_trace_get_file_type'
```
## Expected  behavior
no errors.

## Steps to reproduce:
(bash)
1. conda create -n test_ptvsd
2. conda install -n test_ptvsd pip
3. pip install ptvsd
4. python

(python)
1. import ptvsd
2. ptvsd.enable_attach(address=('localhost', 0))
3. ptvsd.enable_attach(address=('localhost', 0)) (again same line, different second error)
4. ptvsd.enable_attach(address=('localhost', 0)) (again same line, different third error)



Discussion:
Can you give the full tracebacks?

All errors seem to imply that `PyDB` is not properly initialized before being used... 

The first error appears to be because a new process is created (but without the full traceback I can't say whether this is new process from a fork or a new process through subprocess or exec).


(edited first post.)

Further details: doesn't occur with Python 3.7.3
I see the error... ouch, it seems that during the initialization of the debugger a process is spawned (for `gcc`)  when getting the paths from `sysconfig` and at that time the debugger is not properly initialized to be used when it tries to notify that that process was created. This prevents the proper initialization of the debugger (so, the other errors are just side-effects of the first error).

I think you can work around it locally by editing:

`/home/nuno/miniconda3/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py`

then, in the first line in `send_process_created_message` (close to line 1519), change the function by adding the lines below as the first thing in the function.

```
if self.writer is None:
    return
```

Please let me know if that works...

(it seems this is something that changed on Python itself).
Thanks a lot. I reverted to Python 3.7.3 and solves the problem, which supports your theory.

Tried your fix and solved the problem.

What can we do to solve this issue?


I'll create a patch for this shortly...

Resolving patch:
diff --git a/src/ptvsd/_vendored/pydevd/pydevd.py b/src/ptvsd/_vendored/pydevd/pydevd.py
--- a/src/ptvsd/_vendored/pydevd/pydevd.py
+++ b/src/ptvsd/_vendored/pydevd/pydevd.py
@@ -379,7 +379,6 @@ class PyDB(object):
 
     def __init__(self, set_as_global=True):
         if set_as_global:
-            set_global_debugger(self)
             pydevd_tracing.replace_sys_set_trace_func()
 
         self.reader = None
@@ -528,6 +527,10 @@ def new_trace_dispatch(frame, event, arg):
         self._apply_filter_cache = {}
         self._ignore_system_exit_codes = set()
 
+        if set_as_global:
+            # Set as the global instance only after it's initialized.
+            set_global_debugger(self)
+
     def on_configuration_done(self):
         '''
         Note: only called when using the DAP (Debug Adapter Protocol).
@@ -1515,6 +1518,8 @@ def send_caught_exception_stack_proceeded(self, thread):
     def send_process_created_message(self):
         """Sends a message that a new process has been created.
         """
+        if self.writer is None or self.cmd_factory is None:
+            return
         cmd = self.cmd_factory.make_process_created_message()
         self.writer.add_command(cmd)
 


Test patch: