basho/riak_kv — Issue #287: Race Condition causes Javascript VMs to never be returned to JS VM Pool
Author: ian-plosker
Created: 2012-02-15 17:12:49+00:00
State: open

There is a race condition in the `riak_kv_js_manager` wherein:

1. A process (e.g. a pipe fitter) calls `riak_kv_js_manager:blocking_dispatch/4`
2. `riak_kv_js_manager:blocking_dispatch` function calls `reserve_vm`, a `gen_server:call` to the `riak_kv_js_manager`, which reserves a VM
3. The calling process (e.g. a pipe fitting) is killed before calling `riak_kv_js_vm:blocking_dispatch/3` which normally would return the JS VM to the VM pool after successfully running the provided JS

In this situation, the reserved JS VM will never be returned to the JS VM pool.

The offending code is as follows:

```erlang
blocking_dispatch(Name, JSCall, MaxCount, Count) ->
case reserve_vm(Name) of %% reserve_vm is a gen_server:call to the riak_kv_js_manager
{ok, VM} ->
JobId = {VM, make_ref()},
riak_kv_js_vm:blocking_dispatch(VM, JobId, JSCall); %% reserve_vm is a gen_server:call to the riak_kv_js_vm
{error, no_vms} ->
back_off(MaxCount, Count),
blocking_dispatch(Name, JSCall, MaxCount, Count - 1)
end.
```
The simple solution would appear to be linking the calling process to the JS VM.