Skip to content

Commit

Permalink
enhance: synchronize file manipulation for built-ins
Browse files Browse the repository at this point in the history
Use named locks to prevent concurrent writes to files manipulated by
built-in tools. Locks are named after the file they protect. Lock
acquisition has no guaranteed order. A more complex solution should
be considered if a guaranteed order is desired; e.g. FIFO.

Signed-off-by: Nick Hale <4175918+njhale@users.noreply.github.com>
  • Loading branch information
njhale committed Feb 24, 2024
1 parent a2daea4 commit 90e52e4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.0
replace github.com/sashabaranov/go-openai => github.com/gptscript-ai/go-openai v0.0.0-20240206232711-45b6e096246a

require (
github.com/BurntSushi/locker v0.0.0-20171006230638-a6e239ea1c69
github.com/acorn-io/broadcaster v0.0.0-20240105011354-bfadd4a7b45d
github.com/acorn-io/cmd v0.0.0-20240203032901-e9e631185ddb
github.com/adrg/xdg v0.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/BurntSushi/locker v0.0.0-20171006230638-a6e239ea1c69 h1:+tu3HOoMXB7RXEINRVIpxJCT+KdYiI7LAEAUrOw3dIU=
github.com/BurntSushi/locker v0.0.0-20171006230638-a6e239ea1c69/go.mod h1:L1AbZdiDllfyYH5l5OkAaZtk7VkWe89bPJFmnDBNHxg=
github.com/acorn-io/baaah v0.0.0-20240119160309-2a58ee757bbd h1:Zbau2J6sEPl1H4gqnEx4/TI55eZncQR5cjfPOcG2lxE=
github.com/acorn-io/baaah v0.0.0-20240119160309-2a58ee757bbd/go.mod h1:13nTO3svO8zTD3j9E5c86tCtK5YrKsK5sxca4Lwkbc0=
github.com/acorn-io/broadcaster v0.0.0-20240105011354-bfadd4a7b45d h1:hfpNQkJ4I2b8+DbMr8m97gG67ku0uPsMzUfskVu3cHU=
Expand Down
17 changes: 17 additions & 0 deletions pkg/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sort"
"strings"

"github.com/BurntSushi/locker"
"github.com/gptscript-ai/gptscript/pkg/types"
"github.com/jaytaylor/html2text"
)
Expand Down Expand Up @@ -242,6 +243,10 @@ func SysRead(ctx context.Context, env []string, input string) (string, error) {
return "", err
}

// Lock the file to prevent concurrent writes from other tool calls.
locker.RLock(params.Filename)
defer locker.RUnlock(params.Filename)

log.Debugf("Reading file %s", params.Filename)
data, err := os.ReadFile(params.Filename)
if err != nil {
Expand All @@ -260,6 +265,10 @@ func SysWrite(ctx context.Context, env []string, input string) (string, error) {
return "", err
}

// Lock the file to prevent concurrent writes from other tool calls.
locker.Lock(params.Filename)
defer locker.Unlock(params.Filename)

data := []byte(params.Content)
msg := fmt.Sprintf("Wrote %d bytes to file %s", len(data), params.Filename)
log.Debugf(msg)
Expand All @@ -276,6 +285,10 @@ func SysAppend(ctx context.Context, env []string, input string) (string, error)
return "", err
}

// Lock the file to prevent concurrent writes from other tool calls.
locker.Lock(params.Filename)
defer locker.Unlock(params.Filename)

f, err := os.OpenFile(params.Filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return "", err
Expand Down Expand Up @@ -410,6 +423,10 @@ func SysRemove(ctx context.Context, env []string, input string) (string, error)
return "", err
}

// Lock the file to prevent concurrent writes from other tool calls.
locker.Lock(params.Location)
defer locker.Unlock(params.Location)

return fmt.Sprintf("Removed file: %s", params.Location), os.Remove(params.Location)
}

Expand Down

0 comments on commit 90e52e4

Please sign in to comment.