<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>~iany/ Async Programming</title><link>https://blog.iany.me/tags/async-programming/</link><description>Recent content in Async Programming «~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>Sat, 11 Apr 2020 21:08:33 +0800</lastBuildDate><atom:link href="https://blog.iany.me/tags/async-programming/index.xml" rel="self" type="application/rss+xml"/><item><title>Rust Pin</title><link>https://blog.iany.me/2020/04/rust-pin/</link><pubDate>Sat, 11 Apr 2020 21:08:33 +0800</pubDate><author>me@iany.me (Ian Yang)</author><guid>https://blog.iany.me/2020/04/rust-pin/</guid><description>&lt;p&gt;&lt;code&gt;Pin&lt;/code&gt; is an obscure type in Rust because of the naming and indirect concepts.&lt;/p&gt;
&lt;p&gt;The first indirect concept is pointer. &lt;code&gt;Pin&amp;lt;X&amp;gt;&lt;/code&gt; does not guarantee that X will not move. If X is a pointer which target type is T, &lt;code&gt;Pin&amp;lt;X&amp;gt;&lt;/code&gt; guarantees that T will not move.&lt;/p&gt;
&lt;p&gt;The second is &amp;ldquo;not move&amp;rdquo;. It really means that the only way to get the mut reference to T is via unsafe interface.&lt;/p&gt;
&lt;p&gt;The last is the &lt;code&gt;Unpin&lt;/code&gt; mark trait. Pin forbids safe interface to get the mut reference to T only when T is &lt;code&gt;!Unpin&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In simple words, Pin is a pointer wrapper. When a pointer is trapped inside Pin, and the pointee type is &lt;code&gt;!Unpin&lt;/code&gt;, there&amp;rsquo;s no safe way to get a mut reference to the pointee.&lt;/p&gt;
&lt;p&gt;The following diagram has listed what Pin provides and constraints on when we can use those functions.&lt;/p&gt;
&lt;figure class="kg-image-card"&gt;
&lt;img alt="Rust Pin" class="kg-image" loading="lazy" src="https://blog.iany.me/2020/04/rust-pin/Rust%20Pin_hu_223e55d1744a8de7.png" srcset="https://blog.iany.me/2020/04/rust-pin/Rust%20Pin_hu_9a49407eb0b9f490.png 400w, https://blog.iany.me/2020/04/rust-pin/Rust%20Pin_hu_40a792bdf744ce0d.png 800w, https://blog.iany.me/2020/04/rust-pin/Rust%20Pin_hu_44d2a159ada1f2a1.png 1200w, https://blog.iany.me/2020/04/rust-pin/Rust%20Pin_hu_b2a20cac56bf1a4b.png 1600w, https://blog.iany.me/2020/04/rust-pin/Rust%20Pin_hu_223e55d1744a8de7.png 1800w" sizes="(max-width: 1600px) 100vw, 1800px" /&gt;
&lt;/figure&gt;
&lt;p&gt;In Rust, the pointer is indeed the trait &lt;code&gt;Deref&lt;/code&gt; and &lt;code&gt;DerefMut&lt;/code&gt;, from which we can get the shared or mut reference.&lt;/p&gt;
&lt;p&gt;It is not very interesting when the pointer is &lt;code&gt;Deref&lt;/code&gt;. The essential of Pin is when X is &lt;code&gt;DerefMut&lt;/code&gt;. The yellow box shows that the safe interface to get the mut reference is available when T is &lt;code&gt;Unpin&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Unpin&lt;/code&gt; is implemented for types by default. It acts as a safety which disables the core feature of Pin. Pin is only effective when the safety is turned off, a.k.a, when the type is explicitly marked as &lt;code&gt;!Unpin&lt;/code&gt; via &lt;a href="https://doc.rust-lang.org/std/marker/struct.PhantomPinned.html"&gt;PhantomPinned&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Pin is a signal that a self reference type may appear here. It is also a contract. The type provider must mark the type as &lt;code&gt;!Unpin&lt;/code&gt; if it is unsafe to move. The type user must promise not to move the &lt;code&gt;!Unpin&lt;/code&gt; data in the unsafe block.&lt;/p&gt;
&lt;h2 id="further-readings"&gt;Further Readings&lt;/h2&gt;
&lt;p&gt;The &lt;a href="https://doc.rust-lang.org/std/pin/index.html"&gt;pin module&lt;/a&gt; document has explained why and the typical scenario.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;It is sometimes useful to have objects that are guaranteed not to move, in the sense that their placement in memory does not change, and can thus be relied upon. A prime example of such a scenario would be building self-referential structs, as moving an object with pointers to itself will invalidate them, which could cause undefined behavior.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;code&gt;Pin&lt;/code&gt; was suggested in &lt;a href="https://github.com/rust-lang/rfcs/blob/master/text/2349-pin.md"&gt;RFC#2349&lt;/a&gt;. It was also well explained in the book &lt;a href="https://cfsamson.github.io/books-futures-explained/5_pin.html"&gt;Futures Explained in 200 Lines of Rust.&lt;/a&gt;&lt;/p&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/rust/">Rust</category></item><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>