Repository navigation
Conversation
| /** | ||
| * Asks the server to quit and waits for it, forcing it to stop when it does not answer. | ||
| */ | ||
| @Override | ||
| public void close() { | ||
| try { | ||
| if (process.isAlive()) { | ||
| send(Request.newBuilder().setQuit(QuitRequest.getDefaultInstance())); | ||
| toServer.close(); | ||
| if (!process.waitFor(10, TimeUnit.SECONDS)) { | ||
| process.destroyForcibly(); | ||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| process.destroyForcibly(); |
There was a problem hiding this comment.
⚠️ Bug: close() hangs forever when the server never answers the quit request
The javadoc says close() forces the server to stop "when it does not answer", but close() first calls send(quit), and send blocks on Response.parseDelimitedFrom(fromServer) with no timeout. The 10-second waitFor is only reached after a quit response arrives. So if the process is still alive but stuck (deadlocked, busy in a long scan, or stopped writing to stdout), close() and any try-with-resources around the client block forever, and destroyForcibly() is never called. start() has the same problem: a server that never answers hello blocks start() with no way out. Fix: write the quit request and close stdin without waiting for the reply, then use process.waitFor(timeout) as the only wait and call destroyForcibly() when it runs out. Or read the reply on a separate thread with a timeout.
Send quit without waiting for the reply, so that the timed waitFor is the only wait.:
if (process.isAlive()) {
synchronized (this) {
Request.newBuilder().setId(nextId++).setQuit(QuitRequest.getDefaultInstance()).build().writeDelimitedTo(toServer);
toServer.close();
}
if (!process.waitFor(10, TimeUnit.SECONDS)) {
process.destroyForcibly();
}
}
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
97338c2 to
3de11ef
Compare
| public Document getFile(String path, int ahead) throws IOException { | ||
| JsonObject request = command("get-file"); | ||
| request.addProperty("path", path); | ||
| request.addProperty("ahead", ahead); | ||
| return Document.parseFrom(exchange(request).payload); | ||
| } |
There was a problem hiding this comment.
⚠️ Bug: getFile/getTypes call exchange() without the lock send() holds
send is synchronized, which shows the client is meant to be safe across threads. getFile and getTypes call the unsynchronized exchange directly, though, so they skip that lock. If a getFile overlaps a send (for example hasPackage or close) or another getFile, the two can race on nextId, write interleaved bytes to toServer, and each read the other's header and payload from fromServer. The result is id-mismatch failures or a payload parsed as the wrong document. The fix is to make exchange itself synchronized, so all three paths go through one lock.
Put the lock on exchange so every request/response pair is atomic:
private JsonObject send(JsonObject request) throws IOException {
return exchange(request).header;
}
private synchronized Answer exchange(JsonObject request) throws IOException {
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
| if (header.has("payload_bytes")) { | ||
| payload = fromServer.readNBytes(header.get("payload_bytes").getAsInt()); | ||
| } |
There was a problem hiding this comment.
💡 Edge Case: A truncated payload is accepted without any error
InputStream.readNBytes(n) returns fewer than n bytes when the stream hits EOF. If the server crashes or closes stdout partway through a payload, the client never notices. getTypes would return cut-off JSON, and getFile would hand a short buffer to Document.parseFrom. Protobuf accepts input that stops on a field boundary, so the caller can silently get an incomplete Document. The fix is to compare the length of the bytes read with payload_bytes and throw an IOException if they differ.
Fail when fewer bytes arrive than the header announced:
if (header.has("payload_bytes")) {
int expected = header.get("payload_bytes").getAsInt();
payload = fromServer.readNBytes(expected);
if (payload.length != expected) {
throw new IOException("SemSitter server closed its output after " + payload.length + " of " + expected + " payload bytes");
}
}
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
0a1436a to
c4fd850
Compare
c4fd850 to
ae5d84a
Compare
Code Review
|
| Auto-apply | Compact | Unblock |
|
|
|
Was this helpful? React with 👍 / 👎 | Gitar
|




Part 2 of 2 split from #6533. Stacked on #6541.
SemSitterClientdrives one udg-java-server process. It takes the executable path and the project root, checks protocol 2.x, and exposessetSettings,fullGen,getFile,getTypesandhasPackage. Control messages are JSON lines.getFilereturns theDocumentfromanalyzer-wire.getTypesreturns the JSON payload.close()sends quit and force-stops the process if it does not exit.Each client owns its process, so several can run side by side. There is no shared session.
EmbeddedAnalyzeris not carried over.Tests run the client against
FakeServer, a Java stand-in started as a real child process.SemSitterClientE2ETestdrives the real binary with-Dsemsitter.executable=<path>and is skipped otherwise. It passes once the server from SonarSource/semsitter#5821 is built.