Random errors when append with another Documents html() #2280
Replies: 4 comments
-
I solved the errors by creating a static synchronized helper that is the only place where we call parse or connect functions in Jsoup. A non exhaustive set of calls:
Now looking to find all parts of Jsoup that share the same memory and that must be included in this function to make it thread safe. |
Beta Was this translation helpful? Give feedback.
-
Hi there, This was due to the Fixed by #2281 by cloning the Parser when the Document is cloned. BTW you could probably make your implementation more efficient by changing:
to
Which will save a HTML serialize and subsequent parse on every request (and also would work around this issue, as the parser is not called). |
Beta Was this translation helpful? Give feedback.
-
Oh excellent, it was a bug! Thanks for the tip on appendChild. |
Beta Was this translation helpful? Give feedback.
-
We are using jsoup in an HttpServlet and replacing a div with the body from a file we get from the filesystem.
All works well, until we get random errors that seem to be related to multiple simultaneous requests on the servlet.
We use the following code to replace the content of a div of a Document object with the body of another Document object:
When doing remoteTemplateMainContent.append(with.body().html()), there are about 30 different errors that are randomly thrown (by the jsoup parser) with some examples:
Cannot invoke "java.util.ArrayList.isEmpty()" because "this.stack" is null
java.lang.ArrayIndexOutOfBoundsException: arraycopy: length -7 is negative
org.jsoup.helper.ValidationException: String must not be empty
org.jsoup.helper.ValidationException: Object must not be null
java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "" is null
I believe it is the result of with.body().html() that is being changed by another request thread while the call to remoteTemplateMainContent.append is in progress.
These errors seem to have happened in jsoup version 1.18.1 but only once in a while, but in the latest version (1.18.3) it is happening often. At least we now know where they were coming from but preventing them is now our goal.
What are we doing wrong?
Beta Was this translation helpful? Give feedback.
All reactions