Scanner not blocking inside virtual thread

Exactly what the title says. Code:
// Main class:
static final Scanner input = new Scanner(System.in);

// psv main:
Thread.newVirtualThread(() -> {
while (true) {
String command = input.nextLine(); // Throws NoSuchElementException instead of blocking
// ...
}
});
// Main class:
static final Scanner input = new Scanner(System.in);

// psv main:
Thread.newVirtualThread(() -> {
while (true) {
String command = input.nextLine(); // Throws NoSuchElementException instead of blocking
// ...
}
});
The above is the only Scannermethod call in the entire program, and that Scanner is the only one that exists as well. I've never had this happen before in all my years of Java, and I'm stumped. I'm on JDK21.
38 Replies
JavaBot
JavaBot6d ago
This post has been reserved for your question.
Hey @Haiku! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st6d ago
Virtual threads are always daemon threads they don't stop the JVM from exiting if you want the program to continue running, you need a normal (non-daemon) thread being active
Haiku
HaikuOP6d ago
No it's not that it keeps running while the program is stopped, it's that it doesn't block the thread and wait for input. It just says "nope, nothing here" and throws instead of waiting for stdin to have something
JavaBot
JavaBot6d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Dioxin
Dioxin6d ago
wasn't able to reproduce on Java 21 Temurin 21.0.5 which distro are you using @Haiku ?
Dioxin
Dioxin6d ago
JavaBot
JavaBot5d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Haiku
HaikuOP5d ago
OpenJDK Yeah I can't reproduce it on temurin either. I guess it's a bug in the distro. That's a first.
dan1st
dan1st5d ago
Can you show your whole program? And what do you mean with it saying "nope, nothing here"? Note that Dioxin's code has an additional while(true) loop at the end after starting the virtual thread (for the reason I mentioned before) Did you maybe create multiple Scanners?
Haiku
HaikuOP5d ago
NoSuchElementException on the scanner when calling input#nextLine
dan1st
dan1st5d ago
Can you show your full code that reproduces it? Ideally a minimal reproducible example
Haiku
HaikuOP5d ago
Nope, only one. And the main thread is chugging away at other tasks while the virtual thread is running. Since it's not reproducible on temurin I do think it's just a JVM bug That is a minrep What I posted
dan1st
dan1st5d ago
So you can reproduce it without anything instead of the // ...?
Haiku
HaikuOP5d ago
Yep
dan1st
dan1st5d ago
and you also never put a .close() anywhere?
Haiku
HaikuOP5d ago
Nope
dan1st
dan1st5d ago
Can you show the full main class and the output of java -version on the JDK where you could reproduce it?
Haiku
HaikuOP5d ago
It closes when the program shuts down, no sooner
dan1st
dan1st5d ago
?
Haiku
HaikuOP5d ago
I was just repeating that point, don't read into it
dan1st
dan1st5d ago
If you shared all the necessary code, the program is allowed to exit right away I think even a NoSuchElementException would in principle be allowed in that case
Haiku
HaikuOP5d ago
Ah yeah good point The while loop is the best way to simulate what's going on, it actually opens a swing window and does stuff there But I've verified that the main thread is still running, not just the event queue
dan1st
dan1st5d ago
Show me a full main class and the output of java -version that reproeudces the issue together
Haiku
HaikuOP5d ago
Sure just give me a few minutes, I'm afk
dan1st
dan1st5d ago
and your code above is not a reproducer because it's startVirtualThread and not newVirtualThread
Haiku
HaikuOP5d ago
Ah good catch
dan1st
dan1st5d ago
Though I wouldn't expect that to happen with normal JDKs
Haiku
HaikuOP5d ago
But honestly I'm realizing that it's an openjdk version I downloaded back when 21 was still new, and my exact same code works on temurin and breaks on openjdk, so I'm fully willing to chalk it up to a jdk bug and move on
dan1st
dan1st5d ago
Temurin is an OpenJDK build - just one of many openjdk/jdk vendors which is why I also asked for the java -version output
Haiku
HaikuOP5d ago
Maybe it's an updated version then idk
dan1st
dan1st5d ago
.
Haiku
HaikuOP5d ago
Alright give me a few minutes then
dan1st
dan1st5d ago
but I don't see it being a bug even if the thing you described happened I don't think there's anything in the specs that doesn't allow closing System.out in the normal shutdown procedure before killing daemons
Haiku
HaikuOP5d ago
Wait, now that I think about it... Ok give me a few minutes to check with an actual tested minrep instead of vaguely theorizing off-hand like an asshole
Haiku
HaikuOP5d ago
Alright, here's the actual error. You were right that the "minrep" I provided wasn't actually reproducible, and I'm sorry about that. I was in a rush to diagnose it since it failed during our test demo less than an hour before presenting. I was also wrong about a couple of my assumptions, the main one being that it was JDK/OS-dependent. It wasn't.
No description
Haiku
HaikuOP5d ago
I did however figure out what the problem was. We were running it through the gradle console. There's no stdin on the gradle console. I guess I thought it was inconsistent because I didn't realize when I absent-mindedly switched from my Application task to the gradle run task Thanks for the help yall
JavaBot
JavaBot5d ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot5d ago
Post Closed
This post has been closed by <@67415933529894912>.

Did you find this page helpful?