Skip to content

fix(jellycompat): stop binding NUL advisory lock keys - #616

Closed
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-management-f735
Closed

cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-management-f735

Conversation

@cursor

@cursor cursor Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Problem

Part of #599

#529 stripped NUL code points from durable playback-session JSON so PostgreSQL would accept the row. That was not enough: replaceUnstartedNegotiation still joined CompatToken, ClientDeviceID, and RouteItemID with U+0000 in Go and bound the result as a single text argument to pg_advisory_xact_lock(hashtextextended($1, 0)).

PostgreSQL rejects NUL in text (SQLSTATE 22021). The transaction aborts before the upsert, PutNegotiated marks the session unpersisted, and the negotiated PlaySessionId mapping stays cache-only. The first revalidation after a restart or another process then drops playback.

This fires for essentially every real Jellyfin-compat client: HandlePlaybackInfo populates all three fields whenever the client sends a DeviceId.

#599 already captured the failure against a live database:

WARN persist negotiated compat playback session failed component=jellycompat error="ERROR: invalid byte sequence for encoding \"UTF8\": 0x00 (SQLSTATE 22021)"
--- FAIL: TestDurableCompatPlaybackStorePutNegotiatedReplacesAcrossInstances
    replacement negotiation was not durable

Approach

Join the lock key inside PostgreSQL with chr(31), passing the three fields as separate text parameters — the same pattern internal/markers/contribution_repo.go already uses. Bind parameters never contain the delimiter, so the advisory lock can succeed and the negotiated session is written durably.

The in-process mutation lock still uses a NUL-joined Go string; that never leaves the process and is unchanged.

Testing

This environment has no Postgres (SILO_TEST_DATABASE_URL unset), so the existing DB-backed replacement test skipped. Package tests including the new SQL-shape pin:

$ go test ./internal/jellycompat/ -count=1 -timeout 180s
ok  	github.com/Silo-Server/silo-server/internal/jellycompat	2.305s

Also ran gofmt on the touched files, go vet ./internal/jellycompat/, and make verify-local-paths.

When SILO_TEST_DATABASE_URL is set, TestDurableCompatPlaybackStorePutNegotiatedReplacesAcrossInstances is the behavioral regression for this failure.

AI Disclosure

  • Tool(s): Cursor
  • Model(s): cursor-grok-4.6-high
  • Involvement: fully AI-generated
  • Adversarial review: Changing the lock-key encoding cannot collide with in-flight old keys because the NUL-joined bind never succeeded, so no live transaction held that lock. chr(31) concatenation can theoretically collide if a client-controlled DeviceId contains U+001F; that is the same tradeoff already accepted for marker contribution locks, and DeviceId values are not expected to contain unit separators. I left the in-memory NUL join alone because Go strings may contain NUL. I did not broaden this PR to the other [bug] CI runs no PostgreSQL, so 171 DB-backed tests silently skip — 7 are broken on main and 2 packages are flaky #599 items (CI Postgres, skipped DB tests).

Checklist

  • I ran an adversarial AI review of the diff and summarized findings above.
  • I ran the repo verify commands relevant to this change: gofmt, go vet ./internal/jellycompat/, go test ./internal/jellycompat/, and make verify-local-paths. Full make lint / web lint were not required (no frontend or whole-tree Go style changes).
Open in Web View Automation 

#529 stripped NUL from durable playback-session JSON, but
replaceUnstartedNegotiation still joined token/device/item with U+0000
and bound that string as PostgreSQL text. Postgres rejects NUL in text
(SQLSTATE 22021), so every negotiated compat session with a DeviceId
failed to persist and stayed cache-only.

Join the lock key with chr(31) inside PostgreSQL, matching the marker
contribution lock, so the bind parameters stay valid text.

Co-authored-by: Quick <[email protected]>
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