Skip to content

Commit b524e93

Browse files
authored
[release/7.0] [browser] fix asset counting after download retry (#82617)
* fix assets counting * test that we could retry asset downloads * nicer * feedback
1 parent a03dfad commit b524e93

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

src/mono/sample/wasm/browser/Wasm.Browser.Sample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
<PropertyGroup>
1515
<_SampleProject>Wasm.Browser.Sample.csproj</_SampleProject>
1616
</PropertyGroup>
17-
<Target Name="RunSample" DependsOnTargets="RunSampleWithBrowser" />
17+
<Target Name="RunSample" DependsOnTargets="RunSampleWithBrowserAndSimpleServer" />
1818
</Project>

src/mono/sample/wasm/browser/main.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,28 @@ function sub(a, b) {
88
return a - b;
99
}
1010

11+
let testError = true;
12+
let testAbort = true;
1113
try {
1214
const { runtimeBuildInfo, setModuleImports, getAssemblyExports, runMain, getConfig } = await dotnet
1315
.withConsoleForwarding()
1416
.withElementOnExit()
1517
.withModuleConfig({
1618
configSrc: "./mono-config.json",
19+
imports: {
20+
fetch: (url, fetchArgs) => {
21+
// we are testing that we can retry loading of the assembly
22+
if (testAbort && url.indexOf('System.Private.Uri.dll') != -1) {
23+
testAbort = false;
24+
return fetch(url + "?testAbort=true", fetchArgs);
25+
}
26+
if (testError && url.indexOf('System.Console.dll') != -1) {
27+
testError = false;
28+
return fetch(url + "?testError=true", fetchArgs);
29+
}
30+
return fetch(url, fetchArgs);
31+
}
32+
},
1733
onConfigLoaded: (config) => {
1834
// This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
1935
console.log('user code Module.onConfigLoaded');

src/mono/sample/wasm/simple-server/Program.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,36 @@ private async void ServeAsync(HttpListenerContext context)
159159
if (path.EndsWith(".js") || path.EndsWith(".mjs") || path.EndsWith(".cjs"))
160160
contentType = "text/javascript";
161161

162+
var stream = context.Response.OutputStream;
163+
164+
// test download re-try
165+
if (url.Query.Contains("testError"))
166+
{
167+
Console.WriteLine("Faking 500 " + url);
168+
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
169+
await stream.WriteAsync(buffer, 0, 0).ConfigureAwait(false);
170+
await stream.FlushAsync();
171+
context.Response.Close();
172+
return;
173+
}
174+
162175
if (contentType != null)
163176
context.Response.ContentType = contentType;
164177

165178
context.Response.ContentLength64 = buffer.Length;
166179
context.Response.AppendHeader("cache-control", "public, max-age=31536000");
167-
var stream = context.Response.OutputStream;
180+
181+
// test download re-try
182+
if (url.Query.Contains("testAbort"))
183+
{
184+
Console.WriteLine("Faking abort " + url);
185+
await stream.WriteAsync(buffer, 0, 10).ConfigureAwait(false);
186+
await stream.FlushAsync();
187+
await Task.Delay(100);
188+
context.Response.Abort();
189+
return;
190+
}
191+
168192
try
169193
{
170194
await stream.WriteAsync(buffer).ConfigureAwait(false);

src/mono/wasm/runtime/assets.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ export async function mono_download_assets(): Promise<void> {
7676
asset.pendingDownloadInternal = asset.pendingDownload;
7777
const waitForExternalData: () => Promise<AssetWithBuffer> = async () => {
7878
const response = await asset.pendingDownloadInternal!.response;
79-
++actual_downloaded_assets_count;
8079
if (!headersOnly) {
8180
asset.buffer = await response.arrayBuffer();
8281
}
82+
++actual_downloaded_assets_count;
8383
return { asset, buffer: asset.buffer };
8484
};
8585
promises_of_assets_with_buffer.push(waitForExternalData());
@@ -122,6 +122,10 @@ export async function mono_download_assets(): Promise<void> {
122122
if (!skipInstantiateByAssetTypes[asset.behavior]) {
123123
expected_instantiated_assets_count--;
124124
}
125+
} else {
126+
if (skipBufferByAssetTypes[asset.behavior]) {
127+
++actual_downloaded_assets_count;
128+
}
125129
}
126130
}
127131
})());
@@ -197,7 +201,9 @@ async function start_asset_download_with_throttle(asset: AssetEntry, downloadDat
197201
if (!downloadData || !response) {
198202
return undefined;
199203
}
200-
return await response.arrayBuffer();
204+
const buffer = await response.arrayBuffer();
205+
++actual_downloaded_assets_count;
206+
return buffer;
201207
}
202208
finally {
203209
--parallel_count;
@@ -226,7 +232,6 @@ async function start_asset_download_sources(asset: AssetEntryInternal): Promise<
226232
}
227233
}) as any
228234
};
229-
++actual_downloaded_assets_count;
230235
return asset.pendingDownloadInternal.response;
231236
}
232237
if (asset.pendingDownloadInternal && asset.pendingDownloadInternal.response) {
@@ -262,7 +267,6 @@ async function start_asset_download_sources(asset: AssetEntryInternal): Promise<
262267
if (!response.ok) {
263268
continue;// next source
264269
}
265-
++actual_downloaded_assets_count;
266270
return response;
267271
}
268272
catch (err) {
@@ -293,7 +297,7 @@ function resolve_path(asset: AssetEntry, sourcePrefix: string): string {
293297
: asset.name;
294298
}
295299
else if (asset.behavior === "resource") {
296-
const path = asset.culture !== "" ? `${asset.culture}/${asset.name}` : asset.name;
300+
const path = asset.culture && asset.culture !== "" ? `${asset.culture}/${asset.name}` : asset.name;
297301
attemptUrl = assemblyRootFolder
298302
? (assemblyRootFolder + "/" + path)
299303
: path;
@@ -420,7 +424,7 @@ function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
420424
Module.printErr(`MONO_WASM: Error loading ICU asset ${asset.name}`);
421425
}
422426
else if (asset.behavior === "resource") {
423-
cwraps.mono_wasm_add_satellite_assembly(virtualName, asset.culture!, offset!, bytes.length);
427+
cwraps.mono_wasm_add_satellite_assembly(virtualName, asset.culture || "", offset!, bytes.length);
424428
}
425429
++actual_instantiated_assets_count;
426430
}
@@ -429,10 +433,10 @@ export async function instantiate_wasm_asset(
429433
pendingAsset: AssetEntryInternal,
430434
wasmModuleImports: WebAssembly.Imports,
431435
successCallback: InstantiateWasmSuccessCallback,
432-
) {
433-
mono_assert(pendingAsset && pendingAsset.pendingDownloadInternal, "Can't load dotnet.wasm");
436+
): Promise<void> {
437+
mono_assert(pendingAsset && pendingAsset.pendingDownloadInternal && pendingAsset.pendingDownloadInternal.response, "Can't load dotnet.wasm");
434438
const response = await pendingAsset.pendingDownloadInternal.response;
435-
const contentType = response.headers ? response.headers.get("Content-Type") : undefined;
439+
const contentType = response.headers && response.headers.get ? response.headers.get("Content-Type") : undefined;
436440
let compiledInstance: WebAssembly.Instance;
437441
let compiledModule: WebAssembly.Module;
438442
if (typeof WebAssembly.instantiateStreaming === "function" && contentType === "application/wasm") {

0 commit comments

Comments
 (0)