-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
Any way to to turn console into fully synchronous? #11568
Comments
There is more info here: https://github.com/nodejs/node/blob/master/doc/api/process.md#a-note-on-process-io It will be up on the live docs in the next 7.x release. The console (really Please read the docs linked above before doing this. if (process.stdout._handle) process.stdout._handle.setBlocking(true) But, I don't recommend it. Please let me know if you have more questions. |
If console is mostly synchronous why Moreover @bnoordhuis wrote:
I wrote a small package console-sync which replace console methods to be true synchronous, like this:
It some kind of hack but fixes problem with memory leaks and crashes. Moreover it works noticeably faster than the default asynchronous implementation!
|
It is not asynchronous unless you are in one of the conditions as documented above. (I made this change.) In some older versions the behaviour was different. It still goes through libuv blocking or not so it's possible that has some overhead. |
@Fishrock123 thanks for clarifications. I'm thinking that i found a cause of leaks in the console.
for(var i=0; ;++i)
{
console.log("Value of i=%d",i);
} To ensure, that console is synchronous run this script with redirection to file:
Task manager screenshot: how works |
But meanwhile... function write(......) {
if ( stream is synchronous ) {
fs.writeSync(stream.fd,.....)
} else {
//execute current code
}
} |
Hmm… so I assume the problem is that the callbacks passed to |
A couple of things: I can reproduce the memory problem on all the versions of Node I checked (including v0.10) when writing to standard out on Mac OS X. This is normal and expected because we are not respecting backpressure. However, at some point we were writing synchronously when it was file, causing the process to block and slow down. IMHO we are doing the correct behavior atm, e.g. Node.js is expected to ran out of memory when using it in the way described in #11568 (comment). Probably we should explain this better. |
@litmit a stream cannot be synchronous. |
YES!
Simple call of
How it can block more when as described by @Fishrock123
You completely confusing me because this does not match with https://github.com/nodejs/node/blob/master/doc/api/process.md#a-note-on-process-io Suggested solution: In this case we can turn console to be sync/async just using |
@mcollina stdio is weird and "violates" this expectation. |
Streams will always do their error handling/other callback stuff using |
Should this issue stay open? Or is this close-able? |
I do not think this is fixable, not in the generic case anyway. |
I'd say this is closable |
Closing due to the mentioned reasons. |
As suggested above |
Why is:
Not recommended? I know that nodejs is probably attempting to maintain legacy behaviour. But why not make stdout/stderr synchronous when stdout is interacting with a TTY. Example: // Default behaviour on Node.js:
// Files: synchronous on Windows and POSIX
// TTYs (Terminals): asynchronous on Windows, synchronous on POSIX
// Pipes (and sockets): synchronous on Windows, asynchronous on POSIX
// In order to align Windows with POSIX behaviour:
if (process.stdout.isTTY) {
process.stdout._handle.setBlocking(true);
} else if (os.platform() === 'win32' && !process.stdout.isTTY) {
process.stdout._handle.setBlocking(false);
} |
The fact that stdout could be asynchronous concerns process.exit, but it should be OK except for Windows. nodejs/node#11568
The fact that stdout could be asynchronous concerns process.exit, but it should be OK except for Windows. nodejs/node#11568
With Node.js possible to write nice small an reliable fast synchronous console applications using Javascript.
But these applications may consume all available memory and crash with
message
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
.After investigation was founded cause of crash -
console.log()
See also #3524 for details.
With Node 4.x
console.log()
worked in synchronous mode when stdout redirected to file, therefore this problem has workaround, but not with Node 6.x (and this not described in doc https://nodejs.org/api/console.html#console_asynchronous_vs_synchronous_consoles)Please, add feature which allow to turn console into fully synchronous mode!
This may be command line option (
--console-sync
) or API call likeconsole.mode('sync')
or something else.Without this it very hard to debug a synchronous application using
console.log()
orconsole.error()
when an application generate a tons of output.The text was updated successfully, but these errors were encountered: