Skip to content

Commit f29c4c9

Browse files
authored
fix race condition in isPatchable func and remove a test (#10)
1 parent e07aeae commit f29c4c9

File tree

2 files changed

+2
-43
lines changed

2 files changed

+2
-43
lines changed

patcher.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ func (p *Patch) Unpatch() error {
113113
}
114114

115115
func isPatchable(target, redirection *reflect.Value) error {
116+
patchLock.Lock()
117+
defer patchLock.Unlock()
116118
if target.Kind() != reflect.Func || redirection.Kind() != reflect.Func {
117119
return errors.New("the target and/or redirection is not a Func")
118120
}

patcher_test.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package mpatch
22

33
import (
44
"reflect"
5-
"runtime"
65
"testing"
7-
"time"
86
)
97

108
//go:noinline
@@ -136,44 +134,3 @@ func TestInstanceValuePatcher(t *testing.T) {
136134
t.Fatal("The unpatch did not work")
137135
}
138136
}
139-
140-
var slice []int
141-
142-
//go:noinline
143-
func TestGarbageCollectorExperiment(t *testing.T) {
144-
145-
for i := 0; i < 10000000; i++ {
146-
slice = append(slice, i)
147-
}
148-
go func() {
149-
var sl []int
150-
for i := 0; i < 10000000; i++ {
151-
sl = append(slice, i)
152-
}
153-
_ = sl
154-
}()
155-
<-time.After(time.Second)
156-
157-
aVal := methodA
158-
ptr01 := reflect.ValueOf(aVal).Pointer()
159-
slice = nil
160-
runtime.GC()
161-
for i := 0; i < 10000000; i++ {
162-
slice = append(slice, i)
163-
}
164-
go func() {
165-
var sl []int
166-
for i := 0; i < 10000000; i++ {
167-
sl = append(slice, i)
168-
}
169-
_ = sl
170-
}()
171-
<-time.After(time.Second)
172-
slice = nil
173-
runtime.GC()
174-
ptr02 := reflect.ValueOf(aVal).Pointer()
175-
176-
if ptr01 != ptr02 {
177-
t.Fail()
178-
}
179-
}

0 commit comments

Comments
 (0)