Skip to content

Fix thread list failure above SQLite variable limit - #1209

Merged
SawyerHood merged 2 commits into
mainfrom
bb/fix-1205-thread-list-route-crashes-at-16-377-vis-thr_287q8zxazx
Aug 8, 2026
Merged

SawyerHood merged 2 commits into
mainfrom
bb/fix-1205-thread-list-route-crashes-at-16-377-vis-thr_287q8zxazx

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Aug 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Chunk active background-task count reads at 16,377 thread IDs.
  • Derive that size from the 32,766-variable limit, 11 fixed values, and two uses per ID.
  • Batch every thread-list event lookup through one variable-budget helper.
  • Add and pin a covering partial index for background-task events.
  • Add boundary, route, result-equivalence, migration, and query-plan tests.

Reproduction

I ran the issue reproduction before any change. A list of 16,376 IDs completed. A list of 16,377 IDs failed with SqliteError: too many SQL variables.

After the fix, lists of 16,377, 16,378, and 32,754 IDs completed. The thread-list response also completed with 32,767 active threads.

Measurements

I used the 1.45 GB synthetic database from #1204. It has 30,000 threads and about 595,000 events. It has no background-task events. Thus, these results measure rejection of unrelated events. Correctness tests use real background-task rows.

Each cold sample used a fresh process. I evicted the database page cache with posix_fadvise(DONTNEED). I measured reads with /proc/self/io. I capped the NVMe device at 1,000 read IOPS. Each value below is the median of three samples.

Thread IDs Cold reads before Cold reads after Cold time before Cold time after Warm time before Warm time after
5,000 4,149 3 3.96 s 18.7 ms 29.1 ms 18.7 ms
15,000 6,888 3 6.87 s 52.7 ms 75.5 ms 53.5 ms

The old plan used events_thread_type_item_kind_sequence_idx for both task scans. The fixed plan uses events_background_task_thread_type_item_sequence_idx as a covering index for both scans. The query has a literal partial-index predicate and two INDEXED BY pins.

Verification

  • pnpm exec turbo run test --filter=@bb/db --force: 370 tests passed.
  • The focused server route suite passed all 9 tests.
  • pnpm exec turbo run typecheck --filter=@bb/db --filter=@bb/server: passed.
  • A regression test crosses the old active-task variable boundary.
  • A regression test compares chunked counts with single-query counts.
  • A route regression builds 32,767 active thread-list entries.
  • Query tests cross the goal and request-key limits.
  • A query-plan test confirms both partial-index scans.
  • Drizzle generated migration 0089 and its snapshot.

Fixes #1205.

@SawyerHood

Copy link
Copy Markdown
Collaborator Author

🚨 SLOP COP 🚨 · review

I am the SlopCop. I am reviewing this pull request for security, code quality, performance, architecture, and end-to-end behavior.

Comment thread packages/db/src/data/events.ts Resolved

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

🚨 SLOP COP 🚨 · review

ELI5 summary

Think of the thread names as blocks that must fit in a box.

This change uses two boxes, so 16,378 names fit. A later step still uses one box. At 32,767 names, the list breaks again.

Finding

  • Medium — The thread-list route still exceeds the SQLite variable limit at 32,767 visible threads. The inline comment has the full trace. Please batch listLatestGoalEventRowsByThreadIds. The active-thread query has the same one-list pattern.

Review details

  • Security: I found no security issues. The raw SQL predicate uses a fixed typed value.
  • Code quality: The migration, snapshot, schema, query, and tests agree. Duplicate IDs and chunk results work correctly.
  • Performance: The changed query stays below 32,766 variables. Both scans use the new partial index.
  • Architecture: pending-interactions.ts has another local batch helper. A shared database helper can protect all bulk-ID queries.

Validation

  • The database suite passed with 368 tests in 28 files.
  • The database Turbo type check passed.
  • The browser route returned 200 with 16,378 visible threads.
  • The same route returned 500 with 32,767 visible threads. The server logged too many SQL variables.
  • All GitHub checks passed.

@SawyerHood
SawyerHood merged commit f0f7849 into main Aug 8, 2026
10 checks passed
@SawyerHood
SawyerHood deleted the bb/fix-1205-thread-list-route-crashes-at-16-377-vis-thr_287q8zxazx branch August 8, 2026 22:57
amadad pushed a commit to amadad/bb that referenced this pull request Aug 14, 2026
## Summary

- Chunk active background-task count reads at 16,377 thread IDs.
- Derive that size from the 32,766-variable limit, 11 fixed values, and
two uses per ID.
- Batch every thread-list event lookup through one variable-budget
helper.
- Add and pin a covering partial index for background-task events.
- Add boundary, route, result-equivalence, migration, and query-plan
tests.

## Reproduction

I ran the issue reproduction before any change. A list of 16,376 IDs
completed. A list of 16,377 IDs failed with `SqliteError: too many SQL
variables`.

After the fix, lists of 16,377, 16,378, and 32,754 IDs completed. The
thread-list response also completed with 32,767 active threads.

## Measurements

I used the 1.45 GB synthetic database from get-bb#1204. It has 30,000 threads
and about 595,000 events. It has no background-task events. Thus, these
results measure rejection of unrelated events. Correctness tests use
real background-task rows.

Each cold sample used a fresh process. I evicted the database page cache
with `posix_fadvise(DONTNEED)`. I measured reads with `/proc/self/io`. I
capped the NVMe device at 1,000 read IOPS. Each value below is the
median of three samples.

| Thread IDs | Cold reads before | Cold reads after | Cold time before |
Cold time after | Warm time before | Warm time after |
|---:|---:|---:|---:|---:|---:|---:|
| 5,000 | 4,149 | 3 | 3.96 s | 18.7 ms | 29.1 ms | 18.7 ms |
| 15,000 | 6,888 | 3 | 6.87 s | 52.7 ms | 75.5 ms | 53.5 ms |

The old plan used `events_thread_type_item_kind_sequence_idx` for both
task scans. The fixed plan uses
`events_background_task_thread_type_item_sequence_idx` as a covering
index for both scans. The query has a literal partial-index predicate
and two `INDEXED BY` pins.

## Verification

- `pnpm exec turbo run test --filter=@bb/db --force`: 370 tests passed.
- The focused server route suite passed all 9 tests.
- `pnpm exec turbo run typecheck --filter=@bb/db --filter=@bb/server`:
passed.
- A regression test crosses the old active-task variable boundary.
- A regression test compares chunked counts with single-query counts.
- A route regression builds 32,767 active thread-list entries.
- Query tests cross the goal and request-key limits.
- A query-plan test confirms both partial-index scans.
- Drizzle generated migration 0089 and its snapshot.

Fixes get-bb#1205.
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.

listActiveBackgroundTaskCountsByThreadIds throws 'too many SQL variables' at ≥16,377 visible threads, failing the thread-list route

1 participant