Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle.IsClosed Checks #289

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ internal Handle NativeHandle
{
get
{
if (handle.IsInvalid)
if (handle.IsInvalid || handle.IsClosed)
{
throw new ObjectDisposedException(typeof(Config).FullName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal Handle NativeHandle
{
get
{
if (handle.IsInvalid)
if (handle.IsInvalid || handle.IsClosed)
{
throw new ObjectDisposedException(typeof(Engine).FullName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ internal Handle NativeHandle
{
get
{
if (handle.IsInvalid)
if (handle.IsInvalid || handle.IsClosed)
{
throw new ObjectDisposedException(typeof(Module).FullName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ internal Handle NativeHandle
{
get
{
if (handle.IsInvalid)
if (handle.IsInvalid || handle.IsClosed)
{
throw new ObjectDisposedException(typeof(Store).FullName);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/ConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,17 @@ public void ItSetsMultiValue()

act.Should().Throw<WasmtimeException>();
}

[Fact]
public void ItCannotBeAccessedOnceDisposed()
{
var config = new Config();
config.Dispose();

Assert.Throws<ObjectDisposedException>(() => config.NativeHandle);
Assert.Throws<ObjectDisposedException>(() => config.WithBulkMemory(true));
Assert.Throws<ObjectDisposedException>(() => config.WithCacheConfig(null));
Assert.Throws<ObjectDisposedException>(() => config.WithEpochInterruption(true));
}
}
}
18 changes: 18 additions & 0 deletions tests/EngineTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using Xunit;

namespace Wasmtime.Tests;

public class EngineTests
{
[Fact]
public void ItCannotBeAccessedOnceDisposed()
{
var engine = new Engine();

engine.Dispose();

Assert.Throws<ObjectDisposedException>(() => engine.NativeHandle);
Assert.Throws<ObjectDisposedException>(() => engine.IncrementEpoch());
}
}
15 changes: 15 additions & 0 deletions tests/ModuleLoadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,20 @@ public void ItLoadsModuleTextFromEmbeddedResource()
// `ObjectDisposedException`
stream.Read(new byte[0], 0, 0);
}

[Fact]
public void ItCannotBeAccessedOnceDisposed()
{
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("hello.wasm");
stream.Should().NotBeNull();

using var engine = new Engine();
var module = Module.FromStream(engine, "hello.wasm", stream);

module.Dispose();

Assert.Throws<ObjectDisposedException>(() => module.NativeHandle);
Assert.Throws<ObjectDisposedException>(() => module.Serialize());
}
}
}
17 changes: 16 additions & 1 deletion tests/StoreTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using System;
using FluentAssertions;
using System.IO;
using Xunit;

Expand Down Expand Up @@ -91,5 +92,19 @@ public void ItLimitsMemories()
var act = () => { new Instance(Store, module); };
act.Should().Throw<WasmtimeException>();
}

[Fact]
public void ItCannotBeAccessedOnceDisposed()
{
var ctx = Store.Context;
Assert.Equal(Store, ctx.Store);

Store.Dispose();

Assert.Throws<ObjectDisposedException>(() => { var x = Store.Context; });
Assert.Throws<ObjectDisposedException>(() => Store.NativeHandle);
Assert.Throws<ObjectDisposedException>(() => Store.Fuel);
Assert.Throws<ObjectDisposedException>(() => Store.GC());
}
}
}
Loading