stdout/stderr blocking

1,574 views
Skip to first unread message

billywhizz

unread,
Mar 21, 2012, 5:56:39 AM3/21/12
to nod...@googlegroups.com
could anyone enlighten me on why writing to stdout and stderr now blocks? also, do we have a list somewhere of which methods in node.js block by default? to start the ball rolling:

process.stdout.write (unless stdout is a pipe)
process.stderr.write (unless stderr is a pipe)
process.setgid
process.setuid
util.debug
console.log|warn|error|dir|timeEnd
fs.*Sync

mscdex

unread,
Mar 21, 2012, 6:38:51 AM3/21/12
to nodejs
On Mar 21, 5:56 am, billywhizz <apjohn...@gmail.com> wrote:
> could anyone enlighten me on why writing to stdout and stderr now blocks?
> also, do we have a list somewhere of which methods in node.js block by
> default? to start the ball rolling:

I think writes to stderr have always been synchronous, it's stdout's
behavior which has changed in 0.6.

Ben Noordhuis

unread,
Mar 21, 2012, 8:47:39 AM3/21/12
to nod...@googlegroups.com
On Wed, Mar 21, 2012 at 10:56, billywhizz <apjo...@gmail.com> wrote:
> could anyone enlighten me on why writing to stdout and stderr now blocks?

The two main reasons (IIRC):

1. The asynchronous nature of stdout confused a lot of people, esp.
the fact that Node could exit before all console.log statements have
been printed.
2. Printing faster than stdout could handle would queue up the output
in a backlog, leading to excessive memory usage.

On a side note, stderr has always been blocking.

Jorge

unread,
Mar 21, 2012, 9:04:32 AM3/21/12
to nod...@googlegroups.com

I asked the same question recently :
"Now a simple control-S in the terminal can grind to a halt any node.js server."
<http://groups.google.com/group/nodejs-dev/browse_thread/thread/92333182b1fd237e#>

To think that a single control-S could halt my node server makes me feel nervous.
OTOH, it's true that a control-C would be worse.
But even so...

On Mar 14, 2012, at 10:46 AM, Jorge wrote:

> Begin forwarded message:
>
>> From: Jorge Chamorro Bieling <bie...@terra.es>
>> Date: March 14, 2012 12:08:48 AM GMT+01:00
>> To: Isaac Schlueter <i...@izs.me>
>> Subject: Re: Now a simple control-S in the terminal can grind to a halt any node.js server.
>> On Mar 13, 2012, at 11:56 PM, Isaac Schlueter wrote:
>>>
>>> On Tue, Mar 13, 2012 at 15:04, Jorge Chamorro Bieling <bie...@terra.es> wrote:
>>>> Because now, it seems, that write()ing to stdout is blocking...
>>>>
>>>> For example:
>>>>
>>>> 1.- paste this in a shell:
>>>>
>>>> node << EOF
>>>> require('http').createServer(function(req, res) {
>>>> res.end("FAST");
>>>> process.stdout.write('.');
>>>> }).listen(8000);
>>>> EOF
>>>>
>>>>
>>>> 2.- and hit control-S
>>>>
>>>> 3.- then do an `ab -t 2 http://127.0.0.1:8000` in another shell, and all you'll get is :
>>>>
>>>> apr_poll: The timeout specified has expired (70007)
>>>>
>>>> Why? Because the node server is totally blocked by the control-S!
>>>>
>>>> Are you aware of that ?
>>>> Isn't that something to be concerned about ?
>>>> Why did you change write()ing to stdout to blocking ?
>>>>
>>>> Cheers,
>>>> --
>>>> Jorge.
>>
>>
>>> This API change was made quite some time ago. It prevents a lot of
>>> confusion and edge-case bugs.
>>>
>>> If it causes problems for you, please bring it up on the
>>> nodej...@googlegroups.com mailing list, where API changes can be
>>> discussed and explored from multiple angles.
>>>
>>> In the meantime, I would not recommend writing to stdout on every
>>> request if it might be blocked.
>>>
>>
>>
>> Isaac,
>>
>> It would be equally frozen whether it wrote on every request or just once.
>
> A warning, a message, anything, just a single char written to stdout could now freeze a node.js server.
> --
> Jorge.

C. Mundi

unread,
Mar 22, 2012, 12:37:12 AM3/22/12
to nod...@googlegroups.com

According to the docs, POSIX signals are mapped to events.  I was planning on just catching them.  I assume that's how the "press ctrlC again to exit" is implemented for the REPL.  Corrections?

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Ben Noordhuis

unread,
Mar 22, 2012, 10:21:05 AM3/22/12
to nod...@googlegroups.com
On Thu, Mar 22, 2012 at 05:37, C. Mundi <cmu...@gmail.com> wrote:
> According to the docs, POSIX signals are mapped to events.  I was planning
> on just catching them.  I assume that's how the "press ctrlC again to exit"
> is implemented for the REPL.  Corrections?

Some signals are. For example, you can catch Ctrl-C with
process.on('SIGINT', cb) (maybe not on Windows).

However, signals like SIGSTOP (which is what some terminals send if
you press Ctrl-S) cannot be caught.

C. Mundi

unread,
Mar 22, 2012, 10:55:57 AM3/22/12
to nod...@googlegroups.com

Bummer.  I never thought about that, but it makes sense now that I do.  One more reason not to rely on stdin/stdout for stuff I don't want interrupted I guess.

Matt

unread,
Mar 22, 2012, 10:59:41 AM3/22/12
to nod...@googlegroups.com
I think it's just a reason not to rely on running your process interactively in a production environment. Use runit or forever or some other backgrounding solution. I have no idea why people are getting their pants in a knot over ctrl-s pausing a process.

Dan North

unread,
Mar 22, 2012, 12:29:28 PM3/22/12
to nod...@googlegroups.com
A couple of events can't be trapped (KILL and STOP) so you'll never receive events for those.

From: "C. Mundi" <cmu...@gmail.com>
Date: Wed, 21 Mar 2012 21:37:12 -0700
Subject: Re: [nodejs] stdout/stderr blocking

C. Mundi

unread,
Mar 22, 2012, 12:30:39 PM3/22/12
to nod...@googlegroups.com

Well and plainly spoken, Matt.  I use upstart (don't even get me going on forever). 

I will admit that I sometimes pound out an interactive tool out of expedience and that it would be nice to suppress some signals as a guard against my fat fingers, but that's an informed choice and not any shortcoming of POSIX, node, etc.

C. Mundi

unread,
Mar 22, 2012, 12:32:15 PM3/22/12
to nod...@googlegroups.com

Right.  I forgot that STOP was one of them.  I almost never send a stopint.  I'm a simple thinker...I use sigkill liberally.  : )

Ilya Dmitrichenko

unread,
Mar 22, 2012, 12:56:29 PM3/22/12
to nod...@googlegroups.com
"don't even get me going on forever" => +1k!

C. Mundi

unread,
Mar 22, 2012, 1:53:27 PM3/22/12
to nod...@googlegroups.com

I wasn't trying to practice Zen...it just happened.  :)

On Mar 22, 2012 9:56 AM, "Ilya Dmitrichenko" <errorde...@gmail.com> wrote:
"don't even get me going on forever" => +1k!

Reply all
Reply to author
Forward
0 new messages