Skip to content

ADFA-7295 SQLite debugger: refuse text after the JSON body, report a failed finalize, document the connection-id name clash - #2320

Open
davidschachterADFA wants to merge 3 commits into
stagefrom
sqlite-debugger/ADFA-7295
Open

davidschachterADFA wants to merge 3 commits into
stagefrom
sqlite-debugger/ADFA-7295

Conversation

@davidschachterADFA

@davidschachterADFA davidschachterADFA commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

ADFA-7295

Follow-ups to the SQLite debugger (ADFA-7131) from CodeRabbit's review of #2260: F18, F06 and F24 in ADFA-7295, and its three optional items F21, F09 and F11, which David asked for. They concern #2259's code, which is in stage, so this PR goes straight into stage.

What it does (subprojects/sqlite-debugger)

  • F18, text after the JSON body: McpProtocol.post parsed with JSONTokener(body).nextValue(), which reads one value and stops, so {"jsonrpc":"2.0","id":1,"method":"ping"} garbage ran the ping. Only JSON whitespace may follow the value now; anything else gets HTTP 400 with PARSE_ERROR (-32700), and nothing runs. The rest is read with next(), because Android's nextClean() also skips comments and the JVM's org.json does not.
  • F06, a failed sqlite3_finalize: bridge_query dropped its result. A page that stops before SQLITE_DONE ends the statement at sqlite3_finalize, and on a writable connection an autocommit write, such as an INSERT with RETURNING, commits there. A commit that failed rolled the write back while the call returned its first rows. That is now the call's error, unless the query had already failed (then the first error is kept, as the rollback backstop does). No tool opens a writable connection on stage, so production is not affected yet.
  • F24, a README limit: each connection's state is the user data of sqlitedbg_connection_id(), freed by that function's destructor. If the app registers a function of the same name with no arguments on that connection, or deletes ours, SQLite frees the state while the trace callback still uses it. The README's limits say so; a fix is not asked for. The README's read-only paragraph also names the finalize error beside the rollback backstop.
  • F21, the HTTP head deadline: McpHttp.serve and readHead use System.nanoTime(), as finishReply already does, so a backward step of the wall clock no longer extends the deadline.
  • F09, the version script: sqlitedbg.map is a LINK_DEPENDS of libsqlitedbg in CMakeLists.txt, so an edit to the version script alone relinks.
  • F11, the idle timer: TimerIdleScheduler cancels its timer once its task has run, so the timer thread no longer stays parked until the next check cancels it. The class is now internal, for its test.

Commits: 7b83e89 (F18, F06, F24); 156d315 (F21, F09, F11, written by the cloud session); ebadb2d (spotlessApply on 156d315's new test only).

Siblings checked: McpProtocol.post is the module's only JSON body parse. The other sqlite3_finalize calls are is_wal_mode (a read-only PRAGMA; a failure already counts as not WAL) and prepare_one (statements that never ran, so finalize can't fail). On #2312, execute steps every statement to SQLITE_DONE, so its finalize can't add an error either. Left on the wall clock on purpose: the 50 s per-POST tool deadline (McpProtocol.post, clock = System::currentTimeMillis). It crosses into C as epoch milliseconds, and sqlitedbg_jni.c compares it with CLOCK_REALTIME in the progress handler, so a backward step of the clock during a POST lengthens that POST's hold on the tools by the step. Moving it to a monotonic clock changes that contract on both sides at once, in every stacked PR that passes a deadline, so it is out of scope here (2026-11-26 review, comment 4713230914).

Tests

  • New: McpProtocolTest.textAfterTheJsonIsAParseErrorAndRunsNothing (six bodies with text after the JSON, none runs the tool; trailing whitespace still works), and a JniQueryTest case where another connection's read transaction makes the commit at finalize fail with SQLITE_BUSY and the insert is not kept.
  • Revert proofs, run with stage's code and only the new tests: the Kotlin test fails because the first body gets HTTP 200, and the JNI case fails with "null vs 5 the statement failed as it was finished, and nothing it changed was kept: database is locked" (the first page of rows came back instead).
  • New in 156d315: DatabaseToolsTest.theIdleTimerThreadEndsOnceItsCheckHasRun. With the cancel removed it fails at its assertion that the timer thread has ended (rerun locally). F09 was checked by the cloud session with a host CMake and Ninja build: touching sqlitedbg.map relinks, and without LINK_DEPENDS Ninja has no work to do. F21 has no test, as there is no clock seam to step back; the existing trickling-client head-deadline test passes on nanoTime.
  • Local at ebadb2d: :subprojects:sqlite-debugger:testDebugUnitTest (114 tests), spotlessCheck and :subprojects:sqlite-debugger:assembleDebug pass. The C sources are unchanged since 7b83e89.
  • Local at 7b83e89: test.sh passes (ASan+UBSan 15736 checks, TSan 15726, JNI 212, 0 failures); :subprojects:sqlite-debugger:testDebugUnitTest (113 tests) and spotlessCheck pass.

Merging: a test merge with #2312 (and #2313, which carries it) conflicts in bridge_query's done: block, JniQueryTest.java and the README; one with #2319 (and the P2-6 branch on it) conflicts in DatabaseToolsTest.kt, where both add tests at the end. Whichever merges second resolves them. git merge-tree finds no conflict with #2311.

No screens added or changed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn


Code Reviewer: Code Reviewer couldn't review this pull request
The pull request author does not have access to AI for the associated Atlassian organization.

…failed finalize, document the connection-id name clash

Three follow-ups from CodeRabbit's review of #2260 (F18, F06, F24).

- McpProtocol.post: JSONTokener.nextValue() reads one value and stops,
  so {"jsonrpc":"2.0","id":1,"method":"ping"} garbage ran the ping. Only
  JSON whitespace may follow the value now; anything else is HTTP 400
  with PARSE_ERROR, and nothing runs. The rest is read with next(),
  because Android's nextClean() also skips comments and the JVM's
  org.json does not.
- bridge_query dropped the result of sqlite3_finalize. A page that
  stops before SQLITE_DONE ends the statement there, and on a writable
  connection an autocommit write (an INSERT with RETURNING) commits
  there: a failed commit rolled the write back while the call returned
  its first rows. That is now the call's error, unless the query had
  already failed. No tool opens a writable connection yet.
- README: the name clash of sqlitedbg_connection_id() as a limit, and
  the finalize error beside the rollback backstop.

Tests: McpProtocolTest.textAfterTheJsonIsAParseErrorAndRunsNothing, and
a JniQueryTest case where another connection's read lock makes the
commit at finalize fail. Both fail with the fix reverted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Summary

  • Rejects non-whitespace text after a JSON request body in McpProtocol.post. Returns HTTP 400 with PARSE_ERROR (-32700) and does not run the tool. JSON whitespace is still accepted.
  • Reports a failed sqlite3_finalize as the query error in bridge_query, when no earlier query error exists. An earlier query error is kept.
  • Changes HTTP request-head deadlines in McpHttp to use System.nanoTime(), so they follow elapsed time, not wall-clock time.
  • Cancels the idle timer in TimerIdleScheduler after its task runs, including when the task throws. The class changes from private to internal.
  • Makes sqlitedbg.map a link dependency of sqlitedbg, so edits to the version script trigger relinking.
  • Documents connection-state lifetime limits, query cut-cell reporting, schema truncation, and the shared 50-second POST deadline in the README.

Risks:

  • The finalize-failure path is not reached by current stage tools, which open no writable connection.
  • The sqlitedbg_connection_id() name clash (F24) is unfixed and documented only.
  • The finalize-failure change has no clock-seam test for the deadline change (F21). Its timer test covers the idle timer only.
  • The author reports that Gradle tests and spotlessCheck did not run on the latest commit. test.sh was not rerun.
  • Possible merge conflicts with #2312 and #2313 in bridge_query, JniQueryTest.java, and the README.

The SQLite query bridge now reports statement-finalization errors when no earlier exception is pending. The MCP POST parser rejects JSON bodies with trailing non-whitespace content. The HTTP server uses monotonic request-head deadlines, and the idle scheduler cancels its timer after execution.

Changes: SQLite debugger updates

Layer / File(s) Summary
Query finalization errors
subprojects/sqlite-debugger/src/main/cpp/sqlitedbg_jni.c, subprojects/sqlite-debugger/src/test/jni/JniQueryTest.java, subprojects/sqlite-debugger/README.md, subprojects/sqlite-debugger/src/main/cpp/CMakeLists.txt
The JNI query bridge reports a sqlite3_finalize error when no earlier exception is pending. A test checks the error, rollback, and transaction state. The README describes the error path. The CMake target lists sqlitedbg.map as a link dependency.
JSON body validation
subprojects/sqlite-debugger/src/main/java/org/appdevforall/sqlitedebugger/mcp/McpProtocol.kt, subprojects/sqlite-debugger/src/test/java/org/appdevforall/sqlitedebugger/mcp/McpProtocolTest.kt
The POST parser rejects non-whitespace content after a JSON value. Tests check trailing content and trailing whitespace.
Request-head deadlines
subprojects/sqlite-debugger/src/main/java/org/appdevforall/sqlitedebugger/mcp/McpHttp.kt, subprojects/sqlite-debugger/README.md
The HTTP server uses System.nanoTime() to measure request-head deadlines and converts the remaining time to milliseconds for socket timeouts. The README documents the shared POST deadline and request limits.
Idle timer cleanup
subprojects/sqlite-debugger/src/main/java/org/appdevforall/sqlitedebugger/mcp/DatabaseTools.kt, subprojects/sqlite-debugger/src/test/java/org/appdevforall/sqlitedebugger/mcp/DatabaseToolsTest.kt
TimerIdleScheduler is now internal and cancels its timer in a finally block. A test checks that the scheduled thread terminates.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

Merge Risk: Minimal · up to ebadb

The change reports statement-finalize errors as query errors, rejects trailing text after a JSON body, uses monotonic request-head deadlines, and cleans up the idle timer. No concrete defect was found, and the paths that changed have targeted tests, so the change looks ready to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names three changes that the changeset contains: refusing non-whitespace text after the JSON body in McpProtocol.post (F18), reporting a failed sqlite3_finalize as the query error in bridge_…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description is specific and related to the changeset. It explains the F18 fix in McpProtocol.post (rejecting non-whitespace text after the JSON body with HTTP 400 and PARSE_ERROR), the F06 fix in …
Full details: Title check

Explanation

The title names three changes that the changeset contains: refusing non-whitespace text after the JSON body in McpProtocol.post (F18), reporting a failed sqlite3_finalize as the query error in bridge_query (F06), and documenting the connection-id name clash in the README (F24). The title is long and lists several items, but each item matches a real part of the diff, so the title relates to the main change.

Full details: Description check

Explanation

The description is specific and related to the changeset. It explains the F18 fix in McpProtocol.post (rejecting non-whitespace text after the JSON body with HTTP 400 and PARSE_ERROR), the F06 fix in bridge_query (reporting a failed sqlite3_finalize as the query error), the F24 README limit, the F21 nanoTime head deadline in McpHttp, the F09 LINK_DEPENDS in CMakeLists.txt, and the F11 idle timer cancellation in TimerIdleScheduler. It also lists the new tests and the local test results, which match the files in the raw summary.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

A rabbit checks each query’s end,
And guards each JSON tail, my friend.
The clock now counts with steady pace,
The idle timer leaves no trace.
The docs record the limits in place.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: 4e1efc21-7476-40b3-b6a5-760fb4d424e4
Commits

Reviewing files that changed from the base of the PR and between 7b83e89 and ebadb2d.

Files selected for processing: 4

  • subprojects/sqlite-debugger/src/main/cpp/CMakeLists.txt
  • subprojects/sqlite-debugger/src/main/java/org/appdevforall/sqlitedebugger/mcp/DatabaseTools.kt
  • subprojects/sqlite-debugger/src/main/java/org/appdevforall/sqlitedebugger/mcp/McpHttp.kt
  • subprojects/sqlite-debugger/src/test/java/org/appdevforall/sqlitedebugger/mcp/DatabaseToolsTest.kt

Included review availability: This review used your included allowance. Your plan provides up to 5 included reviews per hour; 2 remain after this review.

claude and others added 2 commits November 26, 2026 07:38
On #2320 (7b83e89), which left them out as not asked for; David
asked for them.

- F21: McpHttp's request-head deadline (serve, readHead) uses
  System.nanoTime(), as finishReply does, so a wall clock set back no
  longer extends it.
- F09: sqlitedbg.map is a LINK_DEPENDS of libsqlitedbg, so an edit to
  the version script alone relinks.
- F11: TimerIdleScheduler cancels its timer once the task has run, so
  the daemon thread no longer stays parked until the next schedule.

Tests: DatabaseToolsTest, McpProtocolTest and EventDecoderTest pass
(109), compiled with the Kotlin compiler outside Gradle; the existing
head-deadline test (a trickling client, McpProtocolTest) passes on
nanoTime. New: theIdleTimerThreadEndsOnceItsCheckHasRun; without the
cancel it fails ("still parked"). F09, by a host CMake/Ninja build:
touching sqlitedbg.map relinks, and without LINK_DEPENDS "no work to
do". F21 has no test: there is no clock seam to step back. C sources
unchanged, so test.sh is not rerun. Not run here: Gradle,
spotlessCheck.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAZ8yszEP4MqbR9PLba
…nal change

spotlessKotlinCheck's one change after 156d315: ktlint wraps the
idleThreads() call chain in theIdleTimerThreadEndsOnceItsCheckHasRun.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
@davidschachterADFA

Copy link
Copy Markdown
Collaborator Author

@hal-eisen-adfa, this PR gained two commits since the review request:

  • 156d315 adds ADFA-7295's three optional items, which David asked for:
    • F21: the HTTP head deadline uses System.nanoTime().
    • F09: sqlitedbg.map is a LINK_DEPENDS of libsqlitedbg.
    • F11: the idle timer's thread ends once its check has run, with a test that fails without it.
  • ebadb2d is a Spotless reformat of that test only.

The description lists them with their tests. The earlier commit, 7b83e89, is unchanged.

(Written by Claude Code for David.)

@davidschachterADFA davidschachterADFA left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review of #2320 at ebadb2dcb7. Not stacked (base stage). Includes 156d315 (F21, F09, F11), checked to be an ancestor of this head.

NITPICK - 1 inline, not listed

Verdict: COMMENT. This PR is authored by the account posting the review, and GitHub refuses APPROVE and REQUEST_CHANGES on your own PR. Severity rules: CodeOnTheGo REVIEW.md and CLAUDE.md (a ticket moves to QA with no outstanding critical, high or medium findings); addons has no written rule, so the default applied.


Generated by Claude Code

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.

2 participants