fix(logs): prevent stack overflow flushing large DuckDB write buffers - #316
Merged
Merged
Conversation
flushWriteBuffer() flushes the entire write buffer in a single INSERT and, on failure, requeues it with `unshift(...toFlush)`. Both spread a potentially huge array as call arguments: the INSERT builds a statement with ~7 bound params per row (spread into `db.run(sql, ...params)`), and the requeue spreads the whole batch into `unshift`. Once the buffer grows past V8's argument limit (~100k), this throws "RangeError: Maximum call stack size exceeded". That masks the real write failure and, because clear() flushes before its DELETE, can stop the table from ever draining. - Insert in fixed-size chunks (WRITE_CHUNK_SIZE = 1000) instead of one statement spanning the whole buffer. - Requeue on failure with `toFlush.concat(this.writeBuffer)` instead of `unshift(...toFlush)`, so requeue can't overflow. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
fukouda
approved these changes
Jun 24, 2026
danew
approved these changes
Jun 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
DuckDBStorage.flushWriteBuffer()flushes the entire write buffer in a singleINSERT, and on failure requeues it withunshift(...toFlush). Both spread a potentially huge array as function arguments:INSERTbuilds one statement with ~7 bound params per row, spread intodb.run(sql, ...params), andArray.prototype.unshift.Once the buffer grows past V8's argument limit (~100k elements), this throws
RangeError: Maximum call stack size exceeded. That error masks the original write failure, and becauseclear()callsflushWriteBuffer()before itsDELETE, a wedged flush can stop the table from ever draining — so the buffer/table keeps growing.Observed in production as a steady stream of
DuckDB flush error: RangeError: Maximum call stack size exceeded, alongside Parquet-export OOMs on the same oversized tables.Fix
writeBatchInternalnow inserts in fixed-size chunks (WRITE_CHUNK_SIZE = 1000) via a newwriteChunkhelper, instead of one statement spanning the whole buffer. Each statement is bounded regardless of buffer size.this.writeBuffer = toFlush.concat(this.writeBuffer)instead ofunshift(...toFlush), so requeue can't overflow the stack and the original error surfaces.No behavior change on the happy path; purely bounds the per-statement size and makes the failure path safe.
Testing
Type-checks clean (
tsc --noEmit, 0 errors). The fix is localized toduckdb-storage.ts.🤖 Generated with Claude Code