Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix: prevent memory leak when collecting logs from injected script poll (#4448)

Dmitry Gozmanb9ac9df837d7
diff --git a/src/server/injected/injectedScript.ts b/src/server/injected/injectedScript.ts
index d5c4b50627..e49fb2c94e 100644
--- a/src/server/injected/injectedScript.ts
+++ b/src/server/injected/injectedScript.ts
@@ -183,6 +183,7 @@ export class InjectedScript {
private _runAbortableTask<T>(task: (progess: InjectedScriptProgress) => Promise<T>): InjectedScriptPoll<T> {
let unsentLogs: string[] = [];
let takeNextLogsCallback: ((logs: string[]) => void) | undefined;
+ let taskFinished = false;
const logReady = () => {
if (!takeNextLogsCallback)
return;
@@ -193,7 +194,7 @@ export class InjectedScript {
const takeNextLogs = () => new Promise<string[]>(fulfill => {
takeNextLogsCallback = fulfill;
- if (unsentLogs.length)
+ if (unsentLogs.length || taskFinished)
logReady();
});
@@ -211,9 +212,19 @@ export class InjectedScript {
},
};
+ const result = task(progress);
+
+ // After the task has finished, there should be no more logs.
+ // Release any pending `takeNextLogs` call, and do not block any future ones.
+ // This prevents non-finished protocol evaluation calls and memory leaks.
+ result.finally(() => {
+ taskFinished = true;
+ logReady();
+ });
+
return {
takeNextLogs,
- result: task(progress),
+ result,
cancel: () => { progress.aborted = true; },
takeLastLogs: () => unsentLogs,
};