The application experienced intermittent Docker failures due to a memory leak in a Puppeteer-based scraper. The scraper, used to collect bandwidth data, failed frequently because of unreachable routers, causing errors. These failures led to orphaned Chromium processes consuming excessive RAM on the Azure VM. The lack of proper browser closing within the scraper code was the root cause of the memory leak. Specifically, the `browser.close()` function was not guaranteed to execute after failed scrapes. This resulted in Chromium instances accumulating and exhausting the VM's resources. The solution involved using a `try/catch/finally` block to ensure `browser.close()` always executed and adding a null check in the calling function. Implementing the fix dramatically reduced memory usage and eliminated orphaned processes. The key takeaway is to always guarantee Puppeteer browser cleanup with `try/catch/finally`. Developers should also avoid parallel scraping and monitor for Chromium processes. Finally, adding timeouts and null checks further improves code reliability. The provided code also includes a method to immediately clear orphaned Chromium processes and free up memory.
dev.to
dev.to
