Skip to main content
Menu
Challenges/The Memory Leak That Passed Code Review
// challenge_ST

The Memory Leak That Passed Code Review

This Node.js stream implementation looks fine but will crash your server under load.

mediumNode.jsMemoryPerformance5-10 min
Attempts
1,247
Solved
34%
Avg time
7min
Published
2024-01-15
// the code

Here's what passed code review.

A standard pattern. Nothing in this code is obviously wrong. It works perfectly in dev. It works perfectly in staging. It even works in production — for a while.

server.jsjavascript
const http = require('http');
const fs = require('fs');

const server = http.createServer((req, res) => {
  const filePath = './large-file.pdf';

  // Stream the file to the response
  const stream = fs.createReadStream(filePath);
  stream.pipe(res);

  // Log when complete
  stream.on('end', () => {
    console.log('File sent successfully');
  });
});

server.listen(3000, () => {
  console.log('Server running on port 3000');
});

// This code passed code review.
// It will crash your server under load.
// Can you spot why?
// stuck? progressive hints

Reveal hints one at a time.

Each hint nudges you closer without giving it away. Use as many as you need.

hint_01Start here →+
Think about what happens when the client disconnects mid-transfer.
hint_02Need another nudge?+
The stream keeps reading even if nobody is listening.
hint_03Need another nudge?+
What happens to the file descriptor?
// submit your guess
// solution lands in your inbox

Want more code challenges? Subscribe.

New challenge every two weeks. Production bugs that passed review. ~5 min reads.