Skip to content

Commit

Permalink
fix: runtime init panic
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Jul 26, 2024
1 parent 76ecc85 commit dd87a33
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 26 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/main_darwin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ on:
- "releases/*"
jobs:
build-and-test:
# Ref: https://github.com/actions/runner-images/tree/main/images/macos
# Note: The arch of macos-13-xlarge and macos-14 is arm64
strategy:
matrix:
os: [ macos-12, macos-13 ]
os: [ macos-12, macos-13, macos-13-xlarge, macos-14 ]
runs-on: ${{ matrix.os }}
steps:
- name: Git checkout
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v3 v3.0.1
kcl-lang.io/kpm v0.9.2
kcl-lang.io/kpm v0.9.3
kcl-lang.io/lib v0.10.0-alpha.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1287,8 +1287,8 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5 h1:kmDqav+P+/5e1i9tFfHq1qcF3sOrDp+YEkVDAHu7Jwk=
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
kcl-lang.io/kpm v0.9.2 h1:TNQLFWEYj9MEFZXpHGhp4/PHAv3eBogOjpYDyMIFZsU=
kcl-lang.io/kpm v0.9.2/go.mod h1:4HTbPhB4DVtkamnIv5lrRcHtCVwzp4Pf/b+d3TN4nzs=
kcl-lang.io/kpm v0.9.3 h1:lwFSrJTRyAzjygT5Oo/98DxCCiDZmujo46PcmRx54Ck=
kcl-lang.io/kpm v0.9.3/go.mod h1:4HTbPhB4DVtkamnIv5lrRcHtCVwzp4Pf/b+d3TN4nzs=
kcl-lang.io/lib v0.10.0-alpha.1 h1:GMVB75Pc1W29gcl1WlVgdgUscH6h+d9No0fzBH0rNYg=
kcl-lang.io/lib v0.10.0-alpha.1/go.mod h1:tu+tzwGgHLzYZSIxUG/ntipStrxZd6OvutWYPTxS7cs=
oras.land/oras-go v1.2.5 h1:XpYuAwAb0DfQsunIyMfeET92emK8km3W4yEzZvUbsTo=
Expand Down
16 changes: 0 additions & 16 deletions pkg/kcl/native_service.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/runtime/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func initRuntime(maxProc int) {

// ping
{
args := &gpyrpc.Ping_Args{Value: "ping: kcl-go rest-server"}
args := &gpyrpc.Ping_Args{Value: "echo"}
resp, err := client.Ping(args)
if err != nil || resp.Value != args.Value {
fmt.Println("Init kcl runtime failed, path: ", MustGetKclvmPath())
fmt.Println("Init kcl runtime failed, path:", MustGetKclvmPath())
fmt.Println(tip)
fmt.Printf("If not, you can run `rm -r %s/bin` to fix this issue\n", MustGetKclvmPath())
panic(err)
Expand Down
28 changes: 28 additions & 0 deletions pkg/runtime/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright The KCL Authors. All rights reserved.

package runtime

import (
"sync"
"testing"
)

func TestInitRuntimeConcurrency(t *testing.T) {
var wg sync.WaitGroup
numConcurrentCalls := 10

for i := 0; i < numConcurrentCalls; i++ {
wg.Add(1)
go func(maxProc int) {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
t.Errorf("initRuntime panicked: %v", err)
}
}()
initRuntime(maxProc)
}(2)
}

wg.Wait()
}
15 changes: 11 additions & 4 deletions pkg/runtime/kclvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,19 @@ func installKclArtifact() {
lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
locked, err := fileLock.TryLockContext(lockCtx, time.Second)
if err == nil && locked {
defer fileLock.Unlock()
}
if err != nil {
logger.GetLogger().Warningf("install kclvm failed: %s", err.Error())
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
err = install.InstallKclvm(path)
if err != nil {
Expand Down

0 comments on commit dd87a33

Please sign in to comment.