-
Notifications
You must be signed in to change notification settings - Fork 212
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
Make DaemonRegistry.getProcessId0
more robust
#612
Conversation
if (pid == null) { | ||
pid = ManagementFactory.getRuntimeMXBean().getName().split("@", 0)[0]; | ||
} | ||
if (pid == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICT this branch was impossible previously (pid
would have been set above), so
maven-mvnd/common/src/main/java/org/mvndaemon/mvnd/common/DaemonRegistry.java
Lines 286 to 288 in 3b10083
int rpid = new Random().nextInt(1 << 16); | |
LOGGER.warn("Unable to determine PID, picked a random number=" + rpid); | |
return rpid; |
common/src/main/java/org/mvndaemon/mvnd/common/DaemonRegistry.java
Outdated
Show resolved
Hide resolved
final Path self = Paths.get("/proc/self"); | ||
if (Files.exists(self)) { | ||
pid = self.toRealPath().getFileName().toString(); | ||
if (Os.current() == Os.LINUX) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally thought if (Os.current() != Os.WINDOWS && Os.current() != Os.MAC)
would be better, because both UNKNOWN or any future expansion of the Os enum are likely to be Unix flavors implementing procfs. But I admit that assumption is rather weak and your proposal isn't actually bad because if ManagementFactory.getRuntimeMXBean().getName().split()
does not work for some UNKNOWN or future Os, then somebody would complain and we can make the condition more specific. Thus 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure. https://en.wikipedia.org/wiki/Procfs claims it was removed from OpenBSD. Anyway IIUC this code can be simplified when dropping Java 8 support? (Or perhaps now, if you use reflection?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, ProcessHandle.current().pid()
would simplify it a lot. I'd vote for using reflection as a fallback only if we have real world reports that the current code does not work on some specific platform.
Thanks for your contribution, @jglick! |
Fixes #608. Tested with Java 11 in non-native mode (from
./dist/target/mvnd-0.7.2-SNAPSHOT-linux-amd64/bin/
) on Linux and Windows 10.