Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Remote enable_attach RuntimeError('already started') #1835

Closed
reinismu opened this issue Oct 9, 2019 · 4 comments
Closed

Remote enable_attach RuntimeError('already started') #1835

reinismu opened this issue Oct 9, 2019 · 4 comments

Comments

@reinismu
Copy link

reinismu commented Oct 9, 2019

Hey, my team are trying to use ptvsd to debug our remote Django service. (Django version 1.8.16 and python 2.7). It is running in kubernetes pod.

Error only happens if we start service with the code. If it is added with reload after service has started then there are no issues.

Environment data

  • PTVSD version: v4.3.2 and v5.0.0a5
  • OS and version: Ubuntu 16.04.6 LTS
  • Python version (& distribution if applicable, e.g. Anaconda): 2.7
  • Using VS Code or Visual Studio: VS Code

Actual behavior

Code in manage.py

import ptvsd
ptvsd.enable_attach(address=("0.0.0.0", 4444))

Error on v4.3.2

Traceback (most recent call last):
  File "manage.py", line 13, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 49, in execute
    super(Command, self).execute(*args, **options)
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 88, in handle
    self.run(**options)
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 97, in run
    autoreload.main(self.inner_run, None, options)
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 336, in main
    reloader(wrapped_main_func, args, kwargs)
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 307, in python_reloader
    exit_code = restart_with_reloader()
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 293, in restart_with_reloader
    exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ)
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/_vendored/pydevd/_pydev_bundle/pydev_monkey.py", line 434, in new_spawnve
    return getattr(os, original_name)(mode, path, patch_args(args), env)
  File "/opt/prezi/virtualenv/lib/python2.7/os.py", line 575, in spawnve
    return _spawnvef(mode, file, args, env, execve)
  File "/opt/prezi/virtualenv/lib/python2.7/os.py", line 533, in _spawnvef
    pid = fork()
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/_vendored/pydevd/_pydev_bundle/pydev_monkey.py", line 528, in new_fork
    _on_forked_process()
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/_vendored/pydevd/_pydev_bundle/pydev_monkey.py", line 50, in _on_forked_process
    pydevd.settrace_forked()
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 2427, in settrace_forked
    patch_multiprocessing=True,
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 2179, in settrace
    wait_for_ready_to_run,
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 2230, in _locked_settrace
    debugger.connect(host, port)  # Note: connect can raise error.
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 1060, in connect
    s = start_client(host, port)
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/pydevd_hooks.py", line 136, in _start_client
    return start_client(daemon, h, p)
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/_remote.py", line 62, in <lambda>
    start_client=(lambda daemon, h, port: start_daemon()),
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/_remote.py", line 50, in start_daemon
    _, next_session = daemon.start_server(addr=(host, port))
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/daemon.py", line 158, in start_server
    with self.started():
  File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/daemon.py", line 110, in started
    self.start()
  File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/daemon.py", line 145, in start
    raise RuntimeError('already started')
RuntimeError: already started

Error when I tried with version v5.0.0a5

E+00000.016: [handling #4 event "pydevdInputRequested" from Server-1]
             IDE is not available in Session-1
             
             Traceback (most recent call last):
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 106, in report
                 raise ComponentNotAvailable(type)
             ComponentNotAvailable: IDE is not available
             
             Stack where logged:
               File "/usr/lib/python2.7/threading.py", line 774, in __bootstrap
                 self.__bootstrap_inner()
               File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
                 self.run()
               File "/usr/lib/python2.7/threading.py", line 754, in run
                 self.__target(*self.__args, **self.__kwargs)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/common/messaging.py", line 1528, in _run_handlers
                 handler()
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/common/messaging.py", line 646, in _handle
                 result = handler(self)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 79, in lock_and_handle
                 return f(self, message)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/server.py", line 82, in event
                 self.ide.propagate_after_start(event)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 101, in <lambda>
                 __getattr__ = __setattr__ = lambda self, *_: report()
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 108, in report
                 raise log.exception("{0} in {1}", exc, session)
             

E+00000.018: [handling #4 event "pydevdInputRequested" from Server-1]
             Handler 'event' (file u'/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py', line 75)
             couldn't handle #4 event "pydevdInputRequested" from Server-1:
             IDE is not available

E+00000.019: [handling #6 event "pydevdInputRequested" from Server-1]
             IDE is not available in Session-1
             
             Traceback (most recent call last):
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 106, in report
                 raise ComponentNotAvailable(type)
             ComponentNotAvailable: IDE is not available
             
             Stack where logged:
               File "/usr/lib/python2.7/threading.py", line 774, in __bootstrap
                 self.__bootstrap_inner()
               File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
                 self.run()
               File "/usr/lib/python2.7/threading.py", line 754, in run
                 self.__target(*self.__args, **self.__kwargs)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/common/messaging.py", line 1528, in _run_handlers
                 handler()
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/common/messaging.py", line 646, in _handle
                 result = handler(self)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 79, in lock_and_handle
                 return f(self, message)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/server.py", line 82, in event
                 self.ide.propagate_after_start(event)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 101, in <lambda>
                 __getattr__ = __setattr__ = lambda self, *_: report()
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 108, in report
                 raise log.exception("{0} in {1}", exc, session)
             

E+00000.020: [handling #6 event "pydevdInputRequested" from Server-1]
             Handler 'event' (file u'/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py', line 75)
             couldn't handle #6 event "pydevdInputRequested" from Server-1:
             IDE is not available

E+00000.020: [handling #8 event "pydevdInputRequested" from Server-1]
             IDE is not available in Session-1
             
             Traceback (most recent call last):
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 106, in report
                 raise ComponentNotAvailable(type)
             ComponentNotAvailable: IDE is not available
             
             Stack where logged:
               File "/usr/lib/python2.7/threading.py", line 774, in __bootstrap
                 self.__bootstrap_inner()
               File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
                 self.run()
               File "/usr/lib/python2.7/threading.py", line 754, in run
                 self.__target(*self.__args, **self.__kwargs)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/common/messaging.py", line 1528, in _run_handlers
                 handler()
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/common/messaging.py", line 646, in _handle
                 result = handler(self)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 79, in lock_and_handle
                 return f(self, message)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/server.py", line 82, in event
                 self.ide.propagate_after_start(event)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 101, in <lambda>
                 __getattr__ = __setattr__ = lambda self, *_: report()
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 108, in report
                 raise log.exception("{0} in {1}", exc, session)
             

E+00000.021: [handling #8 event "pydevdInputRequested" from Server-1]
             Handler 'event' (file u'/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py', line 75)
             couldn't handle #8 event "pydevdInputRequested" from Server-1:
             IDE is not available

E+00000.021: [handling #10 event "pydevdInputRequested" from Server-1]
             IDE is not available in Session-1
             
             Traceback (most recent call last):
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 106, in report
                 raise ComponentNotAvailable(type)
             ComponentNotAvailable: IDE is not available
             
             Stack where logged:
               File "/usr/lib/python2.7/threading.py", line 774, in __bootstrap
                 self.__bootstrap_inner()
               File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
                 self.run()
               File "/usr/lib/python2.7/threading.py", line 754, in run
                 self.__target(*self.__args, **self.__kwargs)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/common/messaging.py", line 1528, in _run_handlers
                 handler()
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/common/messaging.py", line 646, in _handle
                 result = handler(self)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 79, in lock_and_handle
                 return f(self, message)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/server.py", line 82, in event
                 self.ide.propagate_after_start(event)
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 101, in <lambda>
                 __getattr__ = __setattr__ = lambda self, *_: report()
               File "/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py", line 108, in report
                 raise log.exception("{0} in {1}", exc, session)
             

E+00000.023: [handling #10 event "pydevdInputRequested" from Server-1]
             Handler 'event' (file u'/opt/prezi/virtualenv/local/lib/python2.7/site-packages/ptvsd/adapter/../../ptvsd/adapter/components.py', line 75)
             couldn't handle #10 event "pydevdInputRequested" from Server-1:
             IDE is not available

Expected behavior

Start a remote debug session

@karthiknadig
Copy link
Member

@reinismu We are addressing this issue generically. The error you see is because of os.fork that occurs when using reload feature in Django. We have a workaround but that only works for python 3.*, for 2.7 unfortunately we don't have a workaround.

See this #943 for more info. That issue is our current highest priority issue. It should address this.

Note:
I recommend using the command-line to start the debugger than using enable_attach. That way your production does not need to include ptvsd in it. Also, in scenarios like fork, multiple processes will try to open the port (in your case 4444), which will fail. When using CLI, for the forked processes we will automatically do this for you.

@reinismu
Copy link
Author

Thanks, we will try to move to command-line and see how it goes :)

@int19h
Copy link
Contributor

int19h commented Oct 15, 2019

For ptvsd 5, it's not clear to me where those "pydevdInputRequested" events are coming from. This is supposed to be something that pydevd sends when somebody tries to do sys.stdin.read(), or anything that wraps that, like input(). But there's nothing like that in the code.

@int19h
Copy link
Contributor

int19h commented Oct 31, 2019

The original issue should be resolved by #1878 (note that this is in master, but not in any released ptvsd version yet).

@int19h int19h closed this as completed Oct 31, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants