Skip to content

Add support for .on('exit', ...) returning process exit status - #110

Merged
chjj merged 2 commits into
chjj:masterfrom
joshwilsdon:exitStatus
Jul 30, 2015
Merged

chjj merged 2 commits into
chjj:masterfrom
joshwilsdon:exitStatus

Conversation

@joshwilsdon

Copy link
Copy Markdown
Contributor

This should resolve #28 and adds the ability to get the exit code and signal (if killed by signal) when a pty spawned process exits.

@psaia

psaia commented Apr 9, 2015

Copy link
Copy Markdown

Any updates on this? Would be awesome to have merged.

@javruben

Copy link
Copy Markdown

@chjj We need the exit code that this PR implements to make the Cloud9 installer function properly. Would you have time to take a look? Thanks!

@tobiastom

Copy link
Copy Markdown

I would be interested into this as well.

@amshali

amshali commented May 6, 2015

Copy link
Copy Markdown

Is there anyway we can get some traction on this? I would be willing to fix this with some guidance on what is the best way to do it. Looking at the issues, there are a couple of ways to go about it. @chjj any thought?

@chjj

chjj commented Jul 30, 2015

Copy link
Copy Markdown
Owner

Sorry, this one flew totally under the radar for me. This handles SIGCHLD via libuv functions? Am I seeing that right? That's exactly what we needed if that's the case. Calling signal(SIGCHLD, callback) ourselves caused some dissonance between pty.cc and libuv.

@chjj

chjj commented Jul 30, 2015

Copy link
Copy Markdown
Owner

Merging this for now. If this handles SIGCHLD the libuv way and fixes the zombie process issue, I'm all for it. This was the problem I had: binding to SIGCHLD the traditional way caused issues with libuv.

chjj added a commit that referenced this pull request Jul 30, 2015
Add support for .on('exit', ...) returning process exit status
@chjj
chjj merged commit b15642e into chjj:master Jul 30, 2015
chjj added a commit that referenced this pull request Jul 30, 2015
@chjj

chjj commented Jul 30, 2015

Copy link
Copy Markdown
Owner

I'm getting a segfault on pty exit. node v0.12.7

@chjj

chjj commented Jul 30, 2015

Copy link
Copy Markdown
Owner

That last commit seemed to fix it.

@chjj

chjj commented Jul 30, 2015

Copy link
Copy Markdown
Owner

Sorry, the commit above this comment ^

@chjj

chjj commented Jul 30, 2015

Copy link
Copy Markdown
Owner

The next step is to figure out how compatible this is with previous node/libuv versions.

@amshali

amshali commented Jul 30, 2015

Copy link
Copy Markdown

Is it possible to issue a release which includes this? Thank you.

@chjj

chjj commented Jul 31, 2015

Copy link
Copy Markdown
Owner

@amshali, yes, but I want to test it on multiple OSes and node versions first. Give it a day or two.

@chjj

chjj commented Jul 31, 2015

Copy link
Copy Markdown
Owner

Also, feel free to test on whatever platform you're using and let us know.

@chjj

chjj commented Jul 31, 2015

Copy link
Copy Markdown
Owner

So far works on node v0.12.x on Arch Linux and OSX.

@chjj

chjj commented Jul 31, 2015

Copy link
Copy Markdown
Owner

Getting some bizarre side effects on v0.12.5 (Linux), but v0.12.7 seems fine.

@chjj

chjj commented Jul 31, 2015

Copy link
Copy Markdown
Owner

I take it back. v0.12.5 seems fine now.

@chjj

chjj commented Jul 31, 2015

Copy link
Copy Markdown
Owner

Build error on v0.10.7 (Linux):

make[2]: Entering directory '/home/user/work/node_modules/pty.js/build'
  CXX(target) Release/obj.target/pty/src/unix/pty.o
../src/unix/pty.cc: In function ‘v8::Handle<v8::Value> PtyFork(const v8::Arguments&)’:
../src/unix/pty.cc:282:74: error: invalid conversion from ‘void (*)(uv_async_t*) {aka void (*)(uv_async_s*)}’ to ‘uv_async_cb {aka void (*)(uv_async_s*, int)}’ [-fpermissive]
         uv_async_init(uv_default_loop(), &baton->async, after_wait_on_pid);
                                                                          ^
In file included from ../node_modules/nan/nan.h:23:0,
                 from ../src/unix/pty.cc:16:
/home/user/.node-gyp/0.10.17/deps/uv/include/uv.h:1186:15: note:   initializing argument 3 of ‘int uv_async_init(uv_loop_t*, uv_async_t*, uv_async_cb)’
 UV_EXTERN int uv_async_init(uv_loop_t*, uv_async_t* async,
               ^
pty.target.mk:83: recipe for target 'Release/obj.target/pty/src/unix/pty.o' failed
make[2]: *** [Release/obj.target/pty/src/unix/pty.o] Error 1
make[2]: Leaving directory '/home/user/work/node_modules/pty.js/build'

Looks like the uv api changed a bit between versions. We can account for that I suppose.

chjj added a commit that referenced this pull request Jul 31, 2015
@chjj

chjj commented Jul 31, 2015

Copy link
Copy Markdown
Owner

Added a version check to fix the arguments for past versions of uv_async_init.

chjj added a commit that referenced this pull request Jul 31, 2015
@chjj

chjj commented Jul 31, 2015

Copy link
Copy Markdown
Owner

Fixed node v0.10.7. Node v0.8.21 also works.

@chjj

chjj commented Jul 31, 2015

Copy link
Copy Markdown
Owner

pty.js v0.2.9 published, complete with exit codes and no zombies.

@amshali

amshali commented Jul 31, 2015

Copy link
Copy Markdown

Thanks a bunch!!! :)

@tobiastom

Copy link
Copy Markdown

Thank you. Can't wait to test it.

Would you mind posting a minimal test case, then I could test it on FreeBSD and OS X El Captain.

Am 31.07.2015 um 07:01 schrieb Christopher Jeffrey (JJ) notifications@github.com:

pty.js v0.2.9 published, complete with exit codes and no zombies.

—
Reply to this email directly or view it on GitHub.

@chjj

chjj commented Jul 31, 2015

Copy link
Copy Markdown
Owner

@tobiastom, here's a simple test for now that you can copy and paste to see the exit status:

var pty = require('pty.js');

var term = pty.spawn('sleep', ['3'], {
  name: 'xterm-color',
  cols: 80,
  rows: 30,
  cwd: process.env.HOME,
  env: process.env
});

term.on('exit', function(code) {
  console.error('CODE: ' + code);
});

OR:

var pty = require('pty.js');

var term = pty.spawn('bash', ['-c', 'echo 1; sleep 3; echo 2; exit 1'], {
  name: 'xterm-color',
  cols: 80,
  rows: 30,
  cwd: process.env.HOME,
  env: process.env
});

term.on('data', function(data) {
  process.stderr.write(data);
});

term.on('exit', function(code) {
  console.error('CODE: ' + code);
});

@tobiastom

Copy link
Copy Markdown

@chjj Works great for me on:

  • FreeBSD 10.1 with node v0.12.7
  • OS X 10.10.4 with node v0.12.7
  • OS X 10.11 (beta) with node v0.12.4

Thanks again for adding this functionality!

@chjj

chjj commented Jul 31, 2015

Copy link
Copy Markdown
Owner

Remember to thank @joshwilsdon: he figured out a way to do this without overriding node's SIGCHLD handler, which was a huge issue in the first place.

@tobiastom

Copy link
Copy Markdown

Oh, I missed that the pull request originated from him.

Thanks @joshwilsdon. You made my day. :)

@chjj

chjj commented Aug 1, 2015

Copy link
Copy Markdown
Owner

Fixed a few issues:

  • Due to a race condition, sometimes we would get an assertion error because the handle.data was freed before UV_CLOSING was set on the flags: node: ../deps/uv/src/unix/core.c:210: uv__finish_close: Assertion 'handle->flags & UV_CLOSING' failed. The solution was to delete handle.data in the uv_close() callback instead.
  • Due to another race condition, sometimes the last data event would be emitted after exit. It now waits for the socket to be destroyed before emitting exit.

@chjj

chjj commented Aug 1, 2015

Copy link
Copy Markdown
Owner

It would be wise to test these fixes on all platforms and versions again.

chjj added a commit that referenced this pull request Aug 2, 2015
@chjj

chjj commented Aug 2, 2015

