Skip to content

fix(duckdb): run DuckDB's tasks on 8 MiB threads so a deep ATTACH cannot overflow - #4354

Merged
datlechin merged 2 commits into
mainfrom
fix/duckdb-deep-attach-crash
Nov 19, 2026
Merged

datlechin merged 2 commits into
mainfrom
fix/duckdb-deep-attach-crash

Conversation

@datlechin

Copy link
Copy Markdown
Member

Summary

  • An ATTACH reads the attached file's catalog inside a task of its statement, and DuckDB's own threads can take that task. They get 512 KiB on macOS and iOS, so a file whose CHECK nests about 600 deep or more (DuckDB allows 1,000) crashed the app now and then. This is the deepFileAttaches() crash in CI.
  • The iOS app and the macOS DuckDB plugin now open DuckDB with threads and external_threads at the processor count, so DuckDB starts no thread of its own, and run its tasks on 8 MiB threads of their own (DuckDBTaskThreads). The locked instance the macOS import opens does the same, since it allows an ATTACH inside its folder.
  • After a statement that can change the count, the app reads the counts on its own connection and takes over any threads DuckDB started for a raise, so a later change can never hang on our threads. allowed_configs keeps that possible after a lock.
  • iOS: a text of several statements runs one statement at a time, split as the editor splits it, with the result duckdb_query gives. A text DuckDB cannot parse runs nothing.
  • Behavior change: SET threads below the processor count now fails with DuckDB's own error. Supporting it needs the setting before the statement runs, which DuckDB 2.0 makes unsafe (ADR 0037).
  • ADR 0037 records the decision and amends 0031.
  • The core driver is unchanged. An ATTACH there also runs on DuckDB's threads, which get 8 MiB on Linux and 1.5 MiB in the Windows app; a 992-deep ATTACH needs about 840 KiB (clang, arm64).

Test plan

  • DuckDBDeepExpressionTests.deepFileAttaches() with -test-iterations 100 -run-tests-until-failure on the iOS 27.0 simulator: on main it crashed at repetition 2 (stack overflow on a DuckDB thread in PhysicalAttach); here 100 of 100 pass, and the whole suite passes 100 iterations.
  • New iOS tests: a 900-deep file attached 100 times; raised counts (SET, PRAGMA, EXPLAIN ANALYZE) taken over, deep ATTACHes after each, a lower count refused; a text that raises the count before a deep ATTACH and then locks; an IMPORT that raises and locks; an unparsable text runs nothing; a failed SET runs once; a text keeps duckdb_query's result.
  • Every DuckDB iOS suite and MobileImportDataSinkTests, verify.sh plugins, lint, l10n.
  • A C probe on the shipped libduckdb: on main 3 of 5 runs of 200 ATTACHes die; with the app's threads 5,000 pass at the same parallel speed.

@datlechin
datlechin enabled auto-merge (squash) November 19, 2026 15:34
@datlechin
datlechin merged commit 68da8db into main Nov 19, 2026
21 checks passed
@datlechin
datlechin deleted the fix/duckdb-deep-attach-crash branch November 20, 2026 14:22
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.

1 participant