Skip to content

Commit

Permalink
file: Add lock to not conflict with async tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKar committed May 26, 2024
1 parent 49c3a6e commit ea4176c
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import (
"io"
"os"
"path/filepath"
"sync"

"github.com/zyedidia/micro/v2/internal/util"
"golang.org/x/text/encoding"
"golang.org/x/text/transform"
)

var (
lock sync.Mutex
)

type OptionalWriteSteps struct {
PreWrite func() (io.WriteCloser, encoding.Encoding, error)
PostWrite func() error
Expand All @@ -22,6 +27,8 @@ type OptionalWriteSteps struct {
func Overwrite(name string, write func(io.Writer) error, optSteps ...OptionalWriteSteps) (err error) {
var writeCloser io.WriteCloser
enc := encoding.Nop
lock.Lock()
defer lock.Unlock()
for _, optional := range optSteps {
tmpEnc := encoding.Nop
writeCloser, tmpEnc, err = optional.PreWrite()
Expand Down

0 comments on commit ea4176c

Please sign in to comment.