Skip to content

Commit

Permalink
Fix memory leaks with undici introduction (#249)
Browse files Browse the repository at this point in the history
* Turn single fetch back on

* Fetch only HEAD in resources.server

* Consume all fetch bodies
  • Loading branch information
brookslybrand authored Apr 4, 2024
1 parent 7f3a5b7 commit 1da530e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
8 changes: 8 additions & 0 deletions app/lib/conf2023.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export async function getSpeakers(
},
});
if (!fetched.ok) {
// Need to consume for undici since it won't garbage collect
await fetched.text();
throw new Error(
"Error fetching speakers, responded with status: " + fetched.status,
);
Expand Down Expand Up @@ -124,6 +126,8 @@ export async function getConfSessions(
},
});
if (!fetched.ok) {
// Need to consume for undici since it won't garbage collect
await fetched.text();
throw new Error(
"Error fetching speakers, responded with status: " + fetched.status,
);
Expand Down Expand Up @@ -187,6 +191,8 @@ export async function getSchedules(
getSpeakers(),
]);
if (!fetched.ok) {
// Need to consume for undici since it won't garbage collect
await fetched.text();
throw new Error(
"Error fetching schedule, responded with status: " + fetched.status,
);
Expand Down Expand Up @@ -460,6 +466,8 @@ async function fetchNaiveStaleWhileRevalidate(
throw err;
}
if (res.status === 504) {
// Need to consume for undici since it won't garbage collect
await res.text();
return fetchWithForceCache();
}

Expand Down
6 changes: 5 additions & 1 deletion app/lib/gh-docs/repo-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export async function getRepoContent(
new URL(pathname, "https://raw.githubusercontent.com/").href,
{ headers: { "User-Agent": `docs:${owner}/${repo}` } },
);
if (!response.ok) return null;
if (!response.ok) {
// Need to consume for undici since it won't garbage collect
await response.text();
return null;
}
return response.text();
}

Expand Down
4 changes: 3 additions & 1 deletion app/lib/resources.server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ async function getSponsorUrl(owner: string) {
let sponsorUrl = `${GITHUB_URL}/sponsors/${owner}`;

try {
let response = await fetch(sponsorUrl);
// We don't need the body, just need to know if it's redirected
// method: "HEAD" removes the need for garbage collection: https://github.com/nodejs/undici?tab=readme-ov-file#garbage-collection
let response = await fetch(sponsorUrl, { method: "HEAD" });
return !response.redirected ? sponsorUrl : undefined;
} catch (e) {
console.error("Failed to fetch sponsor url for", owner);
Expand Down
2 changes: 1 addition & 1 deletion vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default defineConfig({
arraybuffer(),
remix({
future: {
unstable_singleFetch: false,
unstable_singleFetch: true,
},
}),
],
Expand Down

0 comments on commit 1da530e

Please sign in to comment.