robertkrimen / otto

A JavaScript interpreter in Go (golang)
http://godoc.org/github.com/robertkrimen/otto
MIT License
8.01k stars 584 forks source link

fatal error: concurrent map write #509

Closed s00500 closed 12 months ago

s00500 commented 1 year ago

I got a pretty strange crash today...

Notice that this is the first time I have seen this:

    github.com/robertkrimen/otto@v0.2.1/object.go:136
 github.com/robertkrimen/otto.(*_object)._write(...)
 goroutine 797 [running]:
 github.com/robertkrimen/otto.objectDefineOwnProperty(0x40025cc120, {0x4000506dd5, 0x5}, {{0x14a2420?, 0x4004239158?}, 0x1cb3c?}, 0x0)
    github.com/robertkrimen/otto@v0.2.1/object_class.go:436 +0x748
 github.com/robertkrimen/otto.(*_object).defineOwnProperty(...)
    github.com/robertkrimen/otto@v0.2.1/object.go:110
    github.com/robertkrimen/otto@v0.2.1/object.go:52
    github.com/robertkrimen/otto@v0.2.1/object_class.go:263 +0xc8
 github.com/robertkrimen/otto.(*_object).put(...)
 github.com/robertkrimen/otto.(*_propertyReference).putValue(0x40041699a8?, {0x1a9c0?, {0x131bda0?, 0x731cec0?}})
 github.com/robertkrimen/otto.objectPut(0x40025cc120, {0x4000506dd5, 0x5}, {0x2, {0x131bda0, 0x731cec0}}, 0x0)
    github.com/robertkrimen/otto@v0.2.1/type_reference.go:45 +0x5c
 github.com/robertkrimen/otto.(*_runtime).putValue(0x400649db00, {0x6b67540?, 0x4004914210?}, {0x4000506dd5?, {0x131bda0?, 0x731cec0?}})
    github.com/robertkrimen/otto@v0.2.1/runtime.go:111 +0x54
    github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_expression.go:450 +0xc0
    github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_statement.go:102 +0x8ec
 github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeStatement(0x400649db00, {0x6b5eca0?, 0x4004238c60})
 github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeVariableExpression(0x400649db00, 0x4004247d10)
    github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate.go:17 +0x160
    github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_statement.go:119 +0x90
    github.com/robertkrimen/otto@v0.2.1/runtime.go:839 +0x30
 github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeProgram(0x400649db00, 0x400421a4d0, 0x0)
 github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeStatementList(0x400649db00?, {0x400421a460?, 0x7, 0x297?})
 github.com/robertkrimen/otto.(*_runtime).cmpl_runOrEval.func1()
 github.com/robertkrimen/otto.catchPanic(0x4005958e68?)
    github.com/robertkrimen/otto@v0.2.1/error.go:255 +0x58
    github.com/robertkrimen/otto@v0.2.1/runtime.go:838 +0x8c
    github.com/robertkrimen/otto@v0.2.1/runtime.go:851
 github.com/robertkrimen/otto.(*_runtime).cmpl_run(...)
 github.com/robertkrimen/otto.(*_runtime).cmpl_runOrEval(0x400649db00, {0x131c660?, 0x40046c2530?}, {0x0?, 0x0?}, 0x0)
    github.com/robertkrimen/otto@v0.2.1/otto.go:293 +0x44
    reactor/dispatch/DScripts.go:218 +0x1a0
    reactor/dispatch/DScripts.go:276 +0x17c
 created by reactor/dispatch.(*Script).ManageState
 github.com/robertkrimen/otto.Otto.Run({0x4001257020?, 0x400649db00?}, {0x131c660?, 0x40046c2530?})
 reactor/dispatch.(*Script).runScript(0x4000a83680)

Any idea what could have caused this exactly ?

Could this change be related: https://github.com/robertkrimen/otto/commit/fc4074c6167d94ae06b7f48f62256973254c6b66 ?

Greetings, Lukas

stevenh commented 1 year ago

concurrent map write indicates two or more goroutines we're trying to access the map at the same time.

The crash should have had multiple stacks should indicate why.

s00500 commented 1 year ago

The application this runs in is pretty big. Searching to the go routine dump I found this second occasion where a function using the otto package is used

reactor/dispatch.(Script).setUpVMFunctions.func2({0x400649db00, 0x0, 0x0, {0x5, {0x148ad20, 0x40025cc120}}, {0x400499e270, 0x1, 0x1}, 0x40049f3200}) github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_expression.go:239 +0x31c github.com/robertkrimen/otto@v0.2.1/type_function.go:200 +0x5c8 reactor/dispatch/DScripts.go:125 +0xdc github.com/robertkrimen/otto.(_object).call(0x40012571a0, {0xd92f0?, {0x148ad20?, 0x40025cc120?}}, {0x400499e270?, 0x1, 0x1}, 0x60?, {0x0, {0x0, ...}, ...}) github.com/robertkrimen/otto.(_runtime).cmpl_evaluate_nodeCallExpression(0x400649db00, 0x400499c750, {0x0, 0x0, 0x0}) github.com/robertkrimen/otto.(_runtime).cmpl_evaluate_nodeExpression(0x400649db00, {0x6b5e980?, 0x400499c750}) github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_expression.go:43 +0x154 github.com/robertkrimen/otto.(_runtime).cmpl_evaluate_nodeAssignExpression(0x48?, 0x400499c780) github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_expression.go:121 +0x5c github.com/robertkrimen/otto.(_runtime).cmpl_evaluate_nodeExpression(0x400649db00, {0x6b5e8e0?, 0x400499c780}) github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_expression.go:30 +0xc0 github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_statement.go:398 +0x20c github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_statement.go:61 +0x3e8 github.com/robertkrimen/otto.(_runtime).cmpl_evaluate_nodeStatement(0x400649db00, {0x6b5ea40?, 0x400435acf0}) github.com/robertkrimen/otto.(_runtime).cmpl_evaluate_nodeStatement(0x400649db00, {0x6b5ecc0?, 0x400499c720}) github.com/robertkrimen/otto.(_runtime).cmpl_evaluate_nodeWhileStatement(0x400649db00, 0x400499c6f0?) github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_statement.go:107 +0x838 github.com/robertkrimen/otto.(_runtime).cmpl_evaluate_nodeStatementList(0x400649db00?, {0x4004980150?, 0x7, 0x297?}) github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_statement.go:119 +0x90 github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate.go:17 +0x160 github.com/robertkrimen/otto.(_runtime).cmpl_evaluate_nodeProgram(0x400649db00, 0x40049801c0, 0x0) github.com/robertkrimen/otto@v0.2.1/error.go:255 +0x58 github.com/robertkrimen/otto@v0.2.1/runtime.go:838 +0x8c github.com/robertkrimen/otto@v0.2.1/runtime.go:839 +0x30 github.com/robertkrimen/otto.(_runtime).cmpl_runOrEval(0x400649db00, {0x131c660?, 0x400435aaa0?}, {0x0?, 0x0?}, 0x0) github.com/robertkrimen/otto.(_runtime).cmpl_runOrEval.func1() github.com/robertkrimen/otto.catchPanic(0x4000687e68?) github.com/robertkrimen/otto@v0.2.1/runtime.go:851 github.com/robertkrimen/otto.(_runtime).cmpl_run(...) github.com/robertkrimen/otto.Otto.Run({0x4001257020?, 0x400649db00?}, {0x131c660?, 0x400435aaa0?}) github.com/robertkrimen/otto@v0.2.1/otto.go:293 +0x44 reactor/dispatch.(Script).runScript(0x4000a83680) reactor/dispatch/DScripts.go:218 +0x1a0 reactor/dispatch/DScripts.go:276 +0x17c created by reactor/dispatch.(Script).ManageState

stevenh commented 12 months ago

Sounds like your dispatch could be reusing a VM for multiple requests? If this is case you need to lock it to prevent concurrent access.

s00500 commented 12 months ago

Uff, just found that the mutex supposed to protect the structure holding the vm is never used.... not my code... but should have checked closer first.... sorry for opening this issue...

stevenh commented 12 months ago

Glad the pointers helped