You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Memory Leak on prettyDOM calls while waiting for expect.element #7139
While waiting for expect.element to resolve, prettyDOM will be called on every attempt and its going to send the whole body as dom parameter. This makes memory usage go wild and at some point it goes OOM. Can confirm removing prettyDom usage in following line fixes the issue:
@sheremet-va if you havent started working on it yet, do you mind sharing the approach youd like to take here? i have some free time today and would be nice to have a contribution to the project
EDIT: I think we have a few options
Flag the poll call with an extra parameter to tell when its the last call before timeout and only call prettyDOM in that situation (Im not sure about this one cause it will require some major changes and/or facing possible racing conditions here)
Use .query() instead of .element()here and throw a custom error without calling prettyDOM (also not a fan here since it will mean dropping prettyDOM on expect.element() calls)
I suspect that the memory leak is actually caused by main thread being overwhelmed with the amount of prettyDOM calls, so GC is not able to do its job properly. Maybe moving that code to a web worker or giving some time to breath with old good setTimeout(fn, 0)?
Let me know if im missing something
EDIT 2: I dont think option 3 is easily feasible because of the sync nature of how prettyDOM is used in that scenario.
In other hand, I gave a shot to option 1 locally and it works well. Instead of flagging the last attempt, I just try one last time before rejecting the promise here.
Copy-pasting the hardcoded solution I have locally so you can check:
Flag the poll call with an extra parameter to tell when its the last call before timeout and only call prettyDOM in that situation (Im not sure about this one cause it will require some major changes and/or facing possible racing conditions here)
Activity
tsirlucas commentedon Dec 27, 2024
@sheremet-va if you havent started working on it yet, do you mind sharing the approach youd like to take here? i have some free time today and would be nice to have a contribution to the project
EDIT: I think we have a few options
Flag the poll call with an extra parameter to tell when its the last call before timeout and only call prettyDOM in that situation (Im not sure about this one cause it will require some major changes and/or facing possible racing conditions here)
Use
.query()instead of.element()here and throw a custom error without calling prettyDOM (also not a fan here since it will mean dropping prettyDOM onexpect.element()calls)I suspect that the memory leak is actually caused by main thread being overwhelmed with the amount of prettyDOM calls, so GC is not able to do its job properly. Maybe moving that code to a web worker or giving some time to breath with old good
setTimeout(fn, 0)?Let me know if im missing something
EDIT 2: I dont think option 3 is easily feasible because of the sync nature of how prettyDOM is used in that scenario.
In other hand, I gave a shot to option 1 locally and it works well. Instead of flagging the last attempt, I just try one last time before rejecting the promise here.
Copy-pasting the hardcoded solution I have locally so you can check:
packages/vitest/src/integrations/chai/poll.tsreturn function(...args) { const STACK_TRACE_ERROR = new Error("STACK_TRACE_ERROR"); const promise = () => new Promise((resolve, reject) => { let intervalId; let timeoutId; let lastError; const { setTimeout, clearTimeout } = getSafeTimers(); const check = async () => { try { chai$1.util.flag(assertion, "_name", key); const obj = await fn(); chai$1.util.flag(assertion, "object", obj); resolve(await assertionFunction.call(assertion, ...args)); clearTimeout(intervalId); clearTimeout(timeoutId); } catch (err) { lastError = err; - intervalId = setTimeout(check, interval); + if (!chai$1.util.flag(assertion, "_isLastPollAttempt")) { + intervalId = setTimeout(check, interval); + } } }; timeoutId = setTimeout(async () => { clearTimeout(intervalId); + chai$1.util.flag(assertion, "_isLastPollAttempt", true); + await check(); reject( copyStackTrace$1( new Error(`Matcher did not succeed in ${timeout}ms`, { cause: lastError }), STACK_TRACE_ERROR ) ); }, timeout); check(); }); }packages/browser/src/client/tester/expect-element.tsreturn expect.poll(function element() { if (elementOrLocator instanceof Element || elementOrLocator == null) { return elementOrLocator; } chai.util.flag(this, "_poll.element", true); const isNot = chai.util.flag(this, "negate"); const name = chai.util.flag(this, "_name"); + const isLatPollAttempt = chai.util.flag(this, "_isLastPollAttempt"); if (isNot && name === "toBeInTheDocument") { return elementOrLocator.query(); } + if (isLatPollAttempt) { + return elementOrLocator.element(); + } + + const result = elementOrLocator.query(); + + if (!result) { + throw new Error(`Cannot find element with locator: ${JSON.stringify(elementOrLocator)}`); + } + + return result; }, options);sheremet-va commentedon Dec 31, 2024
that's what I would've done
fix: only use locator.element on last expect.element attempt (fix vit…
fix(browser): only use locator.element on last expect.element attempt (…
chore(deps): update dependency vitest to v3
fix(browser): only use locator.element on last expect.element attempt (…