Skip to content

Commit b6ddeef

Browse files
authored
[browser][coreclr] Preserve timer callback during app trimming (#138001)
1 parent 21706e2 commit b6ddeef

4 files changed

Lines changed: 29 additions & 17 deletions

File tree

‎src/coreclr/System.Private.CoreLib/CreateRuntimeRootILLinkDescriptorFile.targets‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
<ItemGroup>
1717
<_ILLinkDescriptorsFilePaths Include="$(ILLinkDirectory)ILLink.Descriptors.Shared.xml" />
18+
<_ILLinkDescriptorsFilePaths Condition="'$(TargetsBrowser)' == 'true' and '$(FeatureMultithreading)' != 'true'" Include="$(ILLinkDirectory)ILLink.Descriptors.Browser.xml" />
1819
<_ILLinkDescriptorsFilePaths Condition="'$(TargetsWindows)' == 'true'" Include="$(ILLinkDirectory)ILLink.Descriptors.Windows.xml" />
1920
<_ILLinkDescriptorsFilePaths Include="$(CoreLibSharedDir)ILLink\ILLink.Descriptors.Shared.xml" />
2021
</ItemGroup>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<linker>
2+
<assembly fullname="System.Private.CoreLib">
3+
<type fullname="System.Threading.TimerQueue">
4+
<!-- Required by the native library even when managed timers are unused. -->
5+
<method name="TimerHandler" />
6+
</type>
7+
</assembly>
8+
</linker>

‎src/libraries/System.Private.CoreLib/src/System/Threading/TimerQueue.Browser.cs‎

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Collections.Generic;
55
using System.Diagnostics;
6-
using System.Diagnostics.CodeAnalysis;
76
using System.Runtime.CompilerServices;
87
using System.Runtime.InteropServices;
98

@@ -23,24 +22,18 @@ internal partial class TimerQueue
2322
private static List<TimerQueue>? s_scheduledTimersToFire;
2423
private static long s_shortestDueTimeMs = long.MaxValue;
2524

26-
// this means that it's in the s_scheduledTimers collection, not that it's the one which would run on the next TimeoutCallback
25+
// Tracks membership in s_scheduledTimers, not whether this queue is due on the next callback.
2726
private bool _isScheduled;
2827
private long _scheduledDueTimeMs;
2928
private TimerQueue(int _)
3029
{
3130
}
3231

33-
// This replaces the current pending setTimeout with shorter one
34-
#if MONO
35-
[MethodImplAttribute(MethodImplOptions.InternalCall)]
36-
private static extern unsafe void MainThreadScheduleTimer(void* callback, int shortestDueTimeMs);
37-
#else
3832
[LibraryImport(RuntimeHelpers.QCall)]
39-
private static unsafe partial void SystemJS_ScheduleTimer(int shortestDueTimeMs);
40-
#endif
33+
private static partial void SystemJS_ScheduleTimer(int shortestDueTimeMs);
4134

35+
// JavaScript invokes this export to process due timers.
4236
[UnmanagedCallersOnly(EntryPoint = "SystemJS_ExecuteTimerCallback")]
43-
// this callback will arrive on the main thread, called from mono_wasm_execute_timer
4437
private static void TimerHandler()
4538
{
4639
try
@@ -78,8 +71,7 @@ private bool SetTimer(uint actualDuration)
7871
}
7972

8073
// shortest time of all TimerQueues
81-
[DynamicDependency("TimerHandler")] // https://github.com/dotnet/runtime/issues/101434
82-
private static unsafe void ReplaceNextTimer(long shortestDueTimeMs, long currentTimeMs)
74+
private static void ReplaceNextTimer(long shortestDueTimeMs, long currentTimeMs)
8375
{
8476
if (shortestDueTimeMs == long.MaxValue)
8577
{
@@ -91,12 +83,8 @@ private static unsafe void ReplaceNextTimer(long shortestDueTimeMs, long current
9183
{
9284
s_shortestDueTimeMs = shortestDueTimeMs;
9385
int shortestWait = Math.Max((int)(shortestDueTimeMs - currentTimeMs), 0);
94-
// this would cancel the previous schedule and create shorter one, it is expensive callback
95-
#if MONO
96-
MainThreadScheduleTimer((void*)(delegate* unmanaged<void>)&TimerHandler, shortestWait);
97-
#else
86+
// Replace the pending JavaScript timeout only when a timer is due sooner.
9887
SystemJS_ScheduleTimer(shortestWait);
99-
#endif
10088
}
10189
}
10290

‎src/mono/wasm/Wasm.Build.Tests/NativeBuildTests.cs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ public NativeBuildTests(ITestOutputHelper output, SharedBuildPerTestClassFixture
2020
{
2121
}
2222

23+
[ConditionalTheory(typeof(BuildTestBase), nameof(IsCoreClrRuntime))]
24+
[InlineData(Configuration.Release)]
25+
[TestCategory("native"), TestCategory("coreclr"), TestCategory("workload")]
26+
public async Task NativeRelinkWithoutManagedTimers(Configuration config)
27+
{
28+
ProjectInfo info = CreateWasmTemplateProject(
29+
Template.WasmBrowser, config, aot: false, "native_relink_without_timers",
30+
extraProperties: "<WasmBuildNative>true</WasmBuildNative><PublishTrimmed>true</PublishTrimmed><WasmRunInWorker>false</WasmRunInWorker>");
31+
ReplaceFile("Program.cs", Path.Combine(BuildEnvironment.TestAssetsPath, "EntryPoints", "SimpleSourceChange.cs"));
32+
ReplaceMainJsWithMinimalRunMain();
33+
34+
PublishProject(info, config, new PublishOptions(AssertAppBundle: false), isNativeBuild: true);
35+
await RunForPublishWithWebServer(new BrowserRunOptions(config, ExpectedExitCode: 55));
36+
}
37+
2338
[Theory]
2439
[BuildAndRun(aot: false)]
2540
[TestCategory("wasm-opt")]

0 commit comments

Comments
 (0)