[2.x] fix: Return JSON-RPC error for unknown server methods (#9002)

The fallback ServerHandler silently dropped requests with unknown
methods, violating the JSON-RPC spec. Clients like Metals that send
unrecognized requests would never receive a response, potentially
causing timeouts or hangs.

Return a -32601 (Method not found) error response instead.

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
BrianHotopp 2026-04-10 01:33:20 -04:00 committed by GitHub
parent 00632754ab
commit d765ba263a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 1 deletions

View File

@ -30,7 +30,10 @@ object ServerHandler {
lazy val fallback: ServerHandler = ServerHandler({ handler =>
ServerIntent(
onRequest = { case x => handler.log.debug(s"Unhandled request received: ${x.method}: $x") },
onRequest = { case x =>
handler.log.debug(s"Unhandled request received: ${x.method}: $x")
handler.jsonRpcRespondError(Some(x.id), -32601, s"Unknown method: ${x.method}")
},
onResponse = { case x => handler.log.debug(s"Unhandled response received") },
onNotification = { case x =>
handler.log.debug(s"Unhandled notification received: ${x.method}: $x")

View File

@ -79,6 +79,16 @@ class ResponseTest extends AbstractServerTest {
}
}
test("unknown method returns error") {
val id = svr.session.nextId()
svr.session.sendJsonRpc(id, "build/foo", "{}").get
val response = svr.session.waitForResponseMsg(10.seconds, id).get
assert(
response.error.exists(_.code == -32601),
s"Expected method-not-found error, got: $response"
)
}
private def neverReceiveResponse(
duration: FiniteDuration
)(predicate: sbt.internal.protocol.JsonRpcResponseMessage => Boolean): Unit =