<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>~iany/ Node.js</title><link>https://blog.iany.me/tags/nodejs/</link><description>Recent content in Node.js «~iany/»</description><language>en-US</language><managingEditor>me@iany.me (Ian Yang)</managingEditor><webMaster>me@iany.me (Ian Yang)</webMaster><copyright>CC-BY-SA 4.0</copyright><lastBuildDate>Sun, 01 Mar 2020 09:29:40 +0800</lastBuildDate><atom:link href="https://blog.iany.me/tags/nodejs/index.xml" rel="self" type="application/rss+xml"/><item><title>Blocking Stdout</title><link>https://blog.iany.me/2020/03/blocking-stdout/</link><pubDate>Sun, 01 Mar 2020 09:29:40 +0800</pubDate><author>me@iany.me (Ian Yang)</author><guid>https://blog.iany.me/2020/03/blocking-stdout/</guid><description>&lt;p&gt;When I first read Stjepan&amp;rsquo;s article &lt;a href="https://web.archive.org/web/20200815123809/https://stjepang.github.io/2019/12/04/blocking-inside-async-code.html"&gt;Blocking inside async code&lt;/a&gt;, I never though I will met the problem mentioned in the post.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;… I bet we all most of the time assume printing to standard output does not block while it really could.&lt;/p&gt;
&lt;p&gt;In case you’re wondering why &lt;code&gt;println!()&lt;/code&gt; can block, imagine we executed &lt;code&gt;program1 | program2&lt;/code&gt; in a shell so that the output of &lt;code&gt;program1&lt;/code&gt; is piped into &lt;code&gt;program2&lt;/code&gt;. If &lt;code&gt;program2&lt;/code&gt; is reading input very slowly, then &lt;code&gt;program1&lt;/code&gt; will have to block whenever it prints something and the pipe is full.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Fortunately, my brain has stored the clue somewhere, and I can retrieve it and save my day when I heart a weird bug.&lt;/p&gt;
&lt;p&gt;We have a GUI app &lt;strong&gt;N&lt;/strong&gt; written in Node which bundles a service binary &lt;strong&gt;C&lt;/strong&gt;. &lt;strong&gt;N&lt;/strong&gt; starts &lt;strong&gt;C&lt;/strong&gt; as a sub-process. &lt;strong&gt;C&lt;/strong&gt; writes logs to a file. After 10 minutes, it stops writing the log file until restarted.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;C&lt;/strong&gt; uses a single thread to write logs. But default, it sends the logs to both stdout and log file. When the stdout buffer of &lt;strong&gt;C&lt;/strong&gt; is full, the logging thread stuck, so it also stops writing the log file.&lt;/p&gt;
&lt;p&gt;A simple fix is telling &lt;strong&gt;C&lt;/strong&gt; not write to stdout. But the root cause is that the app &lt;strong&gt;N&lt;/strong&gt; keeps the child process stdout pipe open and never read from the pipe. The best practice is that if you don&amp;rsquo;t read from the pipe, close it.&lt;/p&gt;
&lt;p&gt;Following is an example In Node to close both stdin and stdout via &lt;a href="https://nodejs.org/api/child_process.html#child_process_options_stdio"&gt;option stdio&lt;/a&gt;. The stderr is open because the parent process will read from the pipe.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-javascript"&gt;const c = spawn('c', [], {
stdio: ['ignore', 'ignore', 'pipe']
});
c.stderr.on('data', (data) =&amp;gt; {
console.log(`c: ${data}`);
});
&lt;/code&gt;&lt;/pre&gt;</description><category domain="https://blog.iany.me/post/">Posts</category><category domain="https://blog.iany.me/tags/async-programming/">Async Programming</category><category domain="https://blog.iany.me/tags/nodejs/">Node.js</category><category domain="https://blog.iany.me/tags/rust/">Rust</category></item></channel></rss>