Closed abhinav-kipper closed 3 years ago
Could it be that Page.evaluate
call from at html2pdf.handlers.PageHandler.lambda$runScriptOnPageLoad$6(PageHandler.java:121)
triggers another load
event in the page and the handler gets called recursively? Can you share the code of PageHandler.java ?
Closing as part of the triage process since it seemed stale or did not get enough upvotes in the past. Please create a new issue with a detailed reproducible or feature request if you still encounter issues.
@abhinav-kipper I finally looked at the code that you shared. The problem is that your onLoad
handler evaluates something in the page that triggers another load event which quickly leads to stackoverflow issue.
...
private void runScriptOnPageLoad() {
page.onLoad(p -> {
...
p.evaluate(script + invokeScriptString);
});
}
...
Note that p.evaluate
is blocking and if it triggers another load
event it will be dispatched above on the same stack. You can easily fix this by using page.waitForLoadState
similar to how you do it in waitForContentLoad
:
...
private void runScriptOnPageLoad() {
page.waitForLoadState();
...
p.evaluate(script + invokeScriptString);
}
...
This should fix stack overflow issue, but still may be racy in case load
event gets dispatched before runScriptOnPageLoad
is called but that'd be a different issue. Please reopen this bug or file a new if the problem persists.
Thanks a lot @yury-s for identifying the issue.
I just need to ask two questions -
Were you also able to identify which piece of code is triggering load event again in p.evaluate() ? I was not able to find anything specific in DomManipulationScript.js .
As per docs, page.waitForLoadState(); waits until load event and if load event is already dispatched it does not wait at all and code can continue. How will then racy condition can arise as p.evaluate() will be called only (once) after the load event?
This resolves when the page reaches a required load state, load by default. The navigation must have been committed when this method is called. If current document has already reached the required state, resolves immediately.
- Were you also able to identify which piece of code is triggering load event again in p.evaluate() ? I was not able to find anything specific in DomManipulationScript.js .
No, sorry, the code snippet is quite long. You can try an bisect it as you are more familiar with that code. I also don't know what the page does and whether DOMManipulationScript.js
may trigger any listeners in it.
2. As per docs, page.waitForLoadState(); waits until load event and if load event is already dispatched it does not wait at all and code can continue. How will then racy condition can arise as p.evaluate() will be called only (once) after the load event?
My statement was inaccurate. Adding for page.onLoad()
is racy as it depends on whether the load event already fired, waitForLoadState
is free of such flaw.
Playwright version
1.12.1
Operating system
Windows
What browsers are you seeing the problem on?
Chromium
Other information
Java version - 8
What happened? / Describe the bug
I am using playwright to navigate to a page and then print pdf. The code works fine for initial requests but after sometime playwright code stops working as it starts throwing stackoverflow exception for every request. If I restart my application, the issue gets resolved.
Why playwright keeps throwing Stackoverflow exception? It keeps recursing over same code again and again until it throws STACKOVERFLOW EXCEPTION
Code snippet to reproduce your bug
No response
Relevant log output