Skip to content

[browser][coreCLR] Fix download()create() lifecycle for WASM loader#127383

Merged
pavelsavara merged 5 commits intodotnet:mainfrom
pavelsavara:browser_download_only
Apr 27, 2026
Merged

[browser][coreCLR] Fix download()create() lifecycle for WASM loader#127383
pavelsavara merged 5 commits intodotnet:mainfrom
pavelsavara:browser_download_only

Conversation

@pavelsavara
Copy link
Copy Markdown
Member

Fixes #124896

The download() API was broken — calling download() followed by create() failed because the native module couldn't be re-initialized. This PR restructures the createRuntime() flow to properly support the two-phase download→create lifecycle.

Changes

Loader core (run.ts)

  • Track download/config state with module-level flags (downloadStarted, downloadedIntoMemory, configInitialized) so download() and create() can be called sequentially without re-running initialization.
  • Config initialization (onConfigLoaded, modulesAfterConfigLoaded, polyfills, console wiring) now runs exactly once regardless of how many times createRuntime is entered.
  • When download() already loaded assets into memory, create() takes a fast path: initialize CoreCLR, fire onDotnetReady, load modulesAfterRuntimeReady — skipping redundant downloads.
  • Duplicate download() calls are a no-op.

HTTP-cache-only prefetch mode (assets.ts)

  • New download(httpCacheOnly: true) parameter. When set, resources are fetched into the browser HTTP cache and discarded — no WASM memory is consumed. A subsequent create() will re-fetch from cache.
  • prefetchAllResources() enqueues all data assets (assemblies, ICU, VFS, WASM, PDBs, symbols, satellites) and fetches them with throttling (maxParallelDownloads).
  • prefetchJSModuleLinks() injects <link rel="prefetch"> hints for JS modules instead of fetching them, since they use import().
  • prefetchUrl() respects loadBootResourceCallback, integrity checks, and disableNoCacheFetch.

Public API (public-api.ts, dotnet.d.ts, host-builder.ts)

  • download() signature updated to download(httpCacheOnly?: boolean).

Tests

  • Extended NoResourcesReFetchedAfterDownloadFinished to verify onConfigLoaded fires during download(), resources are fetched during download, none re-fetched during create(), and create() completes.
  • New HttpCacheOnlyThenCreateWorks test covering download(true)create() with loadBootResource callback verification.
  • Added test scenarios DownloadThenInitHttpCacheOnly in main.js.
  • Enabled DownloadThenInitTests for CoreCLR in BuildWasmAppsJobsListCoreCLR.txt.

@pavelsavara pavelsavara added this to the 11.0.0 milestone Apr 24, 2026
@pavelsavara pavelsavara self-assigned this Apr 24, 2026
Copilot AI review requested due to automatic review settings April 24, 2026 12:21
@pavelsavara pavelsavara added arch-wasm WebAssembly architecture area-Host os-browser Browser variant of arch-wasm labels Apr 24, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Restructures the WASM loader lifecycle to support a two-phase download()create() flow (including an HTTP-cache-only prefetch mode), and extends coverage to prevent regressions.

Changes:

  • Adds loader state tracking so download() can be followed by create() without re-running one-time initialization.
  • Introduces download(httpCacheOnly?: boolean) and implements HTTP-cache-only prefetching for boot assets (plus <link rel="prefetch"> hints for JS modules).
  • Extends/introduces WASM build tests and enables the previously-disabled CoreCLR scenario.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/native/libs/Common/JavaScript/types/public-api.ts Updates the public download(httpCacheOnly?: boolean) API signature and docs.
src/native/libs/Common/JavaScript/loader/run.ts Implements the new lifecycle/state machine and cache-only download path.
src/native/libs/Common/JavaScript/loader/host-builder.ts Wires download(httpCacheOnly?: boolean) through to createRuntime.
src/native/libs/Common/JavaScript/loader/dotnet.d.ts Updates TypeScript declarations for the new download signature.
src/native/libs/Common/JavaScript/loader/assets.ts Adds HTTP-cache-only prefetch implementation and JS module prefetch link hints.
src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js Adds browser test scenarios for download()create() and download(true)create().
src/mono/wasm/Wasm.Build.Tests/DownloadThenInitTests.cs Extends existing test and adds a new test validating the cache-only mode.
eng/testing/scenarios/BuildWasmAppsJobsListCoreCLR.txt Enables DownloadThenInitTests for CoreCLR scenario runs.

Comment thread src/native/libs/Common/JavaScript/loader/assets.ts Outdated
Comment thread src/native/libs/Common/JavaScript/loader/assets.ts Outdated
Comment thread src/native/libs/Common/JavaScript/loader/assets.ts Outdated
Comment thread src/native/libs/Common/JavaScript/loader/run.ts Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 27, 2026 09:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 8 changed files in this pull request and generated 4 comments.

Comment thread src/native/libs/Common/JavaScript/loader/run.ts Outdated
Comment thread src/native/libs/Common/JavaScript/loader/assets.ts Outdated
Comment thread src/native/libs/Common/JavaScript/loader/assets.ts
Comment thread src/native/libs/Common/JavaScript/loader/assets.ts Outdated
@pavelsavara pavelsavara marked this pull request as ready for review April 27, 2026 09:18
@pavelsavara pavelsavara requested a review from maraf as a code owner April 27, 2026 09:18
Copilot AI review requested due to automatic review settings April 27, 2026 09:18
@pavelsavara pavelsavara requested a review from ilonatommy as a code owner April 27, 2026 09:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 8 changed files in this pull request and generated 4 comments.

Comment thread src/native/libs/Common/JavaScript/loader/run.ts
Comment thread src/native/libs/Common/JavaScript/loader/run.ts
Comment thread src/native/libs/Common/JavaScript/loader/run.ts
Comment thread src/native/libs/Common/JavaScript/loader/assets.ts Outdated
Copy link
Copy Markdown
Member

@maraf maraf left a comment

Choose a reason for hiding this comment

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

Looks good to me and my copilot 👍

@pavelsavara pavelsavara merged commit dd4ae10 into dotnet:main Apr 27, 2026
175 of 177 checks passed
@pavelsavara pavelsavara deleted the browser_download_only branch April 27, 2026 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-Host os-browser Browser variant of arch-wasm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[coreclr] Download only - Can't re-init native module

3 participants