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

Make object snapshots/invocations resilient to closes #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 19 additions & 2 deletions durable/durablewazero/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type object struct {
sync.Mutex
instance wapc.Instance
onClose func()
closed bool
}

func newObject(
Expand All @@ -28,6 +29,7 @@ func newObject(
return &object{
instance: instance,
onClose: onClose,
closed: false,
}
}

Expand All @@ -43,17 +45,23 @@ func (o *object) Invoke(
o.Lock()
defer o.Unlock()

if o.closed {
return nil, fmt.Errorf("error snapshotting object: Close() was already called on object")
}
// TODO: Make byte ownership more clear?
return o.instance.Invoke(ctx, operation, payload)
}

// TODO: Make this resilient to double-close.
// TODO: Other methods like Snapshot/Invoke etc should return error after close.
func (o *object) Close(ctx context.Context) error {
o.Lock()
defer o.Unlock()

if o.closed {
return nil
}

o.onClose()
o.closed = true
return o.instance.Close(ctx)
}

Expand All @@ -64,6 +72,9 @@ func (o *object) Snapshot(
o.Lock()
defer o.Unlock()

if o.closed {
return fmt.Errorf("error snapshotting object: Close() was already called on object")
}
memory := o.instance.(*wazero.Instance).UnwrapModule().Memory()
bytes, ok := memory.Read(0, memory.Size())
if !ok {
Expand All @@ -89,6 +100,9 @@ func (o *object) SnapshotIncremental(
o.Lock()
defer o.Unlock()

if o.closed {
return fmt.Errorf("error snapshotting object: Close() was already called on object")
}
memory := o.instance.(*wazero.Instance).UnwrapModule().Memory()
memBytes, ok := memory.Read(0, memory.Size())
if !ok {
Expand All @@ -115,6 +129,9 @@ func (o *object) Hydrate(
o.Lock()
defer o.Unlock()

if o.closed {
return fmt.Errorf("error snapshotting object: Close() was already called on object")
}
var (
memory = o.instance.(*wazero.Instance).UnwrapModule().Memory()
memSize = int(memory.Size())
Expand Down
31 changes: 31 additions & 0 deletions durable/durablewazero/object_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package durablewazero

import (
"context"
"github.com/stretchr/testify/require"
"github.com/wapc/wapc-go/engines/wazero"
"testing"
)

func TestDoubleClose(t *testing.T) {
ctx := context.Background()

module, err := NewModule(ctx, wazero.Engine(), testHost, utilWasmBytes)
require.NoError(t, err)

object, err := module.Instantiate(ctx, "a")
require.NoError(t, err)

err = object.Close(ctx)
require.NoError(t, err)

err = object.Close(ctx)
require.Nil(t, err)

resp, err := object.Invoke(ctx, "", nil)
require.Nil(t, resp)
require.Error(t, err)

err = object.Snapshot(ctx, nil)
require.Error(t, err)
}