Skip to content

test(tauri): retry temp_dir_for canonicalization instead of falling back - #262

Closed
LIUBINfighter wants to merge 1 commit into
devfrom
fix/temp-dir-canonicalize-race
Closed

LIUBINfighter wants to merge 1 commit into
devfrom
fix/temp-dir-canonicalize-race

Conversation

@LIUBINfighter

Copy link
Copy Markdown
Owner

What happened

CI on #261 failed once and passed on rerun:

test tests::delete_file_repo_trash_rejects_paths_outside_the_repo_root ... FAILED
  left:  Some("No such file or directory (os error 2)")
  right: Some("path-outside-workspace")

The assertion is about workspace path scope, but the reported error is an ENOENT raised by fs::canonicalize inside authorize_existing_workspace_path, called on a file the test had just written a few lines earlier. The scope logic never got a chance to run.

Why the fixture was the cause

temp_dir_for already documents the problem it is meant to solve:

Resolve symlinks and macOS firmlinks (e.g. /var -> /private/var) once, up front. Later fs::canonicalize calls on freshly created files inside this directory then run against the already-resolved path, avoiding intermittent ENOENT from realpath racing the firmlink boundary during path-authorization checks.

It then ended with:

fs::canonicalize(&dir).unwrap_or(dir)

So whenever that one canonicalize call failed, the fixture silently kept the unresolved /var/... path. Every later fs::canonicalize of a freshly created entry inside the directory then had to cross the firmlink again, which is exactly the race the comment says it removed. The symptom appears as a bogus ENOENT in an unrelated scope assertion, which is a very expensive way to learn that a temp directory was not resolved.

Change

  • Retry the canonicalize for up to 10 seconds (25 ms apart) instead of giving up on the first failure.
  • Panic with the real path and the underlying error if it still does not resolve, so a broken fixture reports as a broken fixture.

Test-only code, no production behaviour change.

Verification

  • cargo test — 27 passed
  • cargo fmt --check — clean
  • cargo clippy --all-targets — no new warnings (the items after a test module warning is pre-existing and unrelated)
  • Full cargo test --lib run 12 times locally plus 15 runs of the specific test: no failures, confirming the flake is environment-specific rather than reproducible on this machine

CI on #261 failed once with

    tests::delete_file_repo_trash_rejects_paths_outside_the_repo_root
    left:  Some("No such file or directory (os error 2)")
    right: Some("path-outside-workspace")

and passed on rerun. The assertion is about path scope, but the error is an
ENOENT that comes from `fs::canonicalize` inside
`authorize_existing_workspace_path`, called on a file the test had just written.

`temp_dir_for` is supposed to resolve macOS firmlinks (`/var` -> `/private/var`)
once up front precisely so later canonicalize calls on files inside the directory
do not have to cross that boundary. It ended with

    fs::canonicalize(&dir).unwrap_or(dir)

so whenever that canonicalize did fail, the fixture silently kept the unresolved
`/var/...` path and every later canonicalize of an entry inside the directory went
back to racing the firmlink -- the failure then surfaced as a bogus ENOENT in an
unrelated scope assertion rather than anywhere near its cause.

Retry the canonicalize for up to 10s and fail loudly with the real path and error
if it still does not resolve, so a genuine fixture problem cannot masquerade as a
path-authorization regression again.

Test-only code; `cargo test` (27 passed), `cargo fmt --check` clean.
@LIUBINfighter

Copy link
Copy Markdown
Owner Author

Superseded by #263.

The first pass blamed temp_dir_for's canonicalize fallback and did not fix anything: CI reproduced the same failure with that change in place, and the stack showed the temporary directory itself was resolved correctly. The actual cause is that tests read the process-global HOME while with_temp_home had replaced it, so they resolved paths inside a temporary home that was about to be deleted. #263 captures the real home before any test replaces it and resolves through a thread-local override, plus adds a deterministic regression test that fails against the previous resolution.

@LIUBINfighter
LIUBINfighter deleted the fix/temp-dir-canonicalize-race branch October 3, 2026 16:14
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