Skip to content

Commit 3fb1fea

Browse files
javachemeta-codesync[bot]
authored andcommitted
Evict Metro bundle graphs after each Fantom test to prevent OOM (#56514)
Summary: Pull Request resolved: #56514 Changelog: [Internal] Reviewed By: rubennorte Differential Revision: D101652820 fbshipit-source-id: f7608fbf92a8fd2928434e74ad03bfc01b318d20
1 parent 65c561e commit 3fb1fea

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

private/react-native-fantom/runner/bundling.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export async function createBundle(options: BundleOptions): Promise<void> {
2828
let lastBundleResult;
2929
let lastBundleError;
3030

31+
const bundleURL = getBundleURL(options);
32+
3133
// Retry in case Metro hasn't seen the changes in the filesystem yet.
3234
// TODO(T231910841): Remove this when Metro fixes consistency issues when resolving HTTP requests.
3335
let attemps = 0;
@@ -40,7 +42,7 @@ export async function createBundle(options: BundleOptions): Promise<void> {
4042
lastBundleResult = null;
4143

4244
try {
43-
lastBundleResult = await fetch(getBundleURL(options));
45+
lastBundleResult = await fetch(bundleURL);
4446
} catch (e) {
4547
lastBundleError = e;
4648
}
@@ -72,6 +74,15 @@ export async function createBundle(options: BundleOptions): Promise<void> {
7274
await lastBundleResult.text(),
7375
'utf8',
7476
);
77+
78+
// Each test uses a unique entrypoint, so the bundle graph will never be
79+
// requested again. Send DELETE to evict Metro's cached dependency graph
80+
// and delta calculator for this bundle, freeing the memory.
81+
try {
82+
await fetch(bundleURL, {method: 'DELETE'});
83+
} catch {
84+
// Best-effort cleanup — don't fail the test if eviction fails.
85+
}
7586
}
7687

7788
export async function createSourceMap(options: BundleOptions): Promise<void> {

private/react-native-fantom/runner/global-setup/globalTeardown.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type MetroServer = NonNullable<RunServerResult?.['httpServer']>;
1414

1515
declare var __FANTOM_METRO_SERVER__: ?RunServerResult;
1616

17+
const SHUTDOWN_TIMEOUT_MS = 5_000;
18+
1719
function getMetroServer(): ?MetroServer {
1820
return typeof __FANTOM_METRO_SERVER__ !== 'undefined' &&
1921
__FANTOM_METRO_SERVER__ != null
@@ -32,7 +34,11 @@ export default async function globalTeardown(
3234
}
3335

3436
async function stopMetroServer(metroServer: MetroServer): Promise<void> {
35-
return new Promise((resolve, reject) => {
37+
if (!metroServer.listening) {
38+
return;
39+
}
40+
41+
const closed = new Promise<void>((resolve, reject) => {
3642
metroServer.close(error => {
3743
if (error) {
3844
reject(error);
@@ -41,4 +47,21 @@ async function stopMetroServer(metroServer: MetroServer): Promise<void> {
4147
}
4248
});
4349
});
50+
51+
// $FlowFixMe[method-unbinding] Node 18.2+ API
52+
if (typeof metroServer.closeIdleConnections === 'function') {
53+
metroServer.closeIdleConnections();
54+
}
55+
56+
const timeout = new Promise<void>(resolve => {
57+
setTimeout(() => {
58+
// $FlowFixMe[method-unbinding] Node 18.2+ API
59+
if (typeof metroServer.closeAllConnections === 'function') {
60+
metroServer.closeAllConnections();
61+
}
62+
resolve();
63+
}, SHUTDOWN_TIMEOUT_MS);
64+
});
65+
66+
await Promise.race([closed, timeout]);
4467
}

0 commit comments

Comments
 (0)