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

Thread unsafe behavior #1892

Closed
codymikol opened this issue Feb 4, 2023 · 5 comments
Closed

Thread unsafe behavior #1892

codymikol opened this issue Feb 4, 2023 · 5 comments

Comments

@codymikol
Copy link

When trying to parse documents on two different threads an exception is thrown.

I have made a minimal reproduction over here: https://github.com/codymikol/jsoup-thread-unsafe-demo

Here is the exception in question

Exception in thread "main" java.util.NoSuchElementException
	at java.base/java.util.LinkedList.getLast(LinkedList.java:261)
	at org.jsoup.parser.TreeBuilder.currentElement(TreeBuilder.java:58)
	at org.jsoup.parser.HtmlTreeBuilder.insertNode(HtmlTreeBuilder.java:209)
	at org.jsoup.parser.HtmlTreeBuilder.insert(HtmlTreeBuilder.java:167)
	at org.jsoup.parser.HtmlTreeBuilder.insert(HtmlTreeBuilder.java:156)
	at org.jsoup.parser.HtmlTreeBuilderState$6.process(HtmlTreeBuilderState.java:208)
	at org.jsoup.parser.HtmlTreeBuilder.process(HtmlTreeBuilder.java:84)
	at org.jsoup.parser.HtmlTreeBuilderState$6.anythingElse(HtmlTreeBuilderState.java:240)
	at org.jsoup.parser.HtmlTreeBuilderState$6.process(HtmlTreeBuilderState.java:224)
	at org.jsoup.parser.HtmlTreeBuilder.process(HtmlTreeBuilder.java:84)
	at org.jsoup.parser.HtmlTreeBuilderState$4.anythingElse(HtmlTreeBuilderState.java:162)
	at org.jsoup.parser.HtmlTreeBuilderState$4.process(HtmlTreeBuilderState.java:138)
	at org.jsoup.parser.HtmlTreeBuilder.process(HtmlTreeBuilder.java:84)
	at org.jsoup.parser.HtmlTreeBuilderState$3.process(HtmlTreeBuilderState.java:89)
	at org.jsoup.parser.HtmlTreeBuilder.process(HtmlTreeBuilder.java:84)
	at org.jsoup.parser.HtmlTreeBuilderState$2.anythingElse(HtmlTreeBuilderState.java:63)
	at org.jsoup.parser.HtmlTreeBuilderState$2.process(HtmlTreeBuilderState.java:55)
	at org.jsoup.parser.HtmlTreeBuilder.process(HtmlTreeBuilder.java:84)
	at org.jsoup.parser.HtmlTreeBuilderState$1.process(HtmlTreeBuilderState.java:32)
	at org.jsoup.parser.HtmlTreeBuilder.process(HtmlTreeBuilder.java:84)
	at org.jsoup.parser.TreeBuilder.runParser(TreeBuilder.java:48)
	at org.jsoup.parser.TreeBuilder.parse(TreeBuilder.java:41)
	at org.jsoup.parser.HtmlTreeBuilder.parse(HtmlTreeBuilder.java:37)
	at org.jsoup.parser.Parser.parseInput(Parser.java:30)
	at MainKt$main$2$1$1.invokeSuspend(Main.kt:12)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
	Suppressed: java.util.NoSuchElementException
		... 33 more
@jhy
Copy link
Owner

jhy commented Feb 17, 2023

Well, what object are you sharing between threads? Please show the actual code. You should not use the same instance of a Parser object across multiple threads. Either use the parse methods in the Jsoup class, or use the Parser.newInstance() method.

@jhy jhy added the needs-more-info More information is needed from the reporter to progress the issue label Feb 17, 2023
@codymikol
Copy link
Author

I did share the code, would you accept a PR to document that Parser instances are not thread safe?

@codymikol
Copy link
Author

Oh. you're fast, nice 😄

@jhy jhy removed the needs-more-info More information is needed from the reporter to progress the issue label Feb 18, 2023
@zaidusmanali
Copy link

Nice experience

@codymikol
Copy link
Author

I'm glad you enjoyed it

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

3 participants