Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly thread the handles received from the ConPTY #375

Closed
Tyriar opened this issue Jan 2, 2020 · 11 comments · Fixed by #415
Closed

Properly thread the handles received from the ConPTY #375

Tyriar opened this issue Jan 2, 2020 · 11 comments · Fixed by #415
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug help wanted Issues identified as good community contribution opportunities
Milestone

Comments

@Tyriar
Copy link
Member

Tyriar commented Jan 2, 2020

ConPTY can hang because we're managing it in the main thread.

microsoft/terminal#1810 (comment)

https://github.com/microsoft/node-pty/blob/master/src/win/conpty.cc

@JustinGrote
Copy link

Remediation for this is noted in microsoft/terminal#1810 (comment)

@jerch
Copy link
Collaborator

jerch commented Apr 2, 2020

@Tyriar Maybe it is enough to just move the closing of the handle in another thread and delete all references from the mainthread. This would avoid priority issues around the libuv IO handling, that might occur, if the whole conpty handling (spawning --> chunk reading -> closing) has to live in its own thread. Not to forget buffer backpressure/flow control/sync, that needs to be set up up to xterm.js, if the conpty consumer runs "unleashed".

@joshfria
Copy link

Is there any movement on this issue? It causes hangs for many vscode users.

@JustinGrote
Copy link

It's also the highest upvoted bug issue in this repository, to give an idea of the scope of users being affected by hard lock crashes.

@JustinGrote
Copy link

JustinGrote commented Jun 3, 2020

My VSCode still hard locks about 5 times a day due to this issue, to the point I had to make this function:
image

It is the most impacting issue to my VSCode quality of life for several months now.

@Tyriar
Copy link
Member Author

Tyriar commented Jun 9, 2020

Managed to put together a note-pty snippet which repros the crash reliably:

var os = require('os');
var pty = require('../..');

var isWindows = os.platform() === 'win32';
var shell = isWindows ? 'cmd.exe' : 'bash';

let i = 0;

setInterval(() => {
  console.log(`creating pty ${++i}`);
  var ptyProcess = pty.spawn(shell, [], {
    name: 'xterm-256color',
    cols: 80,
    rows: 26,
    cwd: isWindows ? process.env.USERPROFILE : process.env.HOME,
    env: Object.assign({ TEST: "Environment vars work" }, process.env),
    useConpty: true
  });

  ptyProcess.onData(data => console.log(`  data: ${data.replace(/\x1b|\n|\r/g, '_')}`));

  setInterval(() => {
    ptyProcess.write('echo foo\r'.repeat(50));
  }, 10);
  setTimeout(() => {
    console.log(`  killing ${ptyProcess.pid}...`);
    ptyProcess.kill();
    console.log(`  killing ${ptyProcess.pid} after`);
  }, 100);
}, 1200);

ie. close the pty when there is lots of writing happening.

@Tyriar Tyriar added this to the 0.10.0 milestone Jun 9, 2020
@Tyriar Tyriar self-assigned this Jun 9, 2020
Tyriar added a commit that referenced this issue Jun 9, 2020
Conpty can send a final screen update after ClosePseudoConsole which waits for the socket
to be drained before continuing. When both the socket draining and closing occurs on the
same thread a deadlock can occur which locks up VS Code's UI, the only ways to fix are
to close the client or kill the misbehaving conhost instance. This change moves the
handling of the conout named pipe to a Worker which bumps the minimum Node version
requirement to 12 (10 with a flag). This change may also have positive performance
benefits as called out in microsoft/vscode#74620

Fixes #375
Tyriar added a commit that referenced this issue Jun 9, 2020
Conpty can send a final screen update after ClosePseudoConsole which waits for the socket
to be drained before continuing. When both the socket draining and closing occurs on the
same thread a deadlock can occur which locks up VS Code's UI, the only ways to fix are
to close the client or kill the misbehaving conhost instance. This change moves the
handling of the conout named pipe to a Worker which bumps the minimum Node version
requirement to 12 (10 with a flag). This change may also have positive performance
benefits as called out in microsoft/vscode#74620

The worker change also happens for winpty, it seems to work there too.

Fixes #375
@DzmitryFil
Copy link

can reliably hang vscode each time i use lldb debugger with rust and close terminal afterwards with trash icon. This wasn't an issue for me a month ago, now it's unusable

@Tyriar
Copy link
Member Author

Tyriar commented Jul 19, 2020

@DzmitryFil you can set "terminal.integrated.windowsEnableConpty": false to go back to the Windows <= 1809 system.

@Eugeny
Copy link
Contributor

Eugeny commented Feb 6, 2021

I've been preventing hangs while closing PTY handles with this patch in Terminus for a while now:

diff --git a/src/win/conpty.cc b/src/win/conpty.cc
index 4500f6e..2159d8b 100644
--- a/src/win/conpty.cc
+++ b/src/win/conpty.cc
@@ -411,6 +411,11 @@ static NAN_METHOD(PtyResize) {
   return info.GetReturnValue().SetUndefined();
 }
 
+DWORD WINAPI Closer(LPVOID lpParam) {
+    CloseHandle((HANDLE)lpParam);
+    return 0;
+}
+
 static NAN_METHOD(PtyKill) {
   Nan::HandleScope scope;
 
@@ -435,7 +440,7 @@ static NAN_METHOD(PtyKill) {
     }
   }
 
-  CloseHandle(handle->hShell);
+  CreateThread(NULL, 0, Closer, (LPVOID)handle->hShell, 0, NULL);
 
   return info.GetReturnValue().SetUndefined();
 }

Not sure if there any implications in these threads just hanging around if the close hangs forever.

@JustinGrote
Copy link

@Tyriar yay! Any idea when this will work its way upstream to code?

@Tyriar
Copy link
Member Author

Tyriar commented Feb 10, 2021

@Eugeny I'd expect conpty to clean up the threads, is there something to worry about here if so?

@JustinGrote will probably land in tomorrow's insiders, already tested and it seems to work well. PR: microsoft/vscode#116185

@Tyriar Tyriar modified the milestones: 0.11.0, 1.0.0 Feb 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug help wanted Issues identified as good community contribution opportunities
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants