Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown error in XPath. #385

Closed
mateuszskorupa96 opened this issue Sep 23, 2021 · 50 comments
Closed

Unknown error in XPath. #385

mateuszskorupa96 opened this issue Sep 23, 2021 · 50 comments

Comments

@mateuszskorupa96
Copy link

Caused by: javax.xml.transform.TransformerException: Unknown error in XPath.
at com.gargoylesoftware.htmlunit.html.xpath.XPathAdapter.execute(XPathAdapter.java:188)
at com.gargoylesoftware.htmlunit.html.xpath.XPathUtils.evaluateXPath(XPathUtils.java:145)
at com.gargoylesoftware.htmlunit.html.xpath.XPathUtils.getByXPath(XPathUtils.java:77)
... 51 more
Caused by: java.lang.NullPointerException
at org.apache.xml.dtm.ref.dom2dtm.DOM2DTM.nextNode(DOM2DTM.java:390)
at org.apache.xml.dtm.ref.DTMDefaultBase._nextsib(DTMDefaultBase.java:565)
at org.apache.xml.dtm.ref.DTMDefaultBase.getNextSibling(DTMDefaultBase.java:1142)
at org.apache.xml.dtm.ref.DTMDefaultBaseTraversers$ChildTraverser.next(DTMDefaultBaseTraversers.java:463)
at org.apache.xpath.axes.AxesWalker.getNextNode(AxesWalker.java:335)
at org.apache.xpath.axes.AxesWalker.nextNode(AxesWalker.java:363)
at org.apache.xpath.axes.WalkingIterator.nextNode(WalkingIterator.java:195)
at org.apache.xpath.axes.NodeSequence.nextNode(NodeSequence.java:335)
at org.apache.xpath.axes.NodeSequence.runTo(NodeSequence.java:494)
at org.apache.xpath.axes.NodeSequence.setRoot(NodeSequence.java:270)
at org.apache.xpath.axes.LocPathIterator.execute(LocPathIterator.java:212)
at com.gargoylesoftware.htmlunit.html.xpath.XPathAdapter.execute(XPathAdapter.java:167)
... 53 more

We are encountering the aforementioned issue randomly. Most of the time it works fine. We are assuming that this is caused by the fact that XPath object is not designed for multithreading access. I have tried searching for information about this issue however I did not find any solution/workaround, is there any way to fix this?

@rbri
Copy link
Member

rbri commented Sep 23, 2021

Sounds reasonable but maybe something else.... https://sourceforge.net/p/htmlunit/mailman/message/23898002/

@mateuszskorupa96
Copy link
Author

I saw this thread, but we already have a higher version of the HTMLunit (v.2.29) and there the suggestion is to use 2.7 which is so much older.

@rbri
Copy link
Member

rbri commented Nov 17, 2021

the code looks like this (xalan)

next = pos.getNextSibling();

pos can't be null here because it is used before and a local var.

Can you please check your stack trace - maybe the NPE is inside getNextSibling?

@jometzner
Copy link

I have the same issue in the latest version 2.56.0. The stacktrace I'm seeing is:

Caused by: javax.xml.transform.TransformerException: Unknown error in XPath.
        at com.gargoylesoftware.htmlunit.html.xpath.XPathAdapter.execute(XPathAdapter.java:235)
        at com.gargoylesoftware.htmlunit.html.xpath.XPathHelper.evaluateXPath(XPathHelper.java:137)
        at com.gargoylesoftware.htmlunit.html.xpath.XPathHelper.getByXPath(XPathHelper.java:71)
        ... 102 more
Caused by: java.lang.NullPointerException
        at org.apache.xml.dtm.ref.dom2dtm.DOM2DTM.addNode(DOM2DTM.java:215)
        at org.apache.xml.dtm.ref.dom2dtm.DOM2DTM.nextNode(DOM2DTM.java:542)
        at org.apache.xml.dtm.ref.DTMDefaultBase._nextsib(DTMDefaultBase.java:565)
        at org.apache.xml.dtm.ref.DTMDefaultBase.getNextSibling(DTMDefaultBase.java:1142)
        at org.apache.xml.dtm.ref.DTMDefaultBaseTraversers$ChildTraverser.next(DTMDefaultBaseTraversers.java:463)
        at org.apache.xpath.axes.AxesWalker.getNextNode(AxesWalker.java:335)
        at org.apache.xpath.axes.AxesWalker.nextNode(AxesWalker.java:363)
        at org.apache.xpath.axes.WalkingIterator.nextNode(WalkingIterator.java:195)
        at org.apache.xpath.axes.NodeSequence.nextNode(NodeSequence.java:335)
        at org.apache.xpath.axes.NodeSequence.runTo(NodeSequence.java:494)
        at org.apache.xpath.axes.NodeSequence.setRoot(NodeSequence.java:270)
        at org.apache.xpath.axes.LocPathIterator.execute(LocPathIterator.java:212)
        at com.gargoylesoftware.htmlunit.html.xpath.XPathAdapter.execute(XPathAdapter.java:214)
        ... 104 more

Need to have a look at the xalan code base.

@Pldi23
Copy link

Pldi23 commented Jul 1, 2022

We are encountering the same issue intermittently with exactly the same stacktrace (HTMLunit v.2.62.0), are there any workaround/solution?

@Pldi23
Copy link

Pldi23 commented Jul 1, 2022

the code looks like this (xalan)

next = pos.getNextSibling();

pos can't be null here because it is used before and a local var.

Can you please check your stack trace - maybe the NPE is inside getNextSibling?

In our case pos = null what is triggered the error stacktrace, the issue is flaky.

@rbri
Copy link
Member

rbri commented Jul 1, 2022

@Pldi23

org.apache.xml.dtm.ref.dom2dtm.DOM2DTM.addNode(DOM2DTM.java:215)

looks here like this

type = node.getNodeType();

Can you please post your stack trace.
Do you have xalan-2.7.2?

@Pldi23
Copy link

Pldi23 commented Jul 1, 2022

@rbri

Can you please post your stack trace.

java.lang.RuntimeException: Could not retrieve XPath >//input[lower-case(@type)='radio' and @name='tab-group-1656481419909']< on HtmlPage(http://localhost:42685/jenkins/)@398295463
	at com.gargoylesoftware.htmlunit.html.xpath.XPathHelper.getByXPath(XPathHelper.java:93)
	at com.gargoylesoftware.htmlunit.html.DomNode.getByXPath(DomNode.java:1584)
	at com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput.setCheckedForPage(HtmlRadioButtonInput.java:181)
	at com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput.setChecked(HtmlRadioButtonInput.java:139)
	at com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput.onAddedToPage(HtmlRadioButtonInput.java:279)
	at com.gargoylesoftware.htmlunit.html.HtmlPage.notifyNodeAdded(HtmlPage.java:1771)
	at com.gargoylesoftware.htmlunit.html.DomNode.fireAddition(DomNode.java:1060)
	at com.gargoylesoftware.htmlunit.html.DomNode.appendChild(DomNode.java:955)
	at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.appendChild(HtmlUnitNekoDOMBuilder.java:791)
	at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.addNodeToRightParent(HtmlUnitNekoDOMBuilder.java:475)
	at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.startElement(HtmlUnitNekoDOMBuilder.java:359)
	at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
	at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.startElement(HtmlUnitNekoDOMBuilder.java:291)
	at org.apache.xerces.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
	at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.startElement(HTMLTagBalancer.java:812)
	at net.sourceforge.htmlunit.cyberneko.filters.DefaultFilter.startElement(DefaultFilter.java:140)
	at net.sourceforge.htmlunit.cyberneko.filters.NamespaceBinder.startElement(NamespaceBinder.java:278)
	at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scanStartElement(HTMLScanner.java:2811)
	at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2131)
	at net.sourceforge.htmlunit.cyberneko.HTMLScanner.scanDocument(HTMLScanner.java:937)
	at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:443)
	at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:394)
	at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
	at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.parse(HtmlUnitNekoDOMBuilder.java:768)
	at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoHtmlParser.parse(HtmlUnitNekoHtmlParser.java:204)
	at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:298)
	at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:218)
	at org.jvnet.hudson.test.HudsonPageCreator.createPage(HudsonPageCreator.java:54)
	at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:688)
	at com.gargoylesoftware.htmlunit.WebClient.loadDownloadedResponses(WebClient.java:2547)
	at com.gargoylesoftware.htmlunit.html.HtmlFormUtil.submit(HtmlFormUtil.java:82)
	at org.jvnet.hudson.test.JenkinsRule$WebClient.login(JenkinsRule.java:2463)
	at org.jvnet.hudson.test.JenkinsRule$WebClient.login(JenkinsRule.java:2329)
	at org.jvnet.hudson.test.JenkinsRule$WebClient.login(JenkinsRule.java:2475)
	at com.cloudbees.jenkins.plugins.bitbucket.Security2033Test.doClearWhenInvokedUsingGetMethodThenResourceNotFound(Security2033Test.java:157)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.jvnet.hudson.test.JenkinsRule$1.evaluate(JenkinsRule.java:606)
	at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299)
	at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: javax.xml.transform.TransformerException: Unknown error in XPath.
	at com.gargoylesoftware.htmlunit.html.xpath.XPathAdapter.execute(XPathAdapter.java:235)
	at com.gargoylesoftware.htmlunit.html.xpath.XPathHelper.evaluateXPath(XPathHelper.java:137)
	at com.gargoylesoftware.htmlunit.html.xpath.XPathHelper.getByXPath(XPathHelper.java:71)
	... 48 more
Caused by: java.lang.NullPointerException
	at org.apache.xml.dtm.ref.dom2dtm.DOM2DTM.nextNode(DOM2DTM.java:390)
	at org.apache.xml.dtm.ref.DTMDefaultBase._nextsib(DTMDefaultBase.java:565)
	at org.apache.xml.dtm.ref.DTMDefaultBase.getNextSibling(DTMDefaultBase.java:1142)
	at org.apache.xml.dtm.ref.DTMDefaultBaseTraversers$ChildTraverser.next(DTMDefaultBaseTraversers.java:463)
	at org.apache.xpath.axes.AxesWalker.getNextNode(AxesWalker.java:335)
	at org.apache.xpath.axes.AxesWalker.nextNode(AxesWalker.java:363)
	at org.apache.xpath.axes.WalkingIterator.nextNode(WalkingIterator.java:195)
	at org.apache.xpath.axes.NodeSequence.nextNode(NodeSequence.java:335)
	at org.apache.xpath.axes.NodeSequence.runTo(NodeSequence.java:494)
	at org.apache.xpath.axes.NodeSequence.setRoot(NodeSequence.java:270)
	at org.apache.xpath.axes.LocPathIterator.execute(LocPathIterator.java:212)
	at com.gargoylesoftware.htmlunit.html.xpath.XPathAdapter.execute(XPathAdapter.java:214)
	... 50 more

Do you have xalan-2.7.2?

yes, we're using xalan-2.7.2

@rbri
Copy link
Member

rbri commented Jul 1, 2022

Ah ok, the location is

org.apache.xml.dtm.ref.dom2dtm.DOM2DTM.nextNode(DOM2DTM.java:390)

Will think about it again - maybe we can get rid of xalan at all.
PR's are welcome....

@rbri
Copy link
Member

rbri commented Jul 1, 2022

@Pldi23 can imagine two possible scenarios

  • your code (the XPath selection) works in parallel to some js code that manipulates the dom and the xpath engine works on some dom node in an intermediate state
  • there is some code that produces a corrupt dom node that leads to this error

What do you think?

@rbri
Copy link
Member

rbri commented Jul 2, 2022

@Pldi23 can you debug the problem?

@Pldi23
Copy link

Pldi23 commented Jul 4, 2022

@Pldi23 can imagine two possible scenarios

your code (the XPath selection) works in parallel to some js code that manipulates the dom and the xpath engine works on some dom node in an intermediate state
there is some code that produces a corrupt dom node that leads to this error
What do you think?

Hard to say, I'll try to debug, but the issue happens very rarely and there no obvious way how to make it more reproducible.

@rbri
Copy link
Member

rbri commented Jul 4, 2022

I'll try to debug, but the issue happens very rarely and there no obvious way how to make it more reproducible.

I understand, but without having more info it is more or less luck to find this.
If i can make a xalan version with some debug code inside - will you be able to use this?

@Pldi23
Copy link

Pldi23 commented Jul 4, 2022

Hm, sounds good, I'm not sure if we could update somehow our ci infrastructure to use it, but at least I could use it locally I guess.
(By the way, you could also try to run one of our open source tests for example this one bitbucket-branch-source flaky test. It is flaky here on a simple login step when accessing DOM)

@Pldi23
Copy link

Pldi23 commented Jul 4, 2022

Ok I caught it, this is what I see on a debug right now (didn't analyze it yet)
Screenshot 2022-07-04 at 10 25 23

@Pldi23
Copy link

Pldi23 commented Jul 4, 2022

Just as assumption, could there be a race condition between 2 getByXPath calls?

@rbri
Copy link
Member

rbri commented Jul 4, 2022

Just as assumption, could there be a race condition between 2 getByXPath calls?

I think not, they are processed in sequence and also the results are copied into list.

@rbri
Copy link
Member

rbri commented Jul 4, 2022

From your screenshot it looks like some code is executed during page parsing; usually a script tag.
I still guess xalan finds an DomObject still in initialization (has no parent set).

My guess is more code like com.gargoylesoftware.htmlunit.html.DomNode.basicInsertBefore(DomNode) where the node is already attached to the dom before e.g. the parent is set.

If you like i can try to reorder some of this and make a new snapshot build.

@Pldi23
Copy link

Pldi23 commented Jul 4, 2022

If you like i can try to reorder some of this and make a new snapshot build.

If you have fix idea you could also make changes in a draft PR for example and I'll build it and substitute jar locally and setup a our test for a thousands run for the night.

@rbri
Copy link
Member

rbri commented Jul 4, 2022

a new snapshot build 2.63.0-SNAPSHOT is available - let's cross our fingers

@Pldi23
Copy link

Pldi23 commented Jul 5, 2022

@rbri I ran our test with a your new build 1500 times and it failed again on 1497 attempt. (As I see your fix improve the flakiness, because statistically before your fix failures happened ~ 1 per 250 attempts, but didn't solve it completely)

@rbri
Copy link
Member

rbri commented Jul 5, 2022

@Pldi23 you mean it failed only for 3 runs out of the 1500 ones?

Let me think a bit more about this - but for a first wild guess the results are not that bad :-D

@Pldi23
Copy link

Pldi23 commented Jul 5, 2022

I mean it failed once in 1500 runs

@rbri
Copy link
Member

rbri commented Jul 5, 2022

@Pldi23 and if you run it only 1496 times :-D

Will check the code - maybe i can spot more locations to improve

@rbri
Copy link
Member

rbri commented Jul 5, 2022

@Pldi23 made another snapshot build...

@Pldi23
Copy link

Pldi23 commented Jul 5, 2022

@rbri Thanks, with the same name 2.63.0-SNAPSHOT or other?

@rbri
Copy link
Member

rbri commented Jul 5, 2022

@Pldi23 yes same name

@Pldi23
Copy link

Pldi23 commented Jul 5, 2022

Could you please check, (perhaps I'm paranoying ;)), but I don't see your latest commit in 2.63.0-SNAPSHOT which I just downloaded.
Screenshot 2022-07-05 at 12 56 56

@rbri
Copy link
Member

rbri commented Jul 5, 2022

Really strange but it looks you are right.
Have updated the snapshot again - please check

@Pldi23
Copy link

Pldi23 commented Jul 5, 2022

Checked again, weird, but latest commit are still not there.

@rbri
Copy link
Member

rbri commented Jul 5, 2022

@Pldi23
Copy link

Pldi23 commented Jul 5, 2022

Thank you @rbri , you are right (for some reason maven reloaded htmlunit-2.63.0-20220704.141031-4 every time). Now I get latest build and I'll get back to you after next 1500 runs :)

@rbri
Copy link
Member

rbri commented Jul 5, 2022

for maven the -U switch helps here but who knows what all the proxies and content delivery networks out there are doing

@Pldi23
Copy link

Pldi23 commented Jul 5, 2022

@rbri Oh, unfortunately I'm observing a much more failures in this batch of runs, approximately 1 per 100 fails.

@rbri
Copy link
Member

rbri commented Jul 5, 2022

@Pldi23 no idea why the results are bad based on the last change - maybe the problem depends on something else.
Found another possible reason for a broken DOM tree - you can try the new snapshot.

@rbri
Copy link
Member

rbri commented Jul 5, 2022

@Pldi23 have reverted the DomElement changes and made a new snapshot build

@rbri
Copy link
Member

rbri commented Jul 5, 2022

have another idea to reduce the problem.... working on it

@rbri
Copy link
Member

rbri commented Jul 6, 2022

Good morning,
after the ci has passed i made a new snapshot build.

From your stack trace we can see the xpath call (from HtmlRadioButton) is done during the parsing of the page. Usually this is the time where the dom is in manipulation and maybe some nodes are not in a consistent state. Because of this i think using XPath inside the HtmlUnit implementation is a bad idea and i have replaced the calls.

I expect one of two possible results

  • it solves your problem - i outside of tons of new github stars from your whole team i will be your hero of the day :-D or
  • the problem is still there - but now with a different stack trace. In this case i'm hopefully able to make the code more robust (because we no longer use external xpath code for finding).

@Pldi23 waiting for your results ....

@Pldi23
Copy link

Pldi23 commented Jul 6, 2022

Hi @rbri , unfortunately my local night runs still fails (5 times in 1500 runs).
I see your new commit (seems promising, because I also think that the issue typical to HtmlRadioButton and does not apply to other elements, but who knows...) and morning build, I'll try that patch and rerun soonish.
Thanks for being so persistent, we appreciate it!

@Pldi23
Copy link

Pldi23 commented Jul 6, 2022

@rbri good news, no failures in 1500 runs with latest build 🎉🎉🎉! You're a hero of the day!

@rbri
Copy link
Member

rbri commented Jul 6, 2022

before starting the celebration i think you have to do some more tuns to be sure - maybe it was only luck this time

@Pldi23
Copy link

Pldi23 commented Jul 6, 2022

Sure, I setup more runs

@rbri
Copy link
Member

rbri commented Jul 6, 2022

btw any change in terms of performance?

@Pldi23
Copy link

Pldi23 commented Jul 6, 2022

Hard to measure (every test running ~8-10 sec and depends of how much load my PC currently and I don't have any tools like dynatrace connected locally) but visually looking on timing I would say performance didn't get any worse

@rbri
Copy link
Member

rbri commented Jul 6, 2022

As always i had some hope the changes making the monster faster :-)
Still everything stable and green?

@Pldi23
Copy link

Pldi23 commented Jul 6, 2022

based on my code review of your changes (with my limited knowledges of your library) performance should be improved :) And yes, test is still consistently green!

@rbri
Copy link
Member

rbri commented Jul 6, 2022

Ok time to switch of the computer and think about the next steps - do you need a release of this?

@Pldi23
Copy link

Pldi23 commented Jul 7, 2022

I don't want to influence your release strategy, but yes, we need the fix in the nearest release.

@rbri
Copy link
Member

rbri commented Jul 7, 2022

There is no real fixed strategy outside making one nearly every month. Will have a look if i can make one this weekend.

@rbri
Copy link
Member

rbri commented Jul 8, 2022

Will close this now because it seems to be fixed for at least this situation.

Please open a new one if you facing similar problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants