Skip to content

Commit df0cab2

Browse files
authored
Use a more reliable way to apply a timeout for a wasm function call with epoch interruption. (#249)
(This is not strictly required for this specific test as the wasm function is an infinite loop, but intended to be a better example illustrating this approach.) When using a System.Threading.Timer with a dueTime, it could happen that the specified callback is called even after the timer was disposed, which could mean that if the wasm function isn't actually an infinite loop, the engine might already have been disposed when the timer callback is executed, which would lead to a crash due to an unhandled ObjectDisposedException thrown by Engine.IncrementEpoch(). In contrast, CancellationTokenRegistration.Dispose() waits until the callback is finished (if it's currently executing), so that it's guaranteed that after disposing the registration, Engine.IncrementEpoch() isn't called any more.
1 parent f0d42ef commit df0cab2

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

tests/EpochInterruptionTests.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ public void ItCanInterruptInfiniteLoop()
4141

4242
var action = () =>
4343
{
44-
using (var timer = new Timer(state => Fixture.Engine.IncrementEpoch()))
45-
{
46-
timer.Change(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(Timeout.Infinite));
47-
run.Invoke();
48-
}
44+
using var cts = new CancellationTokenSource(100);
45+
using var tokenRegistration = cts.Token.Register(Fixture.Engine.IncrementEpoch);
46+
47+
run.Invoke();
4948
};
5049

5150
action.Should()

0 commit comments

Comments
 (0)