Skip to content

fix: ensure fd monitor file is closed at end of the script run #391

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

Merged
merged 1 commit into from
May 28, 2024
Merged
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
34 changes: 23 additions & 11 deletions pkg/monitor/fd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/gptscript-ai/gptscript/pkg/runner"
Expand All @@ -22,7 +23,8 @@ type Event struct {
}

type fileFactory struct {
file *os.File
file *os.File
runningCount atomic.Int32
}

// NewFileFactory creates a new monitor factory that writes events to the location specified.
Expand All @@ -44,23 +46,26 @@ func NewFileFactory(loc string) (runner.MonitorFactory, error) {

file = os.NewFile(uintptr(fd), "events")
} else {
file, err = os.OpenFile(loc, os.O_WRONLY|os.O_CREATE, 0)
file, err = os.OpenFile(loc, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return nil, err
}
}

return &fileFactory{
file: file,
file: file,
runningCount: atomic.Int32{},
}, nil
}

func (s fileFactory) Start(_ context.Context, prg *types.Program, env []string, input string) (runner.Monitor, error) {
func (s *fileFactory) Start(_ context.Context, prg *types.Program, env []string, input string) (runner.Monitor, error) {
s.runningCount.Add(1)
fd := &fd{
prj: prg,
env: env,
input: input,
file: s.file,
prj: prg,
env: env,
input: input,
file: s.file,
factory: s,
}

fd.event(Event{
Expand All @@ -74,12 +79,21 @@ func (s fileFactory) Start(_ context.Context, prg *types.Program, env []string,
return fd, nil
}

func (s *fileFactory) close() {
if count := s.runningCount.Add(-1); count == 0 {
if err := s.file.Close(); err != nil {
log.Errorf("error closing monitor file: %v", err)
}
}
}

type fd struct {
prj *types.Program
env []string
input string
file *os.File
runLock sync.Mutex
factory *fileFactory
}

func (f *fd) Event(event runner.Event) {
Expand Down Expand Up @@ -117,9 +131,7 @@ func (f *fd) Stop(output string, err error) {
}

f.event(e)
if err = f.file.Close(); err != nil {
log.Errorf("Failed to close file: %v", err)
}
f.factory.close()
}

func (f *fd) Pause() func() {
Expand Down