Skip to content

Clip a failed item's error message on a character boundary - #63

Merged
mason-sharp merged 2 commits into
mainfrom
fix/issue-62-error-message-truncation
Aug 12, 2026
Merged

Clip a failed item's error message on a character boundary#63
mason-sharp merged 2 commits into
mainfrom
fix/issue-62-error-message-truncation

Conversation

@dpage

@dpage dpage commented Aug 12, 2026

Copy link
Copy Markdown
Member

Summary

queue_item_note_error() clipped the message into its fixed 1024 byte buffer with strlcpy(), which counts bytes, so a longer message whose cut fell inside a multi-byte character left a partial one behind. That partial character was then stored, because encoding is validated where input arrives from a client, in pg_client_to_server(), and SPI never crosses that boundary; a literal the worker assembles in C is stored as whatever bytes it holds. pg_mbcliplen() clips to the last character that fits entirely, works in the server encoding rather than assuming UTF-8, and allocates nothing, which matters inside an error handler.

The consequence is a corrupt value rather than a stuck queue: queue.error_message is left invalid in the server encoding, so length(), substring() and any client decoding strictly raise on that row, whilst SELECT, left() and LIKE happen to survive. Whether an operator's query breaks therefore depends on which functions it uses, and the row that breaks it is by definition one that was already reporting a problem.

Worth reading #62's correction before this diff: I originally claimed the truncation made the recording UPDATE unparseable and so put the item in a reclaim loop. Driving the real worker into it disproved that, and the issue now records both the correct mechanism and how my first probe misled me. The fix is the same either way; the test is not, which is the point of having checked.

Test plan

  • test/t/007_error_message_truncation.pl drives the real worker into a 1025 byte message, being 1022 single-byte characters followed by one three-byte character, so the 1023rd byte of the buffer is a lead byte whose other two do not fit. The fault is a trigger on the chunk table, the item is sparse_only so no embedding is ever requested, and the provider is ollama, whose init needs neither a key nor a network round trip.
  • 8/8 pass with the fix. Against an unfixed build, assertions 3 to 6 fail: length() raises rather than returning a number, and octet_length() comes back as 1023 with the message no longer intact. Assertions 1 and 2, that the attempt is charged and the failure is recorded, pass either way and are marked in the test as guards rather than the point, precisely because they do not discriminate.
  • The fixture is pinned by the test itself: it asserts server-side that the injected message really is 1025 bytes, and that the over-long message is the failure being recorded, so the discriminating assertions cannot pass because the error was something shorter all along.
  • Regression suite: 18 of 19 pass locally. providers fails in my environment only, because the running cluster still had a library preloaded from before the recent commit that made four provider GUCs superuser-only, so the unprivileged-session assertions do not fire. It is untouched by this change and passes in CI.

Closes #62

strlcpy() counts bytes, so a message longer than the 1024 byte buffer
whose cut fell inside a multi-byte character left a partial one behind.
That partial character was then stored: encoding is validated where
input arrives from a client, in pg_client_to_server(), and SPI never
crosses that boundary, so the literal the worker assembles in C goes
into queue.error_message as whatever bytes it happens to hold.

The row is then invalid text in the server encoding.  length(),
substring() and any client that decodes strictly raise on it, whilst
SELECT, left() and LIKE happen to survive, so whether an operator's
query breaks depends on which functions it uses -- and the row that
breaks it is by definition one that was already reporting a problem.

pg_mbcliplen() clips to the last character that fits entirely.  It
works in the server encoding rather than assuming UTF-8, and it
allocates nothing, which matters inside an error handler.

The failure is still recorded either way, so this is not the reclaim
loop that 004 rules out; TAP 007 therefore asserts that the stored
message reads back character-wise, which is the property that breaks,
and treats the attempt being charged as a guard rather than the point.

Closes #62
@codacy-production

codacy-production Bot commented Aug 12, 2026

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 570d863f-cdfb-47eb-a814-ffa8ec6e67e1

📥 Commits

Reviewing files that changed from the base of the PR and between abf6c7d and f15c1f2.

📒 Files selected for processing (2)
  • src/worker.c
  • test/t/007_error_message_truncation.pl
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/worker.c

📝 Walkthrough

Walkthrough

The worker uses pg_mbcliplen() to truncate failed-item messages at valid server-encoding character boundaries. The integration test creates a 1025-byte multibyte error, runs the worker, and verifies the stored message, failure attempt, and worker log.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: character-boundary clipping for failed-item error messages.
Description check ✅ Passed The description accurately explains the defect, fix, regression test, and test results.
Linked Issues check ✅ Passed The changes satisfy issue #62 by using pg_mbcliplen() and adding regression coverage for valid multibyte truncation.
Out of Scope Changes check ✅ Passed The worker fix and regression test directly support issue #62, with no unrelated changes identified.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-62-error-message-truncation

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/t/007_error_message_truncation.pl`:
- Line 43: Update the node initialization in the test around $node->init to pass
the locale C and UTF8 encoding options, ensuring the fixture consistently uses
UTF-8 for its byte-length assertions.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5a0f4d95-543a-4c79-bd79-48378654758a

📥 Commits

Reviewing files that changed from the base of the PR and between b0dd712 and abf6c7d.

📒 Files selected for processing (2)
  • src/worker.c
  • test/t/007_error_message_truncation.pl

Comment thread test/t/007_error_message_truncation.pl Outdated
Two review points, neither changing what the fix does.

strnlen() bounded by the buffer replaces strlen(), and strlcpy() given
the clipped length replaces memcpy().  Nothing longer than the buffer
can be kept in any case, so bounding the read costs nothing and means a
message that is somehow not NUL-terminated cannot make us over-read
(CWE-126); trim_whitespace() already measures its input this way for
exactly that reason.  Reusing strlcpy() also leaves the copy in the hands
of a function that cannot overrun the destination.

The test now initialises its cluster with an explicit --encoding=UTF8
rather than inheriting the developer's locale.  What is under test is
encoding-agnostic, since pg_mbcliplen() reads the server encoding, but
the fixture's arithmetic is not: it relies on U+4E16 occupying three
bytes, and under a single-byte encoding the message would neither reach
1025 bytes nor straddle the boundary.
@mason-sharp
mason-sharp merged commit 0f1545f into main Aug 12, 2026
9 checks passed
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.

Worker clips error messages byte-wise, so a multibyte message stops the failure ever being recorded

2 participants