Skip to content

fix(dags): lake_init held two sessions on one catalog, losing what it wrote (#778) - #785

Merged
athvin merged 1 commit into
mainfrom
fix/778-lake-init-one-session
Nov 7, 2026
Merged

athvin merged 1 commit into
mainfrom
fix/778-lake-init-one-session

Conversation

@athvin

@athvin athvin commented Nov 7, 2026

Copy link
Copy Markdown
Owner

Closes #778 · unblocks the last of #645's eight test ports

The bug

lake_init reported success while the lake it built recorded none of its relations: a 1.8MB catalog, 22 orphan schema_migrations parquets, no live tables, and a fresh process saw the same — so not a stale attach.

The cause is two sessions on one catalog file inside one process:

  • exec_task::run_inner opens a session for every task (open_order_session, exec_task.rs:372), which attaches the tenant's DuckLake.
  • LakeInit opened a second one via schema::reconcile_lake_env → open_lake_env.

The reconciler's session committed its DDL and was dropped. The pod's ctx session — attached before the reconcile, and dropped after it, since it lives to the end of run_inner — then wrote its own earlier view back over the committed metadata. What survived was the reconciler's parquet data with no catalog rows pointing at it.

The task could not notice: its catalog_hash is computed from the compiled catalog, not read back.

(The issue description's first guess was std::process::exit skipping destructors. That was wrong — reconcile_lake_env's session is dropped when the function returns, long before exit. Corrected in the issue.)

The fix

LakeInit reconciles through the session it already holds. reconcile_lake takes a plain &duckdb::Connection, so ctx.session.connection() works directly.

The comment that justified the second handle — "the lake this task initialises may not have existed when the pod's ctx session was opened" — is false in this DAG: create_catalog is lake_init's upstream and creates the catalog, so the file exists before the pod starts. That premise is why the second session was there, and it stopped being true when provisioning became a DAG.

Worth noting the framework already names this class — AssemblyError::UnorderedLakeWriters, "two concurrent writers of one tenant lake" — but it guards between tasks in a graph, so it cannot see two writers inside one task.

Two further gaps the same port exposed

Both were things the legacy arm did and the task path did not, so both are needed for provisioning to actually work:

  • activate never promoted the default dataset. The dataset is converged at org creation carrying the org's state of that moment — provisioning — and nothing else ever moves it. So every tenant provisioned through the executor kept a default dataset that, in the legacy comment's words, "the dataset guard rightly renders as still being set up".
  • No tenant_schema_states row was written for a provisioned tenant. The finish handler writes that row, but filtered on plan == "schema_reconciled", and lake_init emits lake_initialized. That table exists so the deploy report and the drift check never have to open a lake; a provisioned tenant was invisible to both. LakeInitialized now carries status and detail for the same reason SchemaReconciled does, and the handler accepts either receipt.

The profile seam

pg_provision.rs could not run on the task path at all, because CreateCatalog called Profile::from_env() itself and a Postgres-tier test cannot set an environment variable — std::env::set_var is unsafe under edition 2024 and the workspace denies unsafe_code. That is precisely why dispatch_once_with takes a profile argument, and the executor path had no equivalent.

So ExecutorConfig carries the profile, run_control_task takes it, and CreateCatalog holds it as task state. It stays Default-constructible for the DAG builder, and Default means "auto-provisioning is not configured" — the answer an operator with no ER_PROVISION_MODE already got.

Evidence

pg_provision.rs ported to the executor: a real provision job, claimed and planned, run as create_catalog → lake_init → activate. It asserts what it already asserted against the legacy arm — the org and dataset reach active, raw_records and schema_migrations exist, the ledger's catalog_hash matches lake_catalog_hash(), and the tenant_schema_state row says migrated.

Observed failing with the second session restored, reproducing the bug exactly:

assertion failed: session.lake_table_exists("raw_records").unwrap()

Before this, nothing under tests/ so much as mentioned create_catalog or lake_init — provisioning had no coverage on the only path that runs it, which is how three separate gaps accumulated there unnoticed.

make check green; full er-server tier green against a real Postgres and DuckLake.

… wrote

`lake_init` reported success while the lake it built recorded none of its
relations: a 1.8MB catalog, 22 orphan `schema_migrations` parquets, no live
tables, and a freshly spawned process saw the same — so not a stale attach.

Two sessions were attached to one catalog file inside one process.
`exec_task::run_inner` opens a session for EVERY task, and `LakeInit` opened a
second through `schema::reconcile_lake_env`. The reconciler's session committed
its DDL and was dropped; the pod's ctx session — attached BEFORE the reconcile
and dropped AFTER it, since it lives to the end of `run_inner` — then wrote its
own earlier view back over the committed metadata. What survived was the
reconciler's parquet data with no catalog rows pointing at it.

The task could not notice. Its `catalog_hash` is computed from the compiled
catalog rather than read back, so it reported the hash it intended to write.

So `LakeInit` reconciles through the session it already holds; `reconcile_lake`
takes a plain `&duckdb::Connection`. The comment that justified the second
handle — "the lake this task initialises may not have existed when the pod's
ctx session was opened" — is false in this DAG: `create_catalog` is
`lake_init`'s upstream and creates the catalog, so the file exists before the
pod starts. That premise stopped being true when provisioning became a DAG, and
the second handle outlived it.

`AssemblyError::UnorderedLakeWriters` already names this class — "two
concurrent writers of one tenant lake" — but it guards BETWEEN tasks in a
graph, so it cannot see two writers inside one task.

## Two more provisioning gaps, found by the same port

Both are things the legacy arm did and the task path did not:

- **`activate` never promoted the default dataset.** It is converged at org
  creation carrying the org's state of that moment, `provisioning`, and nothing
  else ever moves it — the boot sweep is do-nothing on existing rows. So every
  tenant provisioned through the executor kept a `default` dataset which, in
  the legacy comment's words, "the dataset guard rightly renders as still being
  set up".
- **No `tenant_schema_states` row was written for a provisioned tenant.** The
  finish handler writes it, but filtered on `plan == "schema_reconciled"` while
  `lake_init` emits `lake_initialized`. That table exists so the deploy report
  and the drift check never open a lake, and a provisioned tenant was invisible
  to both. `LakeInitialized` now carries `status` and `detail` for the same
  reason `SchemaReconciled` does, and the handler accepts either receipt.

## The profile seam

`pg_provision.rs` could not run on this path at all: `CreateCatalog` called
`Profile::from_env()` itself, and a Postgres-tier test cannot set an
environment variable — `std::env::set_var` is `unsafe` under edition 2024 and
the workspace denies `unsafe_code`. That is exactly why `dispatch_once_with`
takes a profile argument; the executor path had no equivalent.

`ExecutorConfig` now carries the profile, `run_control_task` takes it, and
`CreateCatalog` holds it as task state. It stays `Default`-constructible for
the DAG builder, and `Default` means "auto-provisioning is not configured" —
the answer an operator with no `ER_PROVISION_MODE` already got.

## Evidence

`pg_provision.rs` ported to the executor, asserting what it already asserted
against the legacy arm. Observed failing with the second session restored, and
the reproduction is the bug itself:

    assertion failed: session.lake_table_exists("raw_records").unwrap()

Before this, nothing under `tests/` mentioned `create_catalog` or `lake_init`.
Provisioning had no coverage on the only path that runs it, which is how three
separate gaps accumulated there unnoticed.

`make check` green; the full 104-test er-server tier green against a real
Postgres and DuckLake.

Closes #778
Refs #645
@athvin
athvin merged commit b514607 into main Nov 7, 2026
41 checks passed
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.

lake_init reports success but its catalog records no relations

2 participants