From 20038cbda6e3c810b4ab3951d56bbcf8f81af19c Mon Sep 17 00:00:00 2001 From: kevin <5299031+kevin9327@users.noreply.github.com> Date: Thu, 8 Oct 2026 02:28:59 +0900 Subject: [PATCH] fix(asynqdl): truncate dead-letter errors on a rune boundary (#3964) --- internal/middleware/asynqdl/asynqdl.go | 19 +++++++++++++++++-- internal/middleware/asynqdl/asynqdl_test.go | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/internal/middleware/asynqdl/asynqdl.go b/internal/middleware/asynqdl/asynqdl.go index c853b67a..9974b2c3 100644 --- a/internal/middleware/asynqdl/asynqdl.go +++ b/internal/middleware/asynqdl/asynqdl.go @@ -17,6 +17,7 @@ import ( "context" "encoding/json" "time" + "unicode/utf8" "github.com/Tencent/WeKnora/internal/logger" "github.com/Tencent/WeKnora/internal/types" @@ -233,9 +234,23 @@ func truncateError(s string, max int) string { } const suffix = "...(truncated)" if max <= len(suffix) { - return s[:max] + return cutAtRuneBoundary(s, max) } - return s[:max-len(suffix)] + suffix + return cutAtRuneBoundary(s, max-len(suffix)) + suffix +} + +// cutAtRuneBoundary returns the longest prefix of s that is at most n bytes +// and does not end in the middle of a multi-byte rune. A partial rune would +// make last_error invalid UTF-8, which PostgreSQL and MySQL reject on insert, +// so the dead-letter row would be lost. +func cutAtRuneBoundary(s string, n int) string { + if n >= len(s) { + return s + } + for n > 0 && !utf8.RuneStart(s[n]) { + n-- + } + return s[:n] } // formatUint inlines strconv.FormatUint without importing strconv just diff --git a/internal/middleware/asynqdl/asynqdl_test.go b/internal/middleware/asynqdl/asynqdl_test.go index dc97c15d..d88ae298 100644 --- a/internal/middleware/asynqdl/asynqdl_test.go +++ b/internal/middleware/asynqdl/asynqdl_test.go @@ -4,8 +4,10 @@ import ( "context" "encoding/json" "errors" + "strings" "sync" "testing" + "unicode/utf8" "github.com/Tencent/WeKnora/internal/types" "github.com/hibiken/asynq" @@ -171,6 +173,24 @@ func TestMiddleware_LongErrorTruncated(t *testing.T) { } } +func TestMiddleware_LongMultiByteErrorStaysValidUTF8(t *testing.T) { + repo := &fakeRepo{} + // Each CJK rune is 3 bytes; the leading "E" shifts the 8178-byte cut + // (8192 minus the suffix) into the middle of one. + long := "E" + strings.Repeat("解析失败", 1000) + _ = runMiddleware(repo, "any", []byte("{}"), errors.New(long)) + row := repo.captureRow(0) + if got := len(row.LastError); got > 8192 { + t.Errorf("expected last_error truncated to <=8192 bytes, got %d", got) + } + if !utf8.ValidString(row.LastError) { + t.Errorf("expected last_error to be valid UTF-8, got trailing bytes %q", row.LastError[len(row.LastError)-20:]) + } + if !strings.HasSuffix(row.LastError, "...(truncated)") { + t.Errorf("expected truncation suffix, got %q", row.LastError[len(row.LastError)-20:]) + } +} + func TestMiddleware_RepoFailure_DoesNotMaskTaskError(t *testing.T) { repo := &fakeRepo{fail: errors.New("db down")} wantErr := errors.New("task boom")