Skip to content

Swift test suite: shared-singleton contamination causes scheduling-dependent xctest worker crash #9029

Closed

Description

@eulicesl

Summary

Running the full macOS desktop Swift suite in a single swift test process, one suite reliably hard-crashes its xctest worker with no assertion failure (… started with no passed/failed). This is a scheduling-dependent crash caused by shared process-global state leaking across suites, not a bug in any one test.

Repro

cd desktop/macos
xcrun swift test --package-path Desktop      # single combined process
  • The victim suite moves with the skip set — e.g. MemoriesViewModelObserverTests, then MemoryAuthoritativeTierSyncTests — which is the signature of cross-suite state leakage rather than a single bad test.
  • --no-parallel (serial) does not fix it → it is an order-dependent leak, not a data race.
  • Every suite passes in isolation (per-suite swift test --filter <Suite>/, 114/114 green).

Root cause

Many suites mutate process-global singletons and real on-disk state that persists across suites in the same process:
RewindDatabase.shared, MemoryStorage.shared, AuthState.shared, StagedTaskStorage.shared, an on-disk SQLite file, and UserDefaults.standard.

Current mitigation

desktop/macos/test.sh was rewritten to run each suite in its own process (per-suite swift test --filter), mirroring the backend's per-file pytest isolation. This makes the gate deterministic and green. It is a mitigation, not a fix — the underlying singleton coupling remains and still bites anyone running swift test directly.

Suggested durable fix

Introduce dependency-injection / test seams for the shared singletons so suites don't share process-global state (relates to the broader singleton-count debt in the desktop app).

Found during macOS desktop test-suite hardening.

Activity

  1. aryanorastar commented on Sep 4, 2026

    @aryanorastar
    Collaborator

    Update with a measured result: the combined single-process run is now green, which is the "prove combined-process stability" step this issue asks for.

    Two defects, found by reproducing the crash rather than by reading the singleton list:

    1. The process-terminating crash (fix(desktop): stop the notification-settings query from killing a non-app host #12687) — UNUserNotificationCenter.current() in the production notification-settings query. Off an app bundle it raises NSInternalInconsistencyException, which Swift cannot catch, so it terminated the run. This is what produced every symptom reported here: no assertion failure, the victim suite moving with ordering, and every suite passing in isolation.
    2. A stale test pin (test(desktop): exempt the get_screenshot alias from the tool-declaration check #12705) — ChatDiscoverabilityTests asserting an agent-tool declaration for look_at_frame, which is an alias of get_screenshot and Local API only. Broken since feat: add JIT knowledge ledger foundation and guarded adoption #12084 on 2026-08-27, unrelated to the crash.

    swift test --package-path Desktop, same command each time:

    tree crash lines result
    main 1 died at 2650 tests
    + crash guard 0 3023 completed, 1 failure
    + both 0 3065/3065, 0 failures, 1 skipped

    What this does and does not settle

    It settles that no suite currently leaks state badly enough to fail or crash another one in a shared process. It does not mean the singleton coupling described here is gone — RewindDatabase.shared, MemoryStorage.shared, AuthState.shared, StagedTaskStorage.shared, the on-disk SQLite file and UserDefaults.standard are all still process-global, and the suites still share them. The run is green because nothing currently mutates them in an order that hurts, not because the coupling was removed. A new suite can reintroduce this at any time.

    So I would treat the DI work here as still open, and keep test.sh's per-suite isolation for now. Removing that mitigation is worth doing once these two land and the combined run has been green for a while, but it should be its own PR with its own evidence rather than something folded into either fix.

    Both PRs are independent of each other; the green run above needs both.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions