From 4aaddd923ea5fb4c2bb42399e23580c8d0444fc7 Mon Sep 17 00:00:00 2001 From: peefy Date: Fri, 26 Jul 2024 20:26:11 +0800 Subject: [PATCH] chore: add more lock Signed-off-by: peefy --- pkg/runtime/init.go | 2 ++ pkg/runtime/kclvm.go | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/runtime/init.go b/pkg/runtime/init.go index 07ac5ca3..fc5e6463 100644 --- a/pkg/runtime/init.go +++ b/pkg/runtime/init.go @@ -7,6 +7,7 @@ import ( "runtime" "sync" + "kcl-lang.io/kcl-go/pkg/logger" "kcl-lang.io/kcl-go/pkg/spec/gpyrpc" ) @@ -41,6 +42,7 @@ func initRuntime(maxProc int) { rpcRuntime = NewRuntime(int(maxProc), "kclvm_cli", "server") rpcRuntime.Start() + logger.GetLogger().Errorf("start rpc runtime...") client := &BuiltinServiceClient{ Runtime: rpcRuntime, diff --git a/pkg/runtime/kclvm.go b/pkg/runtime/kclvm.go index 39b71ab8..c3cf9120 100644 --- a/pkg/runtime/kclvm.go +++ b/pkg/runtime/kclvm.go @@ -3,12 +3,14 @@ package runtime import ( + "context" _ "embed" "errors" "os" "os/exec" "path/filepath" "runtime" + "time" "github.com/gofrs/flock" "kcl-lang.io/kcl-go/pkg/env" @@ -34,21 +36,30 @@ func installKclArtifact() { // Acquire a file lock for process synchronization lockPath := filepath.Join(path, "init.lock") fileLock := flock.New(lockPath) - err = fileLock.Lock() + lockCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + logger.GetLogger().Errorf("locking...") + locked, err := fileLock.TryLockContext(lockCtx, time.Second) if err != nil { logger.GetLogger().Warningf("attempt to acquire file lock failed: %s", err.Error()) return } + if !locked { + logger.GetLogger().Warning("unable to acquire file lock") + return + } defer func() { if err := fileLock.Unlock(); err != nil { logger.GetLogger().Errorf("failed to unlock file: %s", err.Error()) } }() // Install lib + logger.GetLogger().Errorf("installing...") err = install.InstallKclvm(path) if err != nil { logger.GetLogger().Warningf("install kclvm failed: %s", err.Error()) } + logger.GetLogger().Errorf("unlocking...") } var (