vavere/htmltidy — pull request #20 by pjeby (2014-05-22 23:29:19+00:00)
Title: Fix for sporadic truncation of output files due to incomplete I/O

Hi. I'm using htmltidy as part of a regression test, to compare output of a system with its previous output. On numerous occasions, one file out of hundreds processed will be truncated somewhere in the middle. After extensive analysis and experimentation, I determined that the problem seems to be due to Node sometimes firing the subprocess ``exit`` event shortly *before* the last ``data`` event from the subprocess's stdout. (This is on Windows 7, by the way.)

This one-line change fixes the problem for ``tidy()`` calls, by delaying the final result callback until Node has finished processing all pending I/O, thus ensuring that the remaining ``data`` event has a chance to fire before the final callback is invoked.

Without the patch, an average of about 1 (randomly selected) file per two thousand processed is truncated on my computer. But with the patch, none are ever truncated as far as I can tell.

(The build is a complex one, with lots of other streaming events going on left and right, so I imagine the event ordering is a little less deterministic than in simpler processes using htmltidy, or that are not invoking it hundreds of times in rapid succession.)