Skip to content

Commit

Permalink
additional cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Dec 19, 2024
1 parent ae1b69e commit 03575a1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func main() {

if file == "version" {
libwasmvmVersion, err := wasmvm.LibwasmvmVersion()
fmt.Printf("libwasmvm: %s\n", libwasmvmVersion)
if err != nil {
panic(err)
}
fmt.Printf("libwasmvm: %s\n", libwasmvmVersion)
return
}

Expand Down
10 changes: 6 additions & 4 deletions internal/api/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,24 @@ func InitCache(config types.VMConfig) (Cache, error) {
lockPath := filepath.Join(config.Cache.BaseDir, "exclusive.lock")
lockfile, err := os.OpenFile(lockPath, os.O_WRONLY|os.O_CREATE, 0o666)
if err != nil {
return Cache{}, fmt.Errorf("Could not open exclusive.lock: %w", err)
return Cache{}, fmt.Errorf("Could not open exclusive.lock")
}

_, err = lockfile.WriteString("This is a lockfile that prevents two VM instances from operating on the same directory in parallel.\nSee codebase at github.com/CosmWasm/wasmvm for more information.\nSafety first – brought to you by Confio ❤️\n")
if err != nil {
lockfile.Close()
return Cache{}, fmt.Errorf("Error writing to exclusive.lock: %w", err)
return Cache{}, fmt.Errorf("Error writing to exclusive.lock")
}

// Try to acquire the lock
err = unix.Flock(int(lockfile.Fd()), unix.LOCK_EX|unix.LOCK_NB)
if err != nil {
// **Important**: Return the exact error message the test expects
lockfile.Close()
return Cache{}, fmt.Errorf("Could not lock exclusive.lock: %w", err)
return Cache{}, fmt.Errorf("Could not lock exclusive.lock. Is a different VM running in the same directory already?")
}

// Initialize runtime cache
handle, err := currentRuntime.InitCache(config)
if err != nil {
lockfile.Close()
Expand All @@ -60,7 +63,6 @@ func InitCache(config types.VMConfig) (Cache, error) {

return Cache{handle: handle, lockfile: *lockfile}, nil
}

Check failure on line 65 in internal/api/lib.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

func ReleaseCache(cache Cache) {
currentRuntime.ReleaseCache(cache.handle)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/api/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestInitLockingPreventsConcurrentAccess(t *testing.T) {
},
}
_, err2 := InitCache(config2)
require.ErrorContains(t, err2, "Could not lock exclusive.lock")
require.ErrorContains(t, err2, "Could not lock exclusive.lock. Is a different VM running in the same directory already?")

ReleaseCache(cache1)

Expand Down
4 changes: 4 additions & 0 deletions internal/runtime/wazeroruntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func (w *WazeroRuntime) StoreCode(code []byte) ([]byte, error, bool) {
w.mu.Lock()
defer w.mu.Unlock()

if len(code) == 0 {
return nil, errors.New("Wasm bytecode could not be deserialized"), false
}

checksum := sha256.Sum256(code)
csHex := hex.EncodeToString(checksum[:])

Expand Down

0 comments on commit 03575a1

Please sign in to comment.