Skip to content

fix: fetch fd leak - #31375

Merged
fraidev merged 1 commit into
denoland:mainfrom
fraidev:fix_fd_leak_fetch
Nov 22, 2025
Merged

fraidev merged 1 commit into
denoland:mainfrom
fraidev:fix_fd_leak_fetch

Conversation

@fraidev

@fraidev fraidev commented Nov 21, 2025 •

Copy link
Copy Markdown
Contributor

Solution for: #31080

Modern reqwest (0.12+) can safely share clients across tokio runtimes.
Because it uses the current runtime's executor instead of spawning background threads.
Using a single shared client prevents resource leaks when worker threads terminate.

Context

It seems that we started to use one client per thread because hyper and reqwest was not safe to share across tokio runtimes (#23699). But seems it's fixed at latest reqwest versions (seanmonstar/reqwest#1148 (comment)).

This PR is using one client again, as we now already use the version of reqwest with this fix.

@coderabbitai

coderabbitai Bot commented Nov 21, 2025 •

Copy link
Copy Markdown

Walkthrough

The PR replaces per-thread HTTP client caching with a single shared client. In cli/http_util.rs, HttpClientProvider’s clients_by_thread_id: Mutex<HashMap<...>> is replaced by client: OnceCell<deno_fetch::Client>, and get_or_create now uses get_or_try_init. HttpClient no longer carries the _unsend_marker. In runtime/ops/web_worker/sync_fetch.rs the fetch path now obtains the client via deno_fetch::get_or_create_client_from_state(state) instead of constructing a client from options per call.

Sequence Diagram(s)

sequenceDiagram
    actor Caller
    participant OldHttpUtil as cli::HttpClientProvider (old)
    participant MutexMap as Mutex<HashMap<ThreadId, Client>>
    participant NewHttpUtil as cli::HttpClientProvider (new)
    participant OnceCell as OnceCell<Client>
    participant Client as deno_fetch::Client
    Note over Caller,OldHttpUtil: Old flow (per-thread)
    Caller->>OldHttpUtil: get_or_create(thread_id, options)
    OldHttpUtil->>MutexMap: lock & lookup(thread_id)
    alt entry exists
        MutexMap-->>OldHttpUtil: existing Client
    else
        MutexMap-->>OldHttpUtil: insert new Client from options
    end
    OldHttpUtil-->>Caller: HttpClient(wrapped per-thread Client)

    Note over Caller,NewHttpUtil: New flow (shared)
    Caller->>NewHttpUtil: get_or_create(options)
    NewHttpUtil->>OnceCell: get_or_try_init(() -> create Client from options)
    alt init needed
        OnceCell-->>NewHttpUtil: new Client
    else
        OnceCell-->>NewHttpUtil: existing Client
    end
    NewHttpUtil-->>Caller: HttpClient(wrapping shared Client)
Loading
sequenceDiagram
    actor WebWorker
    participant OldFetch as sync_fetch.rs (old)
    participant NewFetch as sync_fetch.rs (new)
    participant Options as Options
    participant State as WorkerState
    participant Client as deno_fetch::Client
    Note over WebWorker,OldFetch: Old per-call creation
    WebWorker->>OldFetch: sync_fetch(...)
    OldFetch->>Options: read client options
    Options-->>OldFetch: options
    OldFetch->>Client: create client from options
    Client-->>OldFetch: Client
    OldFetch-->>WebWorker: proceed with fetch using Client

    Note over WebWorker,NewFetch: New state-backed retrieval
    WebWorker->>NewFetch: sync_fetch(...)
    NewFetch->>State: deno_fetch::get_or_create_client_from_state(state)
    State-->>NewFetch: Client
    NewFetch-->>WebWorker: proceed with fetch using Client
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Files to focus on:
    • cli/http_util.rs — verify OnceCell usage, initialization closure, removal of _unsend_marker, and any public API surface changes.
    • runtime/ops/web_worker/sync_fetch.rs — confirm state-backed client retrieval and error mapping.
  • Attention points:
    • Concurrency and lifecycle semantics when moving from per-thread clients to a shared client.
    • Whether client creation options previously used per-thread are preserved or intentionally omitted.
    • Call sites that relied on UnsendMarker or thread-local isolation.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: fetch fd leak' directly addresses the main change: fixing a file descriptor leak in the fetch functionality by switching to a shared client.
Description check ✅ Passed The description clearly relates to the changeset, explaining the rationale for moving from per-thread clients to a single shared client to fix a fd leak.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3eed2cc and 031f8c2.

📒 Files selected for processing (2)
  • cli/http_util.rs (4 hunks)
  • runtime/ops/web_worker/sync_fetch.rs (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
runtime/ops/web_worker/sync_fetch.rs (1)
ext/fetch/lib.rs (12)
  • get_or_create_client_from_state (295-306)
  • state (298-298)
  • state (301-301)
  • state (303-303)
  • state (422-422)
  • state (440-440)
  • state (453-453)
  • state (523-523)
  • state (609-612)
  • state (848-848)
  • state (874-874)
  • state (880-880)
cli/http_util.rs (1)
ext/fetch/lib.rs (4)
  • new (345-348)
  • new (715-721)
  • new (814-816)
  • create_http_client (985-1124)
🔇 Additional comments (6)
cli/http_util.rs (5)

26-26: LGTM!

OnceCell is the right tool for thread-safe lazy initialization of the shared client.


42-42: LGTM!

The shift from per-thread map to a single shared client addresses the fd leak. OnceCell handles thread-safe initialization without requiring a Mutex for reads.


64-64: LGTM!

Standard OnceCell initialization.


69-82: LGTM!

The implementation correctly uses get_or_try_init for thread-safe lazy initialization. Note that this caches the root cert store from the first initialization, so subsequent changes to the provider won't affect the client. This is acceptable for typical use cases where root certs don't change during runtime.


175-175: LGTM - HttpClient is now Send.

Removing the unsend marker allows HttpClient to be sent across threads, which is necessary for the shared client pattern. This depends on the underlying reqwest client being Send + Sync, which should be true for reqwest 0.12+.

runtime/ops/web_worker/sync_fetch.rs (1)

96-97: No issues found—reqwest 0.12.5 async Client pattern is correct.

The code uses the async Client type (confirmed by .await at line 133), which reqwest 0.12+ safely supports across Tokio runtimes via internal Arc-based sharing. The pattern of cloning the client into the spawned thread and using it in the new runtime is sound.


Comment @coderabbitai help to get the list of available commands and usage tips.

@fraidev fraidev changed the title Fix fd leak fetch fix: fetch fd leak Nov 21, 2025
@fraidev fraidev changed the title fix: fetch fd leak fix: fetch fd leak [draft] Nov 21, 2025
@fraidev
fraidev marked this pull request as ready for review November 21, 2025 16:45
@fraidev
fraidev force-pushed the fix_fd_leak_fetch branch 2 times, most recently from f8b348d to 3eed2cc Compare November 21, 2025 18:12
@fraidev fraidev changed the title fix: fetch fd leak [draft] fix: fetch fd leak Nov 21, 2025

@dsherret dsherret left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM too

@fraidev
fraidev merged commit 355d899 into denoland:main Nov 22, 2025
bartlomieju pushed a commit that referenced this pull request Jan 22, 2026
Solution for: #31080

Modern reqwest (0.12+) can safely share clients across tokio runtimes.
Because it uses the current runtime's executor instead of spawning
background threads.
Using a single shared client prevents resource leaks when worker threads
terminate.

## Context
It seems that we started to use one client per thread because hyper and
reqwest was not safe to share across tokio runtimes
(#23699). But seems it's fixed at
latest reqwest versions
(seanmonstar/reqwest#1148 (comment)).

This PR is using one client again, as we now already use the version of
reqwest with this fix.
bartlomieju pushed a commit to bartlomieju/deno that referenced this pull request Jan 26, 2026
Solution for: denoland#31080

Modern reqwest (0.12+) can safely share clients across tokio runtimes.
Because it uses the current runtime's executor instead of spawning
background threads.
Using a single shared client prevents resource leaks when worker threads
terminate.

## Context
It seems that we started to use one client per thread because hyper and
reqwest was not safe to share across tokio runtimes
(denoland#23699). But seems it's fixed at
latest reqwest versions
(seanmonstar/reqwest#1148 (comment)).

This PR is using one client again, as we now already use the version of
reqwest with this fix.
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.

3 participants