Skip to content

Commit

Permalink
Expose proper PID on windows and clean up process handle
Browse files Browse the repository at this point in the history
Fixes chjj#45
  • Loading branch information
Tyriar committed Mar 13, 2017
1 parent 05b02dc commit c6f65ab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/win/pty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ static NAN_METHOD(PtyStartProcess) {
// Pty object values.
Local<Object> marshal = Nan::New<Object>();

marshal->Set(Nan::New<String>("innerPid").ToLocalChecked(), Nan::New<Number>((int)GetProcessId(handle)));
marshal->Set(Nan::New<String>("innerPidHandle").ToLocalChecked(), Nan::New<Number>((int)handle));
marshal->Set(Nan::New<String>("pid").ToLocalChecked(), Nan::New<Number>((int)winpty_agent_process(pc)));
marshal->Set(Nan::New<String>("pty").ToLocalChecked(), Nan::New<Number>(InterlockedIncrement(&ptyCounter)));
marshal->Set(Nan::New<String>("fd").ToLocalChecked(), Nan::New<Number>(-1));
Expand Down Expand Up @@ -311,18 +313,21 @@ static NAN_METHOD(PtyResize) {
static NAN_METHOD(PtyKill) {
Nan::HandleScope scope;

if (info.Length() != 1
|| !info[0]->IsNumber()) // pid
if (info.Length() != 2
|| !info[0]->IsNumber() // pid
|| !info[1]->IsNumber()) // innerPidHandle
{
return Nan::ThrowError("Usage: pty.kill(pid)");
}

int handle = info[0]->Int32Value();
HANDLE innerPidHandle = (HANDLE)info[0]->Int32Value();

winpty_t *pc = get_pipe_handle(handle);

assert(pc != nullptr);
assert(remove_pipe_handle(handle));
CloseHandle(innerPidHandle);

return info.GetReturnValue().SetUndefined();
}
Expand Down
7 changes: 6 additions & 1 deletion src/windowsPtyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export class WindowsPtyAgent {
private _inSocket: net.Socket;
private _outSocket: net.Socket;
private _pid: number;
private _innerPid: number;
private _innerPidHandle: number;

private _fd: any;
private _pty: number;

Expand Down Expand Up @@ -50,6 +53,8 @@ export class WindowsPtyAgent {

// Terminal pid.
this._pid = term.pid;
this._innerPid = term.innerPid;
this._innerPidHandle = term.innerPidHandle;

// Not available on windows.
this._fd = term.fd;
Expand Down Expand Up @@ -83,7 +88,7 @@ export class WindowsPtyAgent {
this._inSocket.writable = false;
this._outSocket.readable = false;
this._outSocket.writable = false;
pty.kill(this.pid);
pty.kill(this.pid, this._innerPidHandle);
}
}

Expand Down

0 comments on commit c6f65ab

Please sign in to comment.