Copy link
Copy Markdown
Owner

Also, it looks like in node v0.8.x, waitpid is already handled somewhere else. errno is ECHILD. I've accounted for this, and everything will work, but node v0.8.x will always get an exit status of 0.

@chjj

chjj commented Aug 2, 2015

Copy link
Copy Markdown
Owner

pty.js v0.2.10 published. Please test.

@tobiastom

Copy link
Copy Markdown

@chjj 0.2.11 seems to work on all my tested platforms from above.

@chjj

chjj commented Aug 4, 2015

Copy link
Copy Markdown
Owner

@tobiastom, good news.

And I have another small improvement for this I'm going to publish tonight.

It looks like every platform from node v0.8.x onward should work at least.

chjj added a commit that referenced this pull request Aug 4, 2015
destitutus pushed a commit to codio/pty.js that referenced this pull request Jul 5, 2016
* add support for .on('exit', function (code, signal) {})

* use an arg instead of arguments[0]

* htop command cannot resize with env.LINSE and env.COLUMNS

* v0.2.8. fixes chjj#122.

* style conventions.

* fix build. see chjj#110.

* call uv_close to take cb off the event loop. see chjj#110.

* more style fixes.

* use older v8 api for function casts?

* fix after_wait_on_pid for node v0.10.x. see chjj#110.

* fix close events. see chjj#110.

* v0.2.9

* refactor waitpid thread.

* refactor pty.fork args.

* free up baton in uv_close callback. see chjj#110.

* fix issue where exit is emitted before the last data event. see chjj#110.

* account for node v0.8.x on waitpid. see chjj#110.

* v0.2.10

* add comment.

* v0.2.11

* better onexit callback.

* event better onexit callback. see chjj#110.

* v0.2.12

* create scope in pty_after_waitpid to prevent fatal v8 error in io.js. fixes chjj#126.

* v0.2.13

* upgrade to nan 2.0.5. see chjj#127.

* remove "heap-style" Nan::Callback.

* strict nan version.

* remove useless v8 namespace prefixes.

* minor: whitespace. cols.

* bind to close only once.

* v0.3.0

* update win/pty.cc to work with nan 2.0.5

* Optional resuming of the socket

The immediate resumption the a socket means that output can be lost
unless you start consuming it right away. In some cases, this
behavior is undesirable. Adding an option to not automatically resume
the socket enables data to be read after a delay.

* Update nan version
destitutus added a commit to codio/pty.js that referenced this pull request Jun 21, 2018
* add support for .on('exit', function (code, signal) {})

* use an arg instead of arguments[0]

* htop command cannot resize with env.LINSE and env.COLUMNS

* v0.2.8. fixes chjj#122.

* style conventions.

* fix build. see chjj#110.

* call uv_close to take cb off the event loop. see chjj#110.

* more style fixes.

* use older v8 api for function casts?

* fix after_wait_on_pid for node v0.10.x. see chjj#110.

* fix close events. see chjj#110.

* v0.2.9

* refactor waitpid thread.

* refactor pty.fork args.

* free up baton in uv_close callback. see chjj#110.

* fix issue where exit is emitted before the last data event. see chjj#110.

* account for node v0.8.x on waitpid. see chjj#110.

* v0.2.10

* add comment.

* v0.2.11

* better onexit callback.

* event better onexit callback. see chjj#110.

* v0.2.12

* create scope in pty_after_waitpid to prevent fatal v8 error in io.js. fixes chjj#126.

* v0.2.13

* upgrade to nan 2.0.5. see chjj#127.

* remove "heap-style" Nan::Callback.

* strict nan version.

* remove useless v8 namespace prefixes.

* minor: whitespace. cols.

* bind to close only once.

* v0.3.0

* update win/pty.cc to work with nan 2.0.5

* Optional resuming of the socket

The immediate resumption the a socket means that output can be lost
unless you start consuming it right away. In some cases, this
behavior is undesirable. Adding an option to not automatically resume
the socket enables data to be read after a delay.

* update dependency nan@2.0.5 to nan@2.3.3

* update nan to @2.3.5

* v0.3.1

* pty.kill is not a function

see https://codio.myjetbrains.com/youtrack/issue/codio-9306

* add comment
@sandrewh

sandrewh commented Jul 20, 2018 •

Copy link
Copy Markdown

In order to have the shell pass the exit code through to the term.on('exit', function(code){...});handler, I had to do something like...

pty.spawn('bash', ['-c', /path/to/my/app; exit $?'], {

plemarquand added a commit to plemarquand/vscode-swift that referenced this pull request Feb 23, 2025
node-pty on Linux suffers from a long standing issue where the last
chunk of output before a program exits is sometimes dropped, especially
if that program produces a lot of output immediately before exiting.
See microsoft/node-pty#72

We can work around this by delegating to bash and forwarding the exit
code. This causes the underlying pipe to be fully flushed before we
recieve the exit event. This workaround was suggested here:
chjj/pty.js#110 (comment)

Swift currently ships on Ubuntu Debian and Fedora which all come with
`bash`. This patch assumes additional Linux distributions added in the
future will ship with `bash`.

Issue: swiftlang#1393
plemarquand added a commit to plemarquand/vscode-swift that referenced this pull request Feb 23, 2025
node-pty on Linux suffers from a long standing issue where the last
chunk of output before a program exits is sometimes dropped, especially
if that program produces a lot of output immediately before exiting.
See microsoft/node-pty#72

We can work around this by delegating to bash and forwarding the exit
code. This causes the underlying pipe to be fully flushed before we
recieve the exit event. This workaround was suggested here:
chjj/pty.js#110 (comment)

Swift currently ships on Ubuntu Debian and Fedora which all come with
`bash`. This patch assumes additional Linux distributions added in the
future will ship with `bash`.

Issue: swiftlang#1393
plemarquand added a commit to plemarquand/vscode-swift that referenced this pull request Feb 23, 2025
node-pty on Linux suffers from a long standing issue where the last
chunk of output before a program exits is sometimes dropped, especially
if that program produces a lot of output immediately before exiting.
See microsoft/node-pty#72

We can work around this by delegating to bash and forwarding the exit
code. This causes the underlying pipe to be fully flushed before we
recieve the exit event. This workaround was suggested here:
chjj/pty.js#110 (comment)

Swift currently ships on Ubuntu Debian and Fedora which all come with
`bash`. This patch assumes additional Linux distributions added in the
future will ship with `bash`.

Issue: swiftlang#1393
plemarquand added a commit to plemarquand/vscode-swift that referenced this pull request Feb 23, 2025
node-pty on Linux suffers from a long standing issue where the last
chunk of output before a program exits is sometimes dropped, especially
if that program produces a lot of output immediately before exiting.
See microsoft/node-pty#72

We can work around this by delegating to bash and forwarding the exit
code. This causes the underlying pipe to be fully flushed before we
recieve the exit event. This workaround was suggested here:
chjj/pty.js#110 (comment)

Swift currently ships on Ubuntu Debian and Fedora which all come with
`bash`. This patch assumes additional Linux distributions added in the
future will ship with `bash`.

Issue: swiftlang#1393
plemarquand added a commit to plemarquand/vscode-swift that referenced this pull request Feb 23, 2025
node-pty on Linux suffers from a long standing issue where the last
chunk of output before a program exits is sometimes dropped, especially
if that program produces a lot of output immediately before exiting.
See microsoft/node-pty#72

We can work around this by delegating to bash and forwarding the exit
code. This causes the underlying pipe to be fully flushed before we
recieve the exit event. This workaround was suggested here:
chjj/pty.js#110 (comment)

Swift currently ships on Ubuntu Debian and Fedora which all come with
`bash`. This patch assumes additional Linux distributions added in the
future will ship with `bash`.

Issue: swiftlang#1393
plemarquand added a commit to plemarquand/vscode-swift that referenced this pull request Feb 23, 2025
node-pty on Linux suffers from a long standing issue where the last
chunk of output before a program exits is sometimes dropped, especially
if that program produces a lot of output immediately before exiting.
See microsoft/node-pty#72

We can work around this by delegating to bash and forwarding the exit
code. This causes the underlying pipe to be fully flushed before we
recieve the exit event. This workaround was suggested here:
chjj/pty.js#110 (comment)

Swift currently ships on Ubuntu Debian and Fedora which all come with
`bash`. This patch assumes additional Linux distributions added in the
future will ship with `bash`.

Issue: swiftlang#1393
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

exit code

7 participants