<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Boris Okunskiy on Medium]]></title>
        <description><![CDATA[Stories by Boris Okunskiy on Medium]]></description>
        <link>https://medium.com/@borisokunskiy?source=rss-5153fbfd2817------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*HkhEJbkV62rl0vti.jpg</url>
            <title>Stories by Boris Okunskiy on Medium</title>
            <link>https://medium.com/@borisokunskiy?source=rss-5153fbfd2817------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 09:40:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@borisokunskiy/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[AWS Lambda + Chrome Headless  ]]></title>
            <link>https://medium.com/the-automator/aws-lambda-chrome-headless-5db989d36040?source=rss-5153fbfd2817------2</link>
            <guid isPermaLink="false">https://medium.com/p/5db989d36040</guid>
            <category><![CDATA[web-automation]]></category>
            <category><![CDATA[headless-chrome]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <dc:creator><![CDATA[Boris Okunskiy]]></dc:creator>
            <pubDate>Mon, 06 Nov 2017 07:39:18 GMT</pubDate>
            <atom:updated>2017-11-06T07:39:18.878Z</atom:updated>
            <content:encoded><![CDATA[<p>TL;DR Here’s a <a href="https://github.com/universalbasket/aws-lambda-chrome">fresh Chrome 64 headless build</a> which runs smoothly in AWS Lambda (specifically, survives the <a href="https://github.com/adieuadieu/serverless-chrome/issues/41#issuecomment-322126328"><em>5th-invocation-crash</em></a>). We also maintain <a href="https://github.com/universalbasket/aws-lambda-chrome/blob/master/BUILD.md">build instructions</a> which includes all necessary hacks to make it work.</p><p>With ❤️ from <a href="http://about.ub.io/">ubio</a>. Enjoy, spread the love, keep in touch!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nanTRHP3y9OGWz5-5wKLKw.jpeg" /></figure><p>The idea of running web automation on a crazy scale with AWS Lambda was floating around ever since Chrome Headless was announced in the beginning of 2017. Just imagine how many awesome things you could do with your own Cloud Browser buddy who is capable of making screenshots and PDFs, accessing and manipulating DOM, simulating input events and <a href="https://chromedevtools.github.io/devtools-protocol/tot">much, much more</a> — and all that on a ridiculous scale of Lambda.</p><p>Excited yet?</p><p>Well, if you ever tried to actually make it work, then most likely you are already familiar with <a href="https://github.com/adieuadieu">Marco Lüthy</a>, his <a href="https://github.com/adieuadieu/serverless-chrome">serverless-chrome</a> and numerous posts on <a href="https://groups.google.com/a/chromium.org/forum/#!msg/headless-dev/qqbZVZ2IwEw/XMKlEMP3EQAJ">headless-dev</a> mailing list discussing all relevant issues, quirks and pitfalls.</p><p>The journey is certainly not for the faint of heart. Here’s a gist of things you need to be ready for.</p><h3>Build Chrome from Source</h3><p>First of all, stock builds won’t work on Lambda, because:</p><ul><li>you need to build it on AWS Linux image and</li><li>you need to do some hacks in Chromium source in order to make it work in an extremely sandbox environment on Lambda.</li></ul><p>Luckily, Marco has your back covered here: you can just grab a custom build from <a href="https://github.com/adieuadieu/serverless-chrome/tree/master/chrome">serverless-chrome</a> repo (which is <em>60.0.3095.0</em> at time of writing).</p><p>However, if you need a more fresh version of Chrome (for example, to use the latest CDP features like Network.setRequestInterception or, say, clipped screenshots), then you need to build it yourself.</p><p>There are few recipes on how to compile Headless Chrome, including the one in <a href="https://github.com/adieuadieu/serverless-chrome/tree/master/chrome">serverless-chrome</a>, so it shouldn’t be a big challenge. Nevertheless, prepare to get your hands dirty with some EC2 and editing a couple of Chrome sources.</p><p>To make life even more interesting, starting with Chrome 62 you’ll also need to include a fresh libnss into your function and wire it with Chrome via LD_LIBRARY_PATH. Some details can be found <a href="https://github.com/universalbasket/aws-lambda-chrome#usage">here</a>.</p><h3>Just when you thought you’re done…</h3><p>Ok, so you managed to run Chrome Headless in Lambda and do some tricks like context.callbackWaitsForEmptyEventLoop = false<em> </em>to make it survive few invocations. Success! 🎉</p><p>Not quite yet. Try to squeeze more than 5 invocations out of the same instance and, if you’re in the same luck as all of us, you will probably get this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wg_TlgGhF_DPG7a0xyA-dw.png" /></figure><p>Chrome dies painfully after exactly 5 invocations! Did you do something wrong? Don’t be afraid, you’re not alone here. This thing is also known as <em>5th-time-crash</em> or <em>crashing-on-5th-invocation </em>(origin <a href="https://github.com/adieuadieu/serverless-chrome/issues/41#issuecomment-322134825">here</a>).</p><p>What’s next then?</p><h3>Kill it, spawn it</h3><p>One obvious strategy is making sure Chrome process is killed on every invocation and to spawn it back.</p><p>While this approach seems more sustainable at first, eventually you will come to realization that each instance is slowly leaking memory due to Chrome process being zombied after each kill and presumably not freeing memory correctly. Depending on your usage scenario, you will be able to squeeze around 60 invocations from a single instance until eventually Chrome will not be able to start anymore. YMMV.</p><p>I won’t post too many details here, but due to inability to forcefully shutdown the instance (and no, process.exit(1) doesn’t help reclaiming the memory and zombie processes!) this strategy won’t get you past some fixed limit of invocations which is still quite high, but definitely not high enough for any serious workload.</p><h3>Cure it!</h3><p>At <a href="http://about.ub.io/">ubio</a> we’re quite determined to making it work: being able to almost instantly scale from 0 to few K jobs is actually a game changer.</p><p>Let’s get back to the 5th-invocation-crash. Funny thing about it: it crashes because Chrome sends IPC messages from renderer process to main process; but if you run Chrome with --single-process flag (which you probably should), then there’s no real IPC happening. Nevertheless, Chrome keeps polling its renderer process (which is supposedly the same process) to see if it’s alive, and if it misses few polls, then it assumes that something is wrong and crashes.</p><p>This obviously doesn’t play nicely with Amazon Kernel Freezer, which suspends Lambda function after each invocation.</p><blockquote>But what if we could just… make it not crash?</blockquote><p>Sounds silly, I know. Still, let’s go <a href="https://chromium.googlesource.com/chromium/src/+/998e890d117912669e3d31ff21b465ee3684966c/content/browser/renderer_host/sandbox_ipc_linux.cc#91">right here</a> and comment out the entire <em>if</em> block which causes the crash.</p><p>Compiling, testing, deploying, running a handful of jobs and — oh my God, it actually works!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*44k3fdJ4OAfMwg9iGdo3WQ.jpeg" /></figure><p>5000 invocations in .5 hour—and counting!</p><p>Assuming this will be interesting to someone, here’s a <a href="https://github.com/universalbasket/aws-lambda-chrome/blob/master/BUILD.md">complete instructions</a> for building Lambda-friendly Chrome including all necessary hacks.</p><p>We also maintain <a href="https://github.com/universalbasket/aws-lambda-chrome">this tiny repo</a> which lets you quickly include latest AWS-friendly Chrome build right into your Node.js app. It only contains the build (Chrome 64.0.3257.0 at time of writing) and appropriate version of libnss. The runtime also sets up the LD_LIBRARY_PATH to glue Chrome and NSS together. If you know your way around <a href="https://github.com/cyrus-and/chrome-remote-interface">chrome-remote-interface</a> or <a href="https://github.com/GoogleChrome/puppeteer">puppeteer</a>, this should be more than enough to get you going.</p><p>Happy automating!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5db989d36040" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-automator/aws-lambda-chrome-headless-5db989d36040">AWS Lambda + Chrome Headless 🌈 🦄</a> was originally published in <a href="https://medium.com/the-automator">The Automator</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ES7 async/await: trouble in paradise]]></title>
            <link>https://medium.com/@borisokunskiy/es7-async-await-trouble-in-paradise-20ff4251fc22?source=rss-5153fbfd2817------2</link>
            <guid isPermaLink="false">https://medium.com/p/20ff4251fc22</guid>
            <category><![CDATA[await]]></category>
            <category><![CDATA[async]]></category>
            <category><![CDATA[es7]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Boris Okunskiy]]></dc:creator>
            <pubDate>Thu, 06 Oct 2016 12:47:35 GMT</pubDate>
            <atom:updated>2016-10-06T16:10:16.932Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi, I’m your average <a href="https://blog.codinghorror.com/the-magpie-developer/">magpie</a> developer. Like all other magpies super-excited about new shiny ECMAScript features I’ve been waiting for async/await to land into JavaScript for almost 3 years now. I’ve been waiting for it like it’s<br>a holy grail, or an answer to life, or an artificial intelligence which will write all my code from now on, or a mix of all these things.</p><p>And this time has <a href="https://github.com/nodejs/promises/issues/4#issuecomment-251025934">come</a>. Yay!</p><p>Well, this only removes some transpiling from the picture. Babel made it possible for more than a year now, so we already grew accustomed to async/await.</p><p>And we learned some pitfalls, because there definitely are some.</p><h3>Forgotten await of eternal regret</h3><p>First thing everyone notices is how you need to train yourself to write <strong><em>await</em></strong> every so often. What happens if you don’t?</p><p>You’re officially screwed. Not only the things that were supposed to run in sequence are now running in parallel, you’re now also have <strong>absolutely no visibility</strong> that it happens by looking in your code.</p><p>And yeah, your errors are now swallowed (luckily, all runtimes kindly warn you about unhandled rejections). But anyway, good luck finding it.</p><p>The easiness of accidentally forgetting <strong><em>await</em></strong> is particularly disturbing.<br>But hey, it’s roughly the same as forgetting to <strong><em>return</em></strong> a promise inside <strong><em>.then</em></strong>, right?</p><h3>Promise of no return</h3><p>This one has struck me the other night. I woke up in cold sweat and ran to my laptop to confirm/infirm my worst nightmare.</p><blockquote><em>Is there any difference between </em><strong>return promise</strong><em> and </em><strong>return await promise </strong><em>inside </em><strong>async function</strong><em>? Can they be used interchangeably?</em></blockquote><p>I used to think that there is absolutely no semantic differences between the two, with following reasoning: since every async function returns a Promise, it should be irrelevant from caller’s point of view whether execution pauses inside a function or not — as long as it resolves with our value.</p><p>What if I told you™ that they are NOT equivalent?</p><p>Consider this (or actually try it with Chrome 53):</p><pre><strong>async function </strong>asyncHello() {<br>    <strong>try </strong>{<br>        <strong>return </strong>asyncHi();<br>    } <strong>catch </strong>(e) {<br>        console.log(&#39;Caught you&#39;, e);<br>    }<br>}<br><br><strong>async function </strong>asyncHi() {<br>    <strong>throw new </strong>Error(&#39;Bug!&#39;);<br>}</pre><p>Now call it. WTF just happened? Where’s my “Caught you” in console?</p><p>Ok, now try again with <strong><em>return await asyncHi()</em></strong>. Makes sense now?</p><p>Now look at the code again (the one without <strong><em>await</em></strong>). I think it works pretty much as expected, don’t you? And it <em>should</em> work like that.</p><p>Don’t let these fancy new keywords trick you: there is no free lunch, asynchronous code is still asynchronous, async/await is not “magical”, <br>it does not liberate you from understanding Promises and mastering your Promise-fu.</p><p>Oh, good. Looks like I still need to reason about my code (kinda decreases possibility of being replaced by AI in near future).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=20ff4251fc22" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>