Skip to content

Commit

Permalink
fix race condition in isPatchable func and remove a test (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyredondo authored Jun 17, 2020
1 parent e07aeae commit f29c4c9
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 43 deletions.
2 changes: 2 additions & 0 deletions patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func (p *Patch) Unpatch() error {
}

func isPatchable(target, redirection *reflect.Value) error {
patchLock.Lock()
defer patchLock.Unlock()
if target.Kind() != reflect.Func || redirection.Kind() != reflect.Func {
return errors.New("the target and/or redirection is not a Func")
}
Expand Down
43 changes: 0 additions & 43 deletions patcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package mpatch

import (
"reflect"
"runtime"
"testing"
"time"
)

//go:noinline
Expand Down Expand Up @@ -136,44 +134,3 @@ func TestInstanceValuePatcher(t *testing.T) {
t.Fatal("The unpatch did not work")
}
}

var slice []int

//go:noinline
func TestGarbageCollectorExperiment(t *testing.T) {

for i := 0; i < 10000000; i++ {
slice = append(slice, i)
}
go func() {
var sl []int
for i := 0; i < 10000000; i++ {
sl = append(slice, i)
}
_ = sl
}()
<-time.After(time.Second)

aVal := methodA
ptr01 := reflect.ValueOf(aVal).Pointer()
slice = nil
runtime.GC()
for i := 0; i < 10000000; i++ {
slice = append(slice, i)
}
go func() {
var sl []int
for i := 0; i < 10000000; i++ {
sl = append(slice, i)
}
_ = sl
}()
<-time.After(time.Second)
slice = nil
runtime.GC()
ptr02 := reflect.ValueOf(aVal).Pointer()

if ptr01 != ptr02 {
t.Fail()
}
}

0 comments on commit f29c4c9

Please sign in to comment.