Comments (6)
This is a race condition. Just don't call deepcopy.Copy
on a structure that is being modified by another goroutine.
You can detect such problems with the race detector:
$ GO111MODULE=off go run -trimpath -race main.go
demo:0xc000132018 &[a b]
==================
WARNING: DATA RACE
Read at 0x00c000132018 by goroutine 8:
main.increaseSlice()
command-line-arguments/main.go:31 +0x6e
Previous write at 0x00c000132018 by goroutine 7:
main.increaseSlice()
command-line-arguments/main.go:31 +0xf3
Goroutine 8 (running) created at:
main.copySlice()
command-line-arguments/main.go:20 +0x1d0
main.main()
command-line-arguments/main.go:13 +0x2f
Goroutine 7 (finished) created at:
main.copySlice()
command-line-arguments/main.go:20 +0x1d0
main.main()
command-line-arguments/main.go:13 +0x2f
==================
==================
WARNING: DATA RACE
Read at 0x00c000094040 by goroutine 12:
runtime.growslice()
runtime/slice.go:125 +0x0
main.increaseSlice()
command-line-arguments/main.go:31 +0x16f
Previous write at 0x00c000094040 by goroutine 7:
main.increaseSlice()
command-line-arguments/main.go:31 +0xbb
Goroutine 12 (running) created at:
main.copySlice()
command-line-arguments/main.go:20 +0x1d0
main.main()
command-line-arguments/main.go:13 +0x2f
Goroutine 7 (finished) created at:
main.copySlice()
command-line-arguments/main.go:20 +0x1d0
main.main()
command-line-arguments/main.go:13 +0x2f
==================
==================
WARNING: DATA RACE
Write at 0x00c000098210 by goroutine 25:
main.increaseSlice()
command-line-arguments/main.go:31 +0xbb
Previous write at 0x00c000098210 by goroutine 20:
main.increaseSlice()
command-line-arguments/main.go:31 +0xbb
Goroutine 25 (running) created at:
main.copySlice()
command-line-arguments/main.go:20 +0x1d0
main.main()
command-line-arguments/main.go:13 +0x2f
Goroutine 20 (running) created at:
main.copySlice()
command-line-arguments/main.go:20 +0x1d0
main.main()
command-line-arguments/main.go:13 +0x2f
==================
==================
WARNING: DATA RACE
Read at 0x00c0000bb000 by main goroutine:
reflect.typedmemmove()
runtime/mbarrier.go:177 +0x0
reflect.packEface()
reflect/value.go:121 +0x12f
reflect.valueInterface()
reflect/value.go:1046 +0x1cd
reflect.Value.Interface()
reflect/value.go:1016 +0x10b1
github.com/mohae/deepcopy.copyRecursive()
github.com/mohae/deepcopy/deepcopy.go:50 +0x10b2
github.com/mohae/deepcopy.copyRecursive()
github.com/mohae/deepcopy/deepcopy.go:106 +0xedc
github.com/mohae/deepcopy.copyRecursive()
github.com/mohae/deepcopy/deepcopy.go:67 +0x106f
github.com/mohae/deepcopy.Copy()
github.com/mohae/deepcopy/deepcopy.go:39 +0x16c
main.copySlice()
command-line-arguments/main.go:23 +0x20e
main.main()
command-line-arguments/main.go:13 +0x2f
Previous write at 0x00c0000bb000 by goroutine 75:
main.increaseSlice()
command-line-arguments/main.go:31 +0xbb
Goroutine 75 (finished) created at:
main.copySlice()
command-line-arguments/main.go:20 +0x1d0
main.main()
command-line-arguments/main.go:13 +0x2f
==================
dst:0xc00019e018 1461 &[a b c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c]
Found 4 data race(s)
exit status 66
from deepcopy.
Please provide example of code that fails.
Here is a template: https://play.golang.org/p/o3RnrONhIM-
from deepcopy.
deepcopy.go
case reflect.Slice:
if original.IsNil() {
return
}
// Make a new slice and copy each element.
fmt.Println("demo length", original.Len()) // len == 2
cpy.Set(reflect.MakeSlice(original.Type(), original.Len(), original.Cap()))
time.Sleep(time.Second * 2)
fmt.Println("demo length", original.Len()) // len == 3
fmt.Println("cpy Cap", cpy.Cap()) // cap == 2
for i := 0; i < original.Len(); i++ {
copyRecursive(original.Index(i), cpy.Index(i))
}
test file
type sliceType []string
func copySlice() {
demo := &sliceType{"a", "b"}
fmt.Printf("demo:%p %v\n", demo, demo)
go increaseSlice(demo)
cpy := deepcopy.Copy(demo)
dst := cpy.(*sliceType)
fmt.Printf("dst:%p %v\n", dst, dst)
}
func increaseSlice(s *sliceType) {
time.Sleep(time.Second)
*s = append(*s, "c")
fmt.Println("increase length", len(*s))
}
from deepcopy.
result
demo:0xc000004480 &[a b]
demo length 2
increase length 3
demo length 3
cpy Cap 2
panic: reflect: slice index out of range
from deepcopy.
https://play.golang.org/p/WmVUZ_RLaY_F
from deepcopy.
Please perform the test several times more, if it fails to reappear the first time.
from deepcopy.
Related Issues (15)
- Format strings in test file are invalid HOT 1
- Could Copy(src, dst) be supported? HOT 1
- DeepCopy interface implementation doesn't improve performance
- proposed fix for a nil case HOT 3
- Not an issue, this repo just saved my day.
- Custom copy HOT 7
- Does this repo still being maintained? HOT 2
- Can you provide the performance test data
- error can not be copyed HOT 1
- facing issue while call deepcopy HOT 2
- There are cases we cannot deepcopy HOT 4
- Make public copyRecursive(original, cpy reflect.Value) HOT 2
- copy unexported field HOT 2
- Map keys aren't deep-copied HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deepcopy.