Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Fix output buffer is imcomplete working in child_process #1834

Merged
merged 9 commits into from
Dec 13, 2016

Conversation

ysugimoto
Copy link
Contributor

When I send a large buffer, over 50KB's CSS string, working in child_process.spawn, node-sass outputs only partial strings.

Certainly, it is caused by nodejs's console.log max size.
nodejs/node#9546

So I modified to send output by continuous partial strings, its asynchronous.

This PR will fix this issue: #1823.

});

emitter.on('done', function() {
if (!options.watch && !options.directory) {
if (!options.watch && !options.directory && !asyncWaiting) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't asyncWaiting always true now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asyncWaiting is initialized on false when called getEmitter().
https://github.com/sass/node-sass/pull/1834/files#diff-c19ab2481e2703fcf0d2808f4c762f63R147

I read project source code, emit log event only stdout case ( is it right? ).
So that, asyncWaiting set to be true only emitted log event.

Please advise if there are other concerns that you need to worry about. thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is right. I believe we always want to exit when emitter.log is called. This is what happens when you call the node-sass cli. If I understand correctly all we need to do is

emitter.on('log', stdout.write);

Am I missing something?

@xzyfer
Copy link
Contributor

xzyfer commented Dec 13, 2016

https://www.npmjs.com/package/stdout-stream seems like a better alternative than writing out own stdout stream lib.

@ysugimoto
Copy link
Contributor Author

I replaced out stdout lib to suggested package, it also works fine!

@ysugimoto
Copy link
Contributor Author

It works fine, but Travis CI is down for some testings...

@xzyfer
Copy link
Contributor

xzyfer commented Dec 13, 2016

I'm sorry the issue here is not clear.

Either the problem is related to console.logs max buffer size, in which we can fix by

emitter.on('log', stdout.write)

It's because process.stdout is no longer blocking, in which case we can use https://github.com/isaacs/block-stream. Either way i don't think this is the correct solution.

@ysugimoto
Copy link
Contributor Author

ysugimoto commented Dec 13, 2016

Yes, I think so.

However, stdout.write is no longer blocking, then we stop process.exit until stdout stream output completely. It's called by emitter.emit('done') immediately, So I add the flag of asyncWaiting to guard process exit.

@xzyfer
Copy link
Contributor

xzyfer commented Dec 13, 2016

OK I did some digging. You're correct that the issue is due how process.exit works.

It is important to note that calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

I see ways to solve this:

  • remove the process.exit (I don't think we need it)
  • use exit instead

@ysugimoto
Copy link
Contributor Author

Thank you for your advice. I replaced to process.exit to exit package.

And I removed exiting in emitter.on('done', ...) callback because of process exited even though using stdout-stream package, I wondering...
After removed this point, It works fine: CLI, spawned process, and watch mode.

@@ -161,13 +163,12 @@ function getEmitter() {
});

emitter.on('log', function(data) {
Copy link
Contributor

@xzyfer xzyfer Dec 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emitter.on('log', stdout.write)

@@ -161,13 +163,12 @@ function getEmitter() {
});

emitter.on('log', function(data) {
console.log(data);
// In nodejs <= 7.1.0, console.log cannot write output larger than 8275 bytes data.
stdout.write(data);
});

emitter.on('done', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete the emitter.on('done' block all together.

@@ -150,7 +152,7 @@ function getEmitter() {
}
console.error(err);
if (!options.watch) {
process.exit(1);
exit(1);
Copy link
Contributor

@xzyfer xzyfer Dec 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.exit is fine here because console.error is synchronous.

@ysugimoto
Copy link
Contributor Author

https://github.com/sass/node-sass/pull/1834/files#diff-c19ab2481e2703fcf0d2808f4c762f63R165

Use bind() because of avoid error on stdout-stream.

@@ -56,6 +56,7 @@
"async-foreach": "^0.1.3",
"chalk": "^1.1.1",
"cross-spawn": "^3.0.0",
"exit": "^0.1.2",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

@@ -47,21 +47,14 @@ module.exports = function(options, emitter) {
var stdin = options.stdin;

var success = function(result) {
var todo = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The done still needs to be fired. It's used in other places. Please undo these changes and just remove

emitter.on('done', function() {		
    // noop		
});

@xzyfer
Copy link
Contributor

xzyfer commented Dec 13, 2016

Thank you for your work and patience. I'll merge this when CI is happy.

@xzyfer xzyfer merged commit 3c17b03 into sass:master Dec 13, 2016
gmaliar pushed a commit to gmaliar/node-sass that referenced this pull request Dec 17, 2016
Fix premature exit before the output stream is fully flushed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants