Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-threaded access to a box #22

Closed
rerosum opened this issue Mar 12, 2020 · 2 comments
Closed

Multi-threaded access to a box #22

rerosum opened this issue Mar 12, 2020 · 2 comments

Comments

@rerosum
Copy link

rerosum commented Mar 12, 2020

Hello,

Can you help me understand how to avoid a segmentation violation when two threads are accessing the same box?

Here is the output from running the attached sample project:

bob@TRADE-SERVER:~/DEV/testObjectBox$ ./testObjectBox
People count: 0
[Thread] counter: 0, people: 0
[Thread] counter: 0, people: 0
[Thread] counter: 1000, people: 1940
[Thread] counter: 1000, people: 2064
People count: 2535
[Thread] counter: 2000, people: 3918
[Thread] counter: 2000, people: 4081
People count: 4813
[Thread] counter: 3000, people: 5916
[Thread] counter: 3000, people: 6079
People count: 6904
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x2 addr=0x7f9730005f88 pc=0x7f9730005f88]

runtime stack:
runtime.throw(0x68c601, 0x2a)
/usr/local/go/src/runtime/panic.go:774 +0x72
runtime.sigpanic()
/usr/local/go/src/runtime/signal_unix.go:378 +0x47c

goroutine 35 [syscall, locked to thread]:
runtime.cgocall(0x5f6b90, 0xc000061c08, 0xc000024000)
/usr/local/go/src/runtime/cgocall.go:128 +0x5b fp=0xc000061bd8 sp=0xc000061ba0 pc=0x408c2b
github.com/objectbox/objectbox-go/objectbox._Cfunc_obx_query_find(0x7f96c8001c00, 0x0, 0x0, 0x0)
_cgo_gotypes.go:1649 +0x4e fp=0xc000061c08 sp=0xc000061bd8 pc=0x5d5efe
github.com/objectbox/objectbox-go/objectbox.(*Query).Find.func1.1(0xc0004cbae0, 0xc000061c98)
/home/bob/go/pkg/mod/github.com/objectbox/objectbox-go@v1.1.0/objectbox/query.go:90 +0x89 fp=0xc000061c58 sp=0xc000061c08 pc=0x5ea079
github.com/objectbox/objectbox-go/objectbox.(*Query).Find.func1(0x300000002)
/home/bob/go/pkg/mod/github.com/objectbox/objectbox-go@v1.1.0/objectbox/query.go:90 +0x2a fp=0xc000061c78 sp=0xc000061c58 pc=0x5ea0ca
github.com/objectbox/objectbox-go/objectbox.cGetBytesArray(0xc000061e80, 0xc000061d30, 0xc000000000, 0x409295, 0xc000056700, 0xc000061d20)
/home/bob/go/pkg/mod/github.com/objectbox/objectbox-go@v1.1.0/objectbox/c-tools.go:71 +0x30 fp=0xc000061cc8 sp=0xc000061c78 pc=0x5dc650
github.com/objectbox/objectbox-go/objectbox.(*Box).readManyObjects.func1(0xc000061d80, 0x7f9728007f90)
/home/bob/go/pkg/mod/github.com/objectbox/objectbox-go@v1.1.0/objectbox/box.go:552 +0x5c fp=0xc000061d70 sp=0xc000061cc8 pc=0x5e75cc
github.com/objectbox/objectbox-go/objectbox.(*ObjectBox).runInTxn(0xc00019b6b0, 0xa76401, 0xc000061e00, 0x0, 0x0)
/home/bob/go/pkg/mod/github.com/objectbox/objectbox-go@v1.1.0/objectbox/objectbox.go:148 +0xa1 fp=0xc000061dd8 sp=0xc000061d70 pc=0x5de061
github.com/objectbox/objectbox-go/objectbox.(*ObjectBox).RunInReadTx(...)
/home/bob/go/pkg/mod/github.com/objectbox/objectbox-go@v1.1.0/objectbox/objectbox.go:105
github.com/objectbox/objectbox-go/objectbox.(*Box).readManyObjects(0xc00016c940, 0x1, 0xc000061e80, 0x0, 0x0, 0x0, 0xc000061e90)
/home/bob/go/pkg/mod/github.com/objectbox/objectbox-go@v1.1.0/objectbox/box.go:551 +0x8a fp=0xc000061e38 sp=0xc000061dd8 pc=0x5da1ba
github.com/objectbox/objectbox-go/objectbox.(*Query).Find(0xc0004cbae0, 0xc000012170, 0x1, 0x1, 0xc0004cbae0)
/home/bob/go/pkg/mod/github.com/objectbox/objectbox-go@v1.1.0/objectbox/query.go:92 +0x6f fp=0xc000061ea0 sp=0xc000061e38 pc=0x5de8ff
testObjectBox/person.(*PersonQuery).Find(...)
/home/bob/DEV/testObjectBox/person/person.obx.go:359
main.read(0xc0001701c8, 0xc0001701c8, 0x1cdb, 0x1cdb, 0x0, 0x0)
/home/bob/DEV/testObjectBox/main.go:80 +0xf1 fp=0xc000061f20 sp=0xc000061ea0 pc=0x5f5961
main.loop(0xc00019b6b0, 0xc0001701c8, 0xf4240)
/home/bob/DEV/testObjectBox/main.go:50 +0xbf fp=0xc000061fc8 sp=0xc000061f20 pc=0x5f554f
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1357 +0x1 fp=0xc000061fd0 sp=0xc000061fc8 pc=0x45eb61
created by main.main
/home/bob/DEV/testObjectBox/main.go:23 +0xf7

goroutine 1 [sleep]:
runtime.goparkunlock(...)
/usr/local/go/src/runtime/proc.go:310
time.Sleep(0x4a817c800)
/usr/local/go/src/runtime/time.go:105 +0x157
main.main()
/home/bob/DEV/testObjectBox/main.go:33 +0x18f

goroutine 34 [semacquire]:
sync.runtime_SemacquireMutex(0xa86c94, 0xc000065e00, 0x1)
/usr/local/go/src/runtime/sema.go:71 +0x47
sync.(*Mutex).lockSlow(0xa86c90)
/usr/local/go/src/sync/mutex.go:138 +0xfc
sync.(*Mutex).Lock(...)
/usr/local/go/src/sync/mutex.go:81
sync.(*RWMutex).Lock(0xa86c90)
/usr/local/go/src/sync/rwmutex.go:98 +0x97
main.read(0xc0001701c8, 0xc0001701c8, 0x1cdb, 0x1cdb, 0x0, 0x0)
/home/bob/DEV/testObjectBox/main.go:79 +0xe3
main.loop(0xc00019b6b0, 0xc0001701c8, 0xf4240)
/home/bob/DEV/testObjectBox/main.go:50 +0xbf
created by main.main
/home/bob/DEV/testObjectBox/main.go:22 +0xc2
testObjectBox.zip

@vaind
Copy link
Collaborator

vaind commented Mar 13, 2020

I can reproduce and will get back to you once I know what's the issue.

@vaind
Copy link
Collaborator

vaind commented Mar 18, 2020

The issue was with the query finalizer sometimes being executed before the native (CGO) query call finished. It's fixed in v1.1.2, please upgrade.

Also, I've noticed mutex/locks and explicit transactions in your code - it's not necessary in your case - box and query APIs manage their transactions internally when needed. Having said that, it still makes sense to do explicit transactions, e.g. to improve performance or to maintain consistency among multiple DB operations, it just wasn't the case with your code. See https://golang.objectbox.io/transactions#explicit-transactions for more details.

@vaind vaind closed this as completed Mar 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants