Closed Krzysztof-Nerwinski closed 1 year ago
Hello @Krzysztof-Nerwinski
thanks for this issue.
Ts.ED use a lot of integration test but I haven’t constated that. But it’s totally possible. Memory leaks in express app is easy to create it but complicated to solve it.
I haven’t a lot of experience to found memory leaks. I already tried to optimize the framework as is it possible but it’s very complicated. For this issue I need help.
I’ll be happy to help and give the all details needed around the framework architecture and solve this issue.
I’ll make this issue as top priority!
See yoy
Interesting point. With Koa, Leaks isn't presents: https://github.com/Romakita/tsed-jest-memory-leaks-koa
So the problem is around the PlatformExpress.ts :D
Nice, that narrows it down, thanks!
I didn't have much time last couple of days. I tried debugging it with chrome inspector but no luck for now.
It seems to be related to the runInContext function combined with Express.js here (https://github.com/tsedio/tsed/tree/production/packages/platform/platform-express/src/components/PlatformExpress.ts):
class PlatformExpress {
useContext(): this {
const app = this.getPlatformApplication();
const invoke = createContext(this.injector);
this.injector.logger.debug("Mount app context");
app.use(async (request: any, response: any, next: any) => {
const $ctx = await invoke({request, response});
await $ctx.start();
$ctx.response.getRes().on("finish", () => $ctx.finish());
// if we send response immediately here, there is no memory leaks:
// response.send("hello")
// when it's called, the memory leaks occurs
return runInContext($ctx, next);
// Note:
// - maybe the memory leaks is under the handlers executed by runInContext?
// - runInContext in Koa run handlers also and haven't memory leaks
});
return this;
}
}
Here the equivalent in PlatformKoa (https://github.com/tsedio/tsed/tree/production/packages/platform/platform-koa/src/components/PlatformKoa.ts):
useContext(): this {
const app = this.getPlatformApplication();
const invoke = createContext(this.injector);
const platformExceptions = this.injector.get<PlatformExceptions>(PlatformExceptions);
this.injector.logger.debug("Mount app context");
app.use(async (koaContext: Context, next: Next) => {
const $ctx = await invoke({
request: koaContext.request as any,
response: koaContext.response as any,
koaContext
});
return runInContext($ctx, async () => {
try {
await $ctx.start();
await next();
const status = koaContext.status || 404;
if (status === 404 && !$ctx.isDone()) {
platformExceptions?.resourceNotFound($ctx);
}
} catch (error) {
platformExceptions?.catch(error, $ctx);
} finally {
await $ctx.finish();
}
});
});
return this;
}
In koa, the next function returning a promise will in express next return nothing. So cleaning cleaning resource is more simple in Koa.
Also Ts.ED create circular ref between PlatformContext
/PlatformRequest
/PlatformResponse
(like Koa do).
that's all i've analyzed so far ^^ See you
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Information
First I'd like to thank everyone involved in this very cool framework. The reason I'm createing this issue is because me and my team stumbled on a problem after adding ~500 tests most of them intergration tests.
We're getting the following error in our pipelines:
FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory
.To test if same issue occurs on a new project I've created an unmodofied projectwith
tsed init .
command. Below are the options I've chosen: ? Choose the target platform: Express.js ? Choose the architecture for your project: Ts.ED ? Choose the convention file styling: Ts.ED ? Check the features needed for your project Swagger, Testing, Linter, Bundler ? Choose unit framework Jest ? Choose linter tools framework EsLint ? Choose extra linter tools ? Choose your bundler (Use arrow keys) ❯ BabelAfter installing
components-scan
I've tried runningnode --expose-gc ./node_modules/.bin/jest --logHeapUsage --detect-leaks
without any modification to tests created oninit
.It returns that the default tests are leaking:
Even without
--detect-leaks
you can observe heap size growing with every test that bootstraps Server application.Do you know about this issue and maybe have a solution we could implement?