Browse Source

Fix #2257 pause/resume semantics for stdin

This makes it so that the stdin TTY-wrap stream gets ref'ed on
.resume() and unref'ed on .pause()

The semantics of the names "pause" and "resume" are a bit weird, but the
important thing is that this corrects an API change from 0.4 -> 0.6
which made it impossible to read from stdin multiple times, without
knowing when it might end up being closed.  If no one has it open, this
lets the process die naturally.

LGTM'd by @ry
v0.7.4-release
isaacs 13 years ago
parent
commit
cf20b6bf65
  1. 2
      lib/net.js
  2. 11
      lib/tty.js
  3. 30
      src/handle_wrap.cc
  4. 1
      src/handle_wrap.h
  5. 1
      src/pipe_wrap.cc
  6. 1
      src/tty_wrap.cc
  7. 37
      test/simple/test-stdin-pause-resume.js

2
lib/net.js

@ -194,7 +194,9 @@ Object.defineProperty(Socket.prototype, 'bufferSize', {
Socket.prototype.pause = function() { Socket.prototype.pause = function() {
if (this._handle) {
this._handle.readStop(); this._handle.readStop();
}
}; };

11
lib/tty.js

@ -83,8 +83,19 @@ function ReadStream(fd) {
this.on('newListener', onNewListener); this.on('newListener', onNewListener);
} }
inherits(ReadStream, net.Socket); inherits(ReadStream, net.Socket);
exports.ReadStream = ReadStream; exports.ReadStream = ReadStream;
ReadStream.prototype.pause = function() {
this._handle.unref();
return net.Socket.prototype.pause.call(this);
};
ReadStream.prototype.resume = function() {
this._handle.ref();
return net.Socket.prototype.resume.call(this);
};
ReadStream.prototype.isTTY = true; ReadStream.prototype.isTTY = true;

30
src/handle_wrap.cc

@ -64,8 +64,10 @@ Handle<Value> HandleWrap::Unref(const Arguments& args) {
UNWRAP UNWRAP
// Calling this function twice should never happen. // Calling unnecessarily is a no-op
assert(wrap->unref == false); if (wrap->unref) {
return v8::Undefined();
}
wrap->unref = true; wrap->unref = true;
uv_unref(uv_default_loop()); uv_unref(uv_default_loop());
@ -74,6 +76,24 @@ Handle<Value> HandleWrap::Unref(const Arguments& args) {
} }
// Adds a reference to keep uv alive because of this thing.
Handle<Value> HandleWrap::Ref(const Arguments& args) {
HandleScope scope;
UNWRAP
// Calling multiple times is a no-op
if (!wrap->unref) {
return v8::Undefined();
}
wrap->unref = false;
uv_ref(uv_default_loop());
return v8::Undefined();
}
Handle<Value> HandleWrap::Close(const Arguments& args) { Handle<Value> HandleWrap::Close(const Arguments& args) {
HandleScope scope; HandleScope scope;
@ -82,10 +102,8 @@ Handle<Value> HandleWrap::Close(const Arguments& args) {
assert(!wrap->object_.IsEmpty()); assert(!wrap->object_.IsEmpty());
uv_close(wrap->handle__, OnClose); uv_close(wrap->handle__, OnClose);
if (wrap->unref) {
uv_ref(uv_default_loop()); HandleWrap::Ref(args);
wrap->unref = false;
}
wrap->StateChange(); wrap->StateChange();

1
src/handle_wrap.h

@ -49,6 +49,7 @@ class HandleWrap {
static void Initialize(v8::Handle<v8::Object> target); static void Initialize(v8::Handle<v8::Object> target);
static v8::Handle<v8::Value> Close(const v8::Arguments& args); static v8::Handle<v8::Value> Close(const v8::Arguments& args);
static v8::Handle<v8::Value> Unref(const v8::Arguments& args); static v8::Handle<v8::Value> Unref(const v8::Arguments& args);
static v8::Handle<v8::Value> Ref(const v8::Arguments& args);
protected: protected:
HandleWrap(v8::Handle<v8::Object> object, uv_handle_t* handle); HandleWrap(v8::Handle<v8::Object> object, uv_handle_t* handle);

1
src/pipe_wrap.cc

@ -85,6 +85,7 @@ void PipeWrap::Initialize(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close); NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref); NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref);
NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart); NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop); NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);

1
src/tty_wrap.cc

@ -69,6 +69,7 @@ class TTYWrap : StreamWrap {
NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close); NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref); NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref);
NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart); NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop); NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);

37
test/simple/test-stdin-pause-resume.js

@ -0,0 +1,37 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
console.error("before opening stdin");
process.stdin.resume();
console.error("stdin opened");
setTimeout(function() {
console.error("pausing stdin");
process.stdin.pause();
setTimeout(function() {
console.error("opening again");
process.stdin.resume();
setTimeout(function() {
console.error("pausing again");
process.stdin.pause();
console.error("should exit now");
}, 1);
}, 1);
}, 1);
Loading…
Cancel
Save