July 24th, 2024

The Process That Kept Dying: A memory leak murder mystery (node)

An investigation into a recurring 502 Bad Gateway error on a crowdfunding site revealed a memory leak caused by Moment.js. Updating the library resolved the issue, highlighting debugging challenges.

Read original articleLink Icon
The Process That Kept Dying: A memory leak murder mystery (node)

The article narrates the investigation of a recurring 502 Bad Gateway error affecting a crowdfunding site, led by an SRE named Charlie and a detective figure. The detective begins by analyzing the situation, noting that the errors have been occurring intermittently since June, primarily linked to the Next.js application. Initial suspicions fall on PM2, a process manager for Node.js, which was found to be restarting processes due to memory limits. Despite adjusting the PM2 configuration, the errors persisted, indicating a deeper issue—a memory leak in the application.

The detective outlines a systematic approach to identify the leak, involving debugging, taking heap snapshots, and load testing. After several attempts, the detective discovers that Moment.js, a date manipulation library, was responsible for the memory leak due to calls to moment.updateLocale. An update from version 2.24.0 to 2.29.4 of Moment.js resolves the issue, eliminating the 502 errors.

The narrative emphasizes the challenges of debugging memory leaks and the importance of thorough investigation and systematic troubleshooting in software development. The case concludes with a sense of relief as the immediate problem is resolved, but the detective acknowledges that more challenges lie ahead in the ever-evolving tech landscape.

Link Icon 6 comments
By @chmod775 - 6 months
The site is broken right now, but I read this when it was first submitted some time ago.

Contrary to what the title would suggest, it's about finding a mundane JS memory leak in moment.js by attaching the chrome inspector to node. There's no out-of-the-ordinary tale here and there's certainly little mystery.

The article might be useful if you've never done it before and need some pointers.

By @oneepic - 6 months
I'd like to read this article but keep getting ERR_TOO_MANY_REDIRECTS. Sorry, your site seems broken.
By @johnnyapol - 6 months
Is anyone else getting a redirect loop when trying the link? At first I was thinking it had to do with my Firefox settings to force HTTPS but even just:

curl -L lukedeniston.com/memory-leak-mystery

> curl: (47) Maximum (50) redirects followed

isn't working.

By @throw156754228 - 6 months
>The NODE_OPTIONS were only applying to the yarn process, not the underlying Next.js server that was being invoked by yarn.

Doesn't make sense. All child processes should get that env var.

By @Charon77 - 6 months
In my team we actually had to do performance test every time we update our libraries, and never auto update.