cli icon indicating copy to clipboard operation
cli copied to clipboard

db reset: intermittent "error running container: exit 1" at "Initialising schema" (one-off migration jobs race the running storage service)

Open milgron opened this issue 2 months ago • 6 comments

Bug report

supabase db reset fails intermittently at "Initialising schema..." with error running container: exit 1 (surfaced by the TypeScript CLI as LegacyDbSetupError). Roughly 1 in 6 to 20 resets on a machine that runs the reset in a loop (pre-commit hook: reset, then supabase test db).

Describe the bug

Sequence printed by the CLI on a failing run:

Resetting local database...
Recreating database...
Initialising schema...
{"_tag":"Error","error":{"code":"LegacyDbSetupError","message":"error running container: exit 1"}}

The failure is inside the reset itself, before any user migration or seed runs. pg_prove afterwards reports every test file with "planned N tests but ran 0" because the schema is missing, which is a consequence, not the cause.

What I think is happening

In apps/cli-go/internal/db/start/start.go, initSchema15 runs one-off Docker jobs for realtime, storage (node dist/scripts/migrate-call.js) and auth (gotrue migrate) against the recreated postgres database. At the same time the long-running service containers reconnect to the recreated database, and at least storage-api runs its own migrations on reconnect. Its container log shows, on every reset:

{"type":"migrations","msg":"[Migrations] Running vector_store migrations"}
{"type":"migrations","msg":"[Migrations] Created database storage_vectors"}

So two migrators race on the same schema during "Initialising schema..." and one of them exits non-zero. That matches the timing-dependent nature: with --debug (slower logging) I could not reproduce it in 22 consecutive resets, without it the failure shows up every handful of runs.

To Reproduce

  1. supabase init, supabase start -x vector,logflare,studio,imgproxy,inbucket,mailpit,edge-runtime (storage, auth, realtime, rest, kong, db and pg_meta running).
  2. Add any migration and a seed.sql.
  3. Loop: for i in $(seq 1 30); do supabase db reset || break; supabase test db; done

One of the resets fails with the message above.

Expected behavior

supabase db reset should either pause the running service containers while the one-off migration jobs run, or run the service migrations only once.

System information

  • OS: Linux 6.18 (Manjaro), Docker 29.6.2
  • Version of supabase-js: n/a
  • Version of Node.js: 24.16
  • Version of supabase CLI: 2.116.0 (devDependency)
  • Images: postgres 17.6.1.165, storage-api 1.70.3, realtime v2.129.3
  • config.toml: default from supabase init, [db.seed] sql_paths = ["./seed.sql"]

Workaround

Retrying supabase db reset once succeeds every time. Excluding realtime from the local stack reduces the number of concurrent migrators but storage-api still races.

milgron avatar Sep 03 '26 11:09 milgron

Able to repro this locally. The background storage container reconnects as soon as postgres comes up and runs its migrations at the exact same time as the one-off migrate job.

Definitely an issue. Putting together a PR to stop the background services before recreating the db so they don't race.

mandar1045 avatar Sep 03 '26 16:09 mandar1045

I've tested the fix locally and all unit tests pass (including mock coverage for stopping services). Could you add the open-for-contribution label so I can open the PR?

mandar1045 avatar Sep 03 '26 16:09 mandar1045

Possibly a second manifestation of this, at the same stage: it can hang instead of exiting.

Same point in the sequence you describe — after Recreating database..., at Initialising schema..., before any user migration runs:

Resetting local database...
Recreating database...
Initialising schema...          <- no further output

No error, no exit. It waits. The longest instance ran 37 minutes before I killed it; a second, reproduced with npx supabase db reset --no-seed directly, was still going at 120s.

While it hangs, the database is dropped and the stack still answers. Checked during the hang:

GET /rest/v1/  ->  HTTP 200 in 0.25s
select count(*) from pg_tables where schemaname='public';  ->  0

So the recreate has happened and nothing has been applied on top — the same "schema is missing" end state your pg_prove output describes, reached by waiting rather than by failing.

Counts, on macOS with Docker Desktop: 2 hangs in 11 resets on one machine. That sits inside the 1-in-6-to-20 you report, which is why I think it may be the same race rather than a separate bug — but I have not established that. I have no evidence connecting the hang to the storage/one-off-job contention specifically; I only know it stops at the same stage and leaves the same state. If the two are the same, a lock wait rather than a container exit would be one way the race resolves.

