Skip to content

Bug: scheduler-drain flakes make an unrelated PR's Unit job red #3036

Closed

Description

@Israeltheminer

Two convex-test suites fail intermittently in CI and pass consistently in isolation. Both drain scheduled functions between cases, and both fail with a symptom that reads as missing data rather than as a timing problem.

Observed

  • convex/audit_logs/integrity_check.test.ts — documented in feat(platform): split chat rag_search into search and list actions #3029 as "a pre-existing load-dependent scheduler-drain flake, 0–5 failures varying per run, 3× standalone 17/17".
  • convex/projects/rest_machine_door.test.ts — getProjectFileForUser … answers the blob ref + type for a bound file failed once with ConvexError: {"code":"PROJECT_NOT_FOUND"}. The case creates a project and immediately queries it. Passed 18/18 twice standalone, 32/32 combined with an unrelated suite, and 403/403 across convex/projects + convex/file_metadata. A whole-run rerun of the same commit was green.

Shape

Both files create a fresh backend per case, so this is not cross-test contamination. Both then drain the scheduler in afterEach:

afterEach(async () => {
  await new Promise((resolve) => setTimeout(resolve, 0));
  await Promise.all([...testBackends].map((t) => t.finishInProgressScheduledFunctions()));
  testBackends.clear();
});

Under CI concurrency a case can proceed while work from the previous case's drain is still settling, and a create-then-read case then reads state that has not landed. The failure surfaces as a not-found error, which looks like a logic bug and is not one.

Why it costs more than a rerun

A change that merely adds tests redistributes files across vitest workers, which is enough to tip a timing-sensitive suite. So an unrelated PR gets a red Unit job, and the author has to prove a negative before merging. That happened here: the failing test was untouched by the diff and unreachable from it, but establishing that took a log read, four local runs, a check of which branches even contain the file, and a full-run rerun.

Worth considering

  • Make the create-then-read cases await the state they depend on rather than relying on the drain.
  • Or give the drain a deterministic barrier so a case cannot start while another's scheduled work is in flight.
  • Or mark the known-fragile cases so a failure is attributable at a glance instead of looking like a regression in whatever PR happened to run.

Filed so the next person to hit a red Unit on an unrelated diff has something to compare against.

Activity

  1. Israeltheminer commented on Aug 22, 2026

    @Israeltheminer
    ContributorAuthor

    Recurred, on a different PR and a different commit.

    convex/projects/rest_machine_door.test.ts > getProjectFileForUser … answers the blob ref + type for a bound file failed again with ConvexError: {"code":"PROJECT_NOT_FOUND"}, thrown from convex/projects/internal_mutations.ts:90 — the branch whose comment reads "Unreachable — the core just inserted it".

    Same discriminators as before:

    • Passed 18/18 standalone on the branch that saw it fail.
    • The diff touched no file under convex/projects/, and nothing reachable from it.
    • The same job's log carried Transaction not started and Transaction already committed or rolled back from unrelated scheduled functions, which is the drain-still-settling signature described above.

    Two data points now: it survived #3038 and #3041, so the toolchain change neither caused nor fixed it. The cost is unchanged — an author whose diff cannot reach the test still has to prove that before merging.

  2. Israeltheminer commented on Aug 22, 2026

    @Israeltheminer
    ContributorAuthor

    Correcting myself, and adding the mechanism for the audit half.

    The two files do not share a shape. The issue above says "both then drain the scheduler in afterEach". Only the projects one does. convex/audit_logs/integrity_check.test.ts drains inline, inside the test body — runCheck() calls a hand-rolled drainScheduledFunctions() after every check run, and never calls finishInProgressScheduledFunctions().

    The audit failures are not assertions. They are that helper's own guard:

    Error: scheduled functions did not drain — investigate the test
    

    Its budget is 200 attempts with a 5ms sleep between them — roughly one second plus a t.run round-trip per iteration. When the fire-and-forget Slack dispatch chain does not quiesce inside that window, the guard throws. So the audit half is a bounded wait that runs out under load, not state read too early.

    Reproduced locally, which the CI-only version was not. On a branch whose diff adds two test files and touches nothing either suite imports, the full platform suite failed 2 runs out of 2, with 1 then 3 failures in this file — the varying count #3029 recorded. Clean main passed the same suite in the same working copy. The branch's diff cannot reach either suite, so the channel is which worker the file lands on, not the change.

    Six files already use the harness API this one hand-rolls — t.finishInProgressScheduledFunctions(), including convex/projects/rest_machine_door.test.ts, convex/tasks/rest_machine_door.test.ts, and convex/documents/create_document_from_upload_rag_skip.test.ts. Replacing the poll with it would make the audit half deterministic rather than merely more patient.

    The projects half is still unexplained, and is a different failure. One fact that may help whoever takes it: createProjectCore (convex/projects/mutations.ts) re-gets the row it just inserted and guards with if (project), tolerating a falsy result — while its caller at convex/projects/internal_mutations.ts:90 throws PROJECT_NOT_FOUND on exactly that condition. Two adjacent call sites defend against the same get-after-insert returning nothing.

  3. Israeltheminer commented on Aug 22, 2026

    @Israeltheminer
    ContributorAuthor

    Correction to the comment above: clean main fails this too.

    I reported "clean main passed the same suite" on a single sample. A second sample of the same suite on the same commit failed — convex/audit_logs/integrity_check.test.ts, 2 failures, the same "did not drain" guard.

    Local samples of bun run --filter @tale/platform test, same machine, run one at a time:

    working copy run 1 run 2
    clean main (f9bdbf7) green 2 failures
    a branch adding two test files 1 failure 3 failures

    So this is ambient on main, not something a particular branch's worker distribution triggers. Drop the "which worker the file lands on" framing from my previous comment — the file fails on an unmodified tree, and the count varies run to run on both. Everything else in that comment stands: the failure is the inline poll's own guard, its budget is 200 attempts at 5ms, and six other suites use t.finishInProgressScheduledFunctions() instead of hand-rolling it.

    Practical consequence: the audit half is not a "prove your diff is innocent" problem, it is a red main problem that happens to be invisible when CI's whole-suite timing lands the other way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority: mediumservice: platform

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions