Skip to content

Commit

Permalink
Fix race condition in the JS VM cache (#4074)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Plourde <[email protected]>
  • Loading branch information
Simon Plourde authored Oct 27, 2020
1 parent 4d5b16a commit 62478c4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed
- Fixed a crash in the backend and agent related to Javascript execution.

## [6.1.1] - 2020-10-22

### Fixed
Expand Down
23 changes: 23 additions & 0 deletions js/js_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package js

import (
"testing"

corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/types/dynamic"
)

// This is a unit test to cover the race condition found in
// https://github.com/sensu/sensu-go/issues/4073
func TestEvaluateRaceCondition(t *testing.T) {
entity := corev2.FixtureEntity("foo")
synth := dynamic.Synthesize(entity)
params := map[string]interface{}{"entity": synth}

go func() {
_, _ = Evaluate("true", params, nil)
}()
go func() {
_, _ = Evaluate("true", params, nil)
}()
}
5 changes: 4 additions & 1 deletion js/vm_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,8 @@ func (c *vmCache) Dispose(key string) {
// Init initializes the value in the cache.
func (c *vmCache) Init(key string, vm *otto.Otto) {
val := &cacheValue{lastRead: time.Now().Unix(), vm: vm}
c.vms.Store(key, val)
// Do not replace the cache value if it already exists, since it's possible it
// was created after we determined it was missing, otherwise we might try to
// unlock the same mutex twice and therefore create a fatal error
_, _ = c.vms.LoadOrStore(key, val)
}

0 comments on commit 62478c4

Please sign in to comment.