Skip to main content
medium5-10 min34% solve rate

The Memory Leak That Passed Code Review

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

Node.jsMemoryPerformance

The Code

This code passed code review and works in development. But it has a bug that will cause problems in production.

server.js
1const http = require('http');
2const fs = require('fs');
3
4const server = http.createServer((req, res) => {
5 const filePath = './large-file.pdf';
6
7 // Stream the file to the response
8 const stream = fs.createReadStream(filePath);
9 stream.pipe(res);
10
11 // Log when complete
12 stream.on('end', () => {
13 console.log('File sent successfully');
14 });
15});
16
17server.listen(3000, () => {
18 console.log('Server running on port 3000');
19});
20
21// This code passed code review.
22// It will crash your server under load.
23// Can you spot why?

Need a Hint?

0 of 3 revealed
Hint 1 locked
Hint 2 locked
Hint 3 locked

Ready for the Solution?

Enter your email to get the full solution with explanation, the fix, and why this bug is so common.