Skip to content

Replace sync with async fs#55

Merged
PolarBearEs merged 1 commit into
masterfrom
fix/async-fs
May 25, 2026
Merged

Replace sync with async fs#55
PolarBearEs merged 1 commit into
masterfrom
fix/async-fs

Conversation

@PolarBearEs
Copy link
Copy Markdown
Owner

No description provided.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 23, 2026

Greptile Summary

This PR replaces synchronous std::fs and Path::exists() calls with async equivalents — either tokio::fs directly or tokio::task::spawn_blocking wrappers — to avoid blocking the async executor during filesystem operations. A secondary improvement is the consistent elimination of TOCTOU-prone check-then-act patterns (e.g., if !path.exists() { return Ok(None) }) in favour of attempting the operation and handling ErrorKind::NotFound.

  • AuthResolver::new is restricted to #[cfg(test)]; a new new_async constructor wraps load_docker_config in spawn_blocking and is used in all production call sites.
  • Synchronous helpers in store/fs.rs (read_json_if_exists, reconcile_partial_file) and new async wrappers in store/mod.rs (atomic_write_bytes_blocking, ensure_store_layout_blocking, etc.) replace the direct blocking calls that previously ran on the async executor.
  • Path::exists() is replaced by tokio::fs::try_exists throughout; unlike exists(), try_exists propagates I/O errors other than NotFound rather than silently returning false.

Confidence Score: 5/5

Safe to merge — all changed code paths are correct and the async migration is consistent throughout.

The change is a mechanical but thorough conversion: blocking filesystem calls are moved off the async executor via spawn_blocking wrappers, and TOCTOU check-then-act patterns are replaced with operation-first + NotFound matching. The unusual implicit-return pattern used in save_reference and load_reference is already established elsewhere in the module and is correct. No logic paths were altered beyond the async/error-handling improvements.

No files require special attention. src/store/mod.rs has the most churn but all changes are consistent with the pattern established earlier in the file.

Important Files Changed

Filename Overview
src/store/mod.rs Largest change: wraps all blocking I/O in spawn_blocking helpers; replaces Path::exists() with tokio_fs::try_exists; adds remove_file_if_exists helper. Logic is consistent and TOCTOU-safe.
src/store/fs.rs read_json_if_exists and reconcile_partial_file updated to match on ErrorKind::NotFound instead of pre-checking existence, eliminating TOCTOU races.
src/auth.rs new() restricted to #[cfg(test)]; new_async() added for production use via spawn_blocking. load_docker_config switches to ErrorKind::NotFound matching.
src/serve_registry/server.rs Single change: path.exists() → tokio_fs::try_exists. Errors beyond NotFound now propagate instead of silently returning false.
src/export/oci_archive.rs blob_path().exists() → tokio_fs::try_exists for missing-blob detection. Correct and minimal change.
src/pull/orchestrator.rs AuthResolver::new() replaced with new_async().await. No logic changes.
src/runtime.rs AuthResolver::new() replaced with new_async().await. No logic changes.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Caller: pull / serve] --> B[AuthResolver::new_async]
    B --> C[spawn_blocking: load_docker_config]
    C -->|NotFound| D[Ok: None]
    C -->|Ok| E[Ok: DockerConfig]
    C -->|Other error| F[Err propagated]

    A2[Store::open / open_active] --> G[ensure_directory_blocking]
    G --> H{shared_lock?}
    H -->|yes| I[acquire_shared_cache_lock]
    H -->|no| J[lock = None]
    I --> K[ensure_store_layout_blocking]
    J --> K
    K --> L[Store ready]

    L --> M[save_blob_bytes]
    M --> N[tokio_fs::try_exists]
    N -->|exists + complete| O[return Ok early]
    N -->|missing| P[atomic_write_bytes_blocking]

    L --> Q[save_reference]
    Q --> R[spawn_blocking: atomic_write_json]

    L --> S[load_reference]
    S --> T[spawn_blocking: read_json_if_exists]
    T -->|NotFound| U[Ok: None]
    T -->|Ok| V[Ok: Some record]
Loading

Reviews (1): Last reviewed commit: "Replace sync with async fs" | Re-trigger Greptile

@PolarBearEs PolarBearEs merged commit 869955e into master May 25, 2026
11 checks passed
@PolarBearEs PolarBearEs deleted the fix/async-fs branch May 25, 2026 05:22
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