Skip to content

SONARJAVA-7339 Add the SemSitter client - #6542

Draft
rombirli wants to merge 4 commits into
rombirli/semsitter-frontend-data-modelfrom
rombirli/semsitter-client-stub
Draft

rombirli wants to merge 4 commits into
rombirli/semsitter-frontend-data-modelfrom
rombirli/semsitter-client-stub

Conversation

@rombirli

@rombirli rombirli commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Part 2 of 2 split from #6533. Stacked on #6541.

SemSitterClient drives one udg-java-server process. It takes the executable path and the project root, checks protocol 2.x, and exposes setSettings, fullGen, getFile, getTypes and hasPackage. Control messages are JSON lines. getFile returns the Document from analyzer-wire. getTypes returns 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. EmbeddedAnalyzer is not carried over.

Tests run the client against FakeServer, a Java stand-in started as a real child process. SemSitterClientE2ETest drives the real binary with -Dsemsitter.executable=<path> and is skipped otherwise. It passes once the server from SonarSource/semsitter#5821 is built.

@hashicorp-vault-sonar-prod

Copy link
Copy Markdown
Contributor

SONARJAVA-7339

Comment on lines +140 to +154
/**
* 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();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 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 👍 / 👎

@datadog-sonarsource

datadog-sonarsource Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Pipelines

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: d77c7ec | Docs | Give us feedback!

@rombirli
rombirli force-pushed the rombirli/semsitter-client-stub branch from 97338c2 to 3de11ef Compare November 20, 2026 08:34
Comment on lines +117 to +122
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);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 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 👍 / 👎

Comment on lines +168 to +170
if (header.has("payload_bytes")) {
payload = fromServer.readNBytes(header.get("payload_bytes").getAsInt());
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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 👍 / 👎

@rombirli
rombirli force-pushed the rombirli/semsitter-client-stub branch from c4fd850 to ae5d84a Compare November 20, 2026 10:29
@rombirli rombirli changed the title SONARJAVA-7339 Add the SemSitter server client SONARJAVA-7339 Add the SemSitter client Nov 20, 2026
@gitar-bot

gitar-bot Bot commented Nov 20, 2026

Copy link
Copy Markdown
Code Review ⚠️ Changes requested 🟡 Medium risk 1 closed / 4 findings

Adding a SemSitter subprocess client changes Java analysis execution and protocol handling; incompatible responses could disrupt project analysis.

Adds a SemSitterClient that drives one udg-java-server process over JSON control lines with protobuf payloads, exposing setSettings, fullGen, getFile, getTypes, and hasPackage. Changes are requested before merge: close() and start() can block forever when the server never replies, getFile and getTypes bypass the lock that send() holds, and truncated payloads are accepted without error. The previously dropped Response.warnings issue has been addressed.

⚠️ Bug: close() hangs forever when the server never answers the quit request

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:140-154 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:122-127 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:78

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();
  }
}
⚠️ Bug: getFile/getTypes call exchange() without the lock send() holds

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:117-122 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:127-133 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:153-167

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 {
💡 Edge Case: A truncated payload is accepted without any error

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:168-170

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");
  }
}
✅ 1 closed
✅ Quality: Response.warnings is dropped by every public client method

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:97-111 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:134-137
The protocol defines Response.warnings ("Present when the scan has unresolved imports and no dependencies are configured") at the level of the envelope. Every public method (setSettings, fullGen, getFile, getTypes, hasPackage) returns only the inner result message. send neither logs nor exposes warnings. Callers therefore cannot see the warnings the server sends about missing dependencies. Fix: log the warnings inside send, or return or collect them so the front-end can report them.

🤖 Prompt for agents
Code Review: Adds a `SemSitterClient` that drives one udg-java-server process over JSON control lines with protobuf payloads, exposing `setSettings`, `fullGen`, `getFile`, `getTypes`, and `hasPackage`. Changes are requested before merge: `close()` and `start()` can block forever when the server never replies, `getFile` and `getTypes` bypass the lock that `send()` holds, and truncated payloads are accepted without error. The previously dropped `Response.warnings` issue has been addressed.

1. ⚠️ Bug: close() hangs forever when the server never answers the quit request
   Files: java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:140-154, java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:122-127, java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:78

   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.

   Fix (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();
     }
   }

2. ⚠️ Bug: getFile/getTypes call exchange() without the lock send() holds
   Files: java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:117-122, java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:127-133, java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:153-167

   `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.

   Fix (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 {

3. 💡 Edge Case: A truncated payload is accepted without any error
   Files: java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:168-170

   `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.

   Fix (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");
     }
   }

Verification Rules 2/2 met Repos 3 inspected · PRs 1 inspected

Functional validation No results

Rules 2 passed 2 not applicable

✅ Internal artifact registries
✅ Repository license compliance

Broader impact Repos 3 inspected PRs 1 inspected
Cross-repo 3 repositories inspected 0 open impacts

SonarSource/sonarlint-vscode Inspected
SonarSource/sonar-iac Inspected
SonarSource/sonar-lits Inspected

Cross-PR 1 PR inspected 0 open impacts

PR 6541: SONARJAVA-7339 Add the SemSitter front-end module with its protobuf data model Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.
Unblock → Override a blocking verdict and allow merging.

Comment with these commands to change the behavior for this request:

Auto-apply Compact Unblock
gitar auto-apply:on         
gitar display:verbose         
gitar unblock         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

Copy link
Copy Markdown
Contributor

Quality Gate failed Quality Gate failed

Failed conditions
1 New issue

See analysis details on SonarQube

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE SonarQube for IDE

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant