def start_client(self, addr):
        """Return ("socket", start_session) with a new client socket."""

        ptvsd.log.debug('Starting client daemon on {0!r}.', addr)

        addr = Address.from_raw(addr)
        self._singlesession = True
        with self.started():
            assert self.session is None
            client = create_client()
            connect(client, addr)
        sock = self._sock

        def start_session(**kwargs):
            self._check_ready_for_session()
            if self._server is not None:
                raise RuntimeError('running as server')
            if self._numsessions:
                raise RuntimeError('session stopped')

            try:
                self._bind_session(client)
                self._start_session_safely('ptvsd.Client', **kwargs)
                return self._session
            except Exception:
                self._stop_quietly()
                raise

        return sock, start_session
예제 #2
0
def start_client(daemon, host, port):
    """Return a socket to an existing "remote" pydevd-handling daemon.

    The daemon supports the pydevd client wire protocol, sending
    requests and handling responses (and events).

    This is a replacement for _pydevd_bundle.pydevd_comm.start_client.
    """
    client = create_client()
    client.connect((host, port))

    pydevd = daemon.start()
    daemon.set_connection(client)
    return pydevd
예제 #3
0
 def connect(addr, wait=None, closeonly=False):
     sock = create_client()
     try:
         sock.settimeout(1)
         sock.connect(addr)
         debug('>connected')
         if wait is not None:
             debug('>waiting')
             time.sleep(wait)
     finally:
         debug('>closing')
         if closeonly:
             sock.close()
         else:
             close_socket(sock)
예제 #4
0
def notify_root(port):
    assert options.subprocess_of

    debug('Subprocess %d notifying root process at port %d' %
          (os.getpid(), options.subprocess_notify))
    conn = create_client()
    conn.connect(('localhost', options.subprocess_notify))
    stream = JsonIOStream.from_socket(conn)
    channel = JsonMessageChannel(stream)
    channel.start()

    # Send the notification about ourselves to root, and wait for it to tell us
    # whether an incoming connection is anticipated. This will be true if root
    # had successfully propagated the notification to the IDE, and false if it
    # couldn't do so (e.g. because the IDE is not attached). There's also the
    # possibility that connection to root will just drop, e.g. if it crashes -
    # in that case, just exit immediately.

    request = channel.send_request(
        'ptvsd_subprocess', {
            'parentProcessId': options.subprocess_of,
            'processId': os.getpid(),
            'port': port,
        })

    try:
        response = request.wait_for_response()
    except Exception:
        print('Failed to send subprocess notification; exiting',
              file=sys.__stderr__)
        traceback.print_exc()
        sys.exit(0)

    # Keep the channel open until we exit - root process uses open channels to keep
    # track of which subprocesses are alive and which are not.
    atexit.register(lambda: channel.close())

    if not response['incomingConnection']:
        debugger = get_global_debugger()
        while debugger is None:
            time.sleep(0.1)
            debugger = get_global_debugger()
        debugger.ready_to_run = True
예제 #5
0
def notify_root(port):
    assert options.subprocess_of

    debug('Subprocess %d notifying root process at port %d' % (os.getpid(), options.subprocess_notify))
    conn = create_client()
    conn.connect(('localhost', options.subprocess_notify))
    stream = JsonIOStream.from_socket(conn)
    channel = JsonMessageChannel(stream)
    channel.start()

    # Send the notification about ourselves to root, and wait for it to tell us
    # whether an incoming connection is anticipated. This will be true if root
    # had successfully propagated the notification to the IDE, and false if it
    # couldn't do so (e.g. because the IDE is not attached). There's also the
    # possibility that connection to root will just drop, e.g. if it crashes -
    # in that case, just exit immediately.

    request = channel.send_request('ptvsd_subprocess', {
        'parentProcessId': options.subprocess_of,
        'processId': os.getpid(),
        'port': port,
    })

    try:
        response = request.wait_for_response()
    except Exception:
        print('Failed to send subprocess notification; exiting', file=sys.__stderr__)
        traceback.print_exc()
        sys.exit(0)

    # Keep the channel open until we exit - root process uses open channels to keep
    # track of which subprocesses are alive and which are not.
    atexit.register(lambda: channel.close())

    if not response['incomingConnection']:
        debugger = get_global_debugger()
        while debugger is None:
            time.sleep(0.1)
            debugger = get_global_debugger()
        debugger.ready_to_run = True
예제 #6
0
def create_client():
    """Return a new (unconnected) client socket."""
    return _ptvsd.create_client()