Skip to content

Create a node-pty host process with flow control and event batching #74620

Closed

Description

@Tyriar

As of around a year ago node-pty is launched directly from the renderer process, while this does mean ctrl+c is responsive it has several downsides:

My proposal for these problems:

Activity

  1. added
    feature-request
    freeze-slow-crash-leak
    terminal
    on May 30, 2019
  2. added this to the On Deck milestone on May 30, 2019
  3. reinux commented on Jul 3, 2019

    @reinux
  4. Tyriar commented on Jul 3, 2019

    @Tyriar
    Author
  5. Tyriar commented on Jul 13, 2019

    @Tyriar
    ContributorAuthor

    Commit that moved the process into renderer: 35cee02

  6. Tyriar commented on Sep 17, 2019

    @Tyriar
    ContributorAuthor

    Commit that converted to use amd ed9cdf5

  7. Tyriar commented on Sep 18, 2019

    @Tyriar
    ContributorAuthor

    Did some exploration into this.

    How terminal processes are launched currently

                         TerminalInstance
                               |
                               v
                     TerminalProcessManager
                        |              |
                        v              v
    TerminalProcessExtHostProxy    TerminalProcess
                 |                 (renderer proc)
                 v
       (an ext host connection)
                 |
                 v
          TerminalProcess
          (ext host proc)
    

    Issues:

    • In proc node-pty can crash the renderer when conpty/winpty misbehave
    • Slow node event loop means slower data flow from the pty, I believe this is the primary reason terminals are a lot slower on Windows

    Using just the ext host

              TerminalInstance
                     |
                     v
            TerminalProcessManager
                     |
                     v
        TerminalProcessExtHostProxy
                     |
                     v
    (local or remote ext host connection)
                     |
                     v
              TerminalProcess
    

    Issues:

    • In proc node-pty can crash the ext host when conpty/winpty misbehave
    • More data from terminal can flood ext host connection, eg. after running yes command (flow control is eventual solution to this)
    • Blocker: SSH extension depends on terminals which means the local ext host would have to be up and running

    Prototype: #81106

    Moving the processes out of proc, this is how it used to be (before ext host processes)

            TerminalInstance
                   |
             (forked process)
                   v
            TerminalProcess
            (uses node-pty)
    

    Issues:

    • Slower data flow as we're passing around JS strings
    • Can flood connection (note this is not an issue locally because the nodejs event loop blocks it)

    Proposed solution

                         TerminalInstance
                               |
                               v            
                     TerminalProcessManager 
                        |              |    
                        v              v    
    TerminalProcessExtHostProxy    TerminalPty <--> TerminalInstanceService[2]
                 |                                             |
                 v                                       (forked proc[1])
       (an ext host connection)                                v
                 |                                   TerminalPtyHostProcess
                 |
                 v                     
            TerminalPty <--> ExtHostTerminalTerminalService[2]
                                           |
                                     (forked proc[1])
                                           v
                                 TerminalPtyHostProcess
    

    [1] Uses ext host socket-based message protocol
    [2] The terminal services own the host process and establish the connection

    • Crash protection for ext host and renderer, if node-pty crashes it would take down only TerminalPtyHostProcess (and all terminals it's running)
    • Local terminals are still available for SSH extension
    • Data flow
      • Faster data flow as messages passed over socket are binary data which node-pty can now emit and xterm.js can consume
      • Add flow control which pauses node-pty sockets when they are getting too ahead of xterm.js parsing of the data
      • TerminalPtyHostProcess is basically just handling data flow for all terminals, so it shouldn't block the pty as much
      • Can also batch data to reduce the amount of messages coming

    Prototype: #81105

    Note that this could be split into 2 stages:

    1. Moving into new process, communicate via ChildProcess.send and add flow control
    2. Adding new protocol and enabling fast transfer of data via ArrayBuffers

    On flow control

    We recently exposed a callback on xterm's write which fires when the data is parsed. This will make flow control much easier that the earlier proposals I talked about. For example we could provide a callback for event nth write and send messages over to TerminalPtyHostProcess to indicate where the renderer is at in parsing the data.

  8. Tyriar commented on Jun 11, 2020

    @Tyriar
    ContributorAuthor

    I did an exploration into moving node-pty into the main process (#99897) and it worked pretty well. It's just a prototype and falls over when it comes to pty lifecycle management but it seems like it would be quite easy to tweak it to restore terminals across reloads #20013 if it lived in the main process (as the prototype shows). Another benefit is it speeds up the terminal output significantly, tree running in the same directly was several times faster due to event loop being freed up, I expect this to also happen when we get microsoft/node-pty#415 in, even when node-pty is hosted in the renderer.

    My plan is to eventually move node-pty into a process that's forked from the main process and then establishing a direct socket connection for the terminal if possible to the window so we keep pretty much everything of the main process except for the setting up of the connection.

    The branch in action with node-pty hosted on the main process and (the hacky) reload persistence:

    recording (18)

  9. tmandry commented on Jul 29, 2020

    @tmandry

    Restoring the actual terminal session would be great, but restoring the shell layout with current directory and scrollback saved would give 99% of the benefits for me.

  10. Tyriar commented on Jan 5, 2021

    @Tyriar
    ContributorAuthor

    I split the flow control and event batching portion of this into #113827

  11. Tyriar commented on Feb 11, 2021

    @Tyriar
    ContributorAuthor

    Update:

    #116373 will be merged soon which will:

    • Create a ptyHost process off the shared process where node-pty is used
      • This should boost terminal performance on Windows due to conpty's small event sizes
    • Update node-pty to fix the Windows hang issue now that it's on a plain node process

    I'll close this issue when the above gets merged and defer the following to more targeted issues:

  12. added
    unreleased
    on Feb 18, 2021
  13. Tyriar commented on Feb 18, 2021

    @Tyriar
    ContributorAuthor

    This is what the process architecture ended up looking like:

    Screen Shot 2021-02-18 at 5 37 39 AM

    PtyService on the pty host process is what creates the terminal processes

  14. added
    insiders-released
    and removed
    unreleased
    on Feb 19, 2021
  15. locked and limited conversation to collaborators on Apr 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

feature-requestfreeze-slow-crash-leakinsiders-releasedon-testplanperfterminal

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions