Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deno.serve Empty Response Memory Leak #27545

Open
is-jonreeves opened this issue Jan 3, 2025 · 0 comments
Open

Deno.serve Empty Response Memory Leak #27545

is-jonreeves opened this issue Jan 3, 2025 · 0 comments
Labels
bug Something isn't working correctly ext/http related to ext/http

Comments

@is-jonreeves
Copy link

Version: Deno 2.1.4
OS: Windows 11 Pro

I was benchmarking Deno with K6 and noticed that some of my endpoints where using more and more memory over time. After some experimenting it seems to be only happening to those that returned an empty body:

// ❌ Empty string response = Memory increases over time
Deno.serve({ port: 3000 }, () => new Response('', { status: 200 }));
// ✅ Space character response = Memory garbage collects correctly
Deno.serve({ port: 3000 }, () => new Response(' ', { status: 200 }));

For convenience, here is an example K6 script to observe the issue:

import http from 'k6/http';

export const options = {
  vus: 50,
  duration: '5m',
};

export default function() {
  http.get('http://127.0.0.1:3000/');
}
@bartlomieju bartlomieju added bug Something isn't working correctly ext/http related to ext/http labels Jan 3, 2025
nathanwhit added a commit to denoland/deno_core that referenced this issue Jan 4, 2025
Ref denoland/deno#27545.

Fixes a leak in deno.serve with an empty response. We would call
`to_string_ptr` with an empty string and then call alloc with a length
of 0 which is UB.

Instead of using `alloc` directly, which has a large number of
invariants to uphold, I've changed to just use `Vec` APIs. That leaves
us with fewer invariants to uphold, handles the tricky cases for us, and
we need to make a `Vec` eventually anyway (since `String` just wraps a
Vec).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly ext/http related to ext/http
Projects
None yet
Development

No branches or pull requests

2 participants