Flagging it because the two failure modes cost very differently. An exit 1 is loud and fails fast. A hang has no verdict at all: our local gate runner has no timeout, so it sat for 37 minutes; CI would have failed it at 20 by job timeout. If a fix targets the container-exit path specifically, it may be worth checking whether the same contention can block instead.

Happy to gather more if useful — a pg_stat_activity / pg_locks capture taken during a hang would settle whether it is lock contention, but the rate makes catching one in the act slow.

ky1976-byte avatar Sep 07 '26 17:09 ky1976-byte

We are investigating a possibly related local reset failure, but have not established that it has the same cause as this report.

Our setup uses npm Supabase CLI 2.114.0, PostgreSQL major version 17, Node 22.22.0 and a GitHub-hosted Ubuntu 24.04 runner. Our wrapper invokes supabase db reset with default text output. One bounded diagnostic failed during initialization on invocation 9 after eight successes. The wrapper retained exit 1 but discarded the native error message; its SQLSTATE allowlist matched nothing, which does not exclude a database error. We cannot identify the failing initialization operation from that retained record; it need not be a temporary job.

We corrected a separate wrapper reporting problem: the pinned CLI's default error renderer adds ANSI SGR decoration even in our non-interactive pipe. Our fixed-label matcher now handles that decoration. A subsequent bounded run had ten first-attempt successes; this is non-reproduction, not evidence of a reset fix.

We have not established that persistent Storage or Auth restarted into competing migrations, and are not attributing our failure to that mechanism.

Is there a known correction applicable to CLI 2.114.0, or a supported way to capture the failing initialization operation and its underlying error in the same invocation, including service identity if a temporary job failed? We want to preserve default reset behavior and avoid unbounded retries or publishing raw logs that might contain private data.

Electricshepherd87 avatar Sep 08 '26 06:09 Electricshepherd87

I reproduced this failure class on a PostgreSQL 15 local reset path and prepared a focused fix branch:

https://github.com/dubstylee/cli/tree/fix/reset-schema-migration-race

The change stops only the two persistent services evidenced as competing migrators—Storage and Realtime—before recreating Postgres. It runs the existing one-off schema jobs while they are stopped, then restarts those same services (and reloads Kong) through deferred cleanup even if reset initialization fails. It does not add a reset retry or alter user migrations.

Regression coverage proves both that the two services are stopped before a successful reset and that they are restarted while preserving an original recreate failure. Validation: go test ./internal/db/reset -count=1 passes in an isolated Go 1.26 container.

Could a maintainer please add the open-for-contribution label to this open issue? The repository contribution gate requires it before an external PR can be opened; once labeled, I will open the PR targeting develop immediately.

dubstylee avatar Sep 14 '26 22:09 dubstylee

Following up on my September 14 comment: the reset initialization failure still occurs in our CI after upgrading to Supabase CLI 2.118.0, using PostgreSQL 15 on a dedicated self-hosted Linux runner.

Our harness runs supabase start followed by supabase db reset --local --no-seed. Initial startup successfully applies the application migrations. The subsequent reset failed on both the original CI attempt and one failed-job rerun with:

Resetting local database...
Recreating database...
Initialising schema...
error running container: exit 1

The reset fails before replaying application migrations or running our isolation tests. The same runner passed an earlier revision, consistent with an intermittent failure. This matches the failure class in this issue, although we have not captured the internal job's underlying error on these latest attempts and therefore are not claiming the competing-migrator mechanism is conclusively established for them.

I also compared the legacy reset implementations in v2.119.0 and v2.120.0-beta.7. The Go PostgreSQL 15 reset path still does not stop persistent Storage/Realtime services before initialization; the corresponding legacy TypeScript recreate path likewise performs fresh database setup before restarting satellite services. The separate stack backend has a different lifecycle, but that is not the backend used by our current harness.

The focused fix branch from my earlier comment is still available: https://github.com/dubstylee/cli/tree/fix/reset-schema-migration-race

Could a maintainer please add the open-for-contribution label so we can submit the proposed fix for review under the repository's contribution gate? If a different supported fix or contribution route is preferred, please let us know. Thank you!

dubstylee avatar Oct 03 '26 11:10 dubstylee