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

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

Merged
merged 2 commits into from
Jul 30, 2015
Merged

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

merged 2 commits into from
Jul 30, 2015

Conversation

joshwilsdon
Copy link
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
Copy link

psaia commented Apr 9, 2015

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

@javruben
Copy link

@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

I would be interested into this as well.

@amshali
Copy link

amshali commented May 6, 2015

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
Copy link
Owner

chjj commented Jul 30, 2015

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
Copy link
Owner

chjj commented Jul 30, 2015

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
Copy link
Owner

chjj commented Jul 30, 2015

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

@chjj
Copy link
Owner

chjj commented Jul 30, 2015

That last commit seemed to fix it.

@chjj
Copy link
Owner

chjj commented Jul 30, 2015

Sorry, the commit above this comment ^

@chjj
Copy link
Owner

chjj commented Jul 30, 2015

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

@amshali
Copy link

amshali commented Jul 30, 2015

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

@chjj
Copy link
Owner

chjj commented Jul 31, 2015

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

@chjj
Copy link
Owner

chjj commented Jul 31, 2015

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

@chjj
Copy link
Owner

chjj commented Jul 31, 2015

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

@chjj
Copy link
Owner

chjj commented Jul 31, 2015

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

@chjj
Copy link
Owner

chjj commented Jul 31, 2015

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

@chjj
Copy link
Owner

chjj commented Jul 31, 2015

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
Copy link
Owner

chjj commented Jul 31, 2015

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
Copy link
Owner

chjj commented Jul 31, 2015

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

@chjj
Copy link
Owner

chjj commented Jul 31, 2015

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

@amshali
Copy link

amshali commented Jul 31, 2015

Thanks a bunch!!! :)

@tobiastom
Copy link

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
Copy link
Owner

chjj commented Jul 31, 2015

@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

@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
Copy link
Owner

chjj commented Jul 31, 2015

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

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

Thanks @joshwilsdon. You made my day. :)

@chjj
Copy link
Owner

chjj commented Aug 1, 2015

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
Copy link
Owner

chjj commented Aug 1, 2015

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
Copy link
Owner

chjj commented Aug 2, 2015

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
Copy link
Owner

chjj commented Aug 2, 2015

pty.js v0.2.10 published. Please test.

@tobiastom
Copy link

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

@chjj
Copy link
Owner

chjj commented Aug 4, 2015

@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
Copy link

sandrewh commented Jul 20, 2018

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 $?'], {

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