anchor.diff:      TeachFun = fun(Proc0, Acc) ->
           try
               {ok, Proc1} = teach_ddoc(DDoc, DDocKey, Proc0),
  -            {stop, assign_proc(State#state.tab, Client, Proc1)}
  +            {stop, assign_proc(State#state.tab, ClientPid, Proc1)}
           catch _:_ ->
               {ok, Acc}
           end
       end,
  -    try iter_procs(State#state.tab, Lang, IterFun, nil) of
  -    {not_found, _} ->
  -        case iter_procs(State#state.tab, Lang, TeachFun, nil) of
  -        {not_found, _} ->
  -            spawn_link(?MODULE, new_proc, [From, Lang, DDoc, DDocKey]),
  -            {noreply, State};
  -        {ok, Proc} ->
  -            {reply, {ok, Proc, State#state.config}, State}
  -        end;
  -    {ok, Proc} ->
  -        {reply, {ok, Proc, State#state.config}, State}
  -    catch error:Reason ->
  -        ?LOG_ERROR("~p ~p ~p", [?MODULE, Reason, erlang:get_stacktrace()]),
  -        {reply, {error, Reason}, State}
  -    end;
  -
  -handle_call({get_proc, Lang}, {Client, _} = From, State) ->
  +    Client = #client{from=From, lang=Lang, ddoc=DDoc, ddoc_key=DDocKey},
  +    find_proc(State, Client, [IterFun, TeachFun]);
  +
  +handle_call({get_proc, Lang}, From, State) ->
  +    {ClientPid, _} = From,
       IterFun = fun(Proc, _Acc) ->
  -        {stop, assign_proc(State#state.tab, Client, Proc)}
  +        {stop, assign_proc(State#state.tab, ClientPid, Proc)}
       end,
  -    try iter_procs(State#state.tab, Lang, IterFun, nil) of
  -    {not_found, _} ->
  -        spawn_link(?MODULE, new_proc, [From, Lang]),
  -        {noreply, State};
  -    {ok, Proc} ->
  -        {reply, {ok, Proc, State#state.config}, State}
  -    catch error:Reason ->
  -        ?LOG_ERROR("~p ~p ~p", [?MODULE, Reason, erlang:get_stacktrace()]),
  -        {reply, {error, Reason}, State}
  -    end;
  -
  -handle_call({ret_proc, #proc{client=Ref} = Proc}, _From, State) ->
  +    Client = #client{from=From, lang=couch_util:to_binary(Lang)},
  +    find_proc(State, Client, [IterFun]);
  +
  +handle_call({ret_proc, #proc{client=Ref, lang=Lang0} = Proc}, _From, State) ->
       erlang:demonitor(Ref, [flush]),
  +    Lang = couch_util:to_binary(Lang0),
       % We need to check if the process is alive here, as the client could be
       % handing us a #proc{} with a dead one.  We would have already removed the
       % #proc{} from our own table, so the alternative is to do a lookup in the
       % table before the insert.  Don't know which approach is cheaper.
  -    return_proc(State#state.tab, Proc),
  -    {reply, true, State};
  +    {reply, true, return_proc(State, Proc#proc{lang=Lang})};
   
   handle_call(_Call, _From, State) ->
       {reply, ignored, State}.
   
  -handle_cast({os_proc_idle, Pid}, #state{tab=Tab}=State) ->
  -    Limit = config:get("query_server_config", "os_process_soft_limit", "100"),
  -    case ets:lookup(Tab, Pid) of
  -        [#proc{client=nil}] ->
  -            case ets:info(Tab, size) > list_to_integer(Limit) of
  -                true ->
  +handle_cast({os_proc_idle, Pid}, #state{tab=Tab, proc_counts=Counts}=State0) ->
  +    Limit = list_to_integer(
  +            config:get("query_server_config", "os_process_soft_limit", "100")),
  +    State = case ets:lookup(Tab, Pid) of
  +        [#proc{client=nil, lang=Lang}] ->
  +            case dict:find(Lang, Counts) of
  +                {ok, Count} when Count > Limit ->
                       ?LOG_INFO("Closing idle OS Process: ~p", [Pid]),
                       ets:delete(Tab, Pid),
                       case is_process_alive(Pid) of
  
positive.code: handle_config_change(_, _, _, _, _) ->
      {ok, nil}.
  
  iter_procs(Tab, Lang, Fun, Acc) when is_binary(Lang) ->
      iter_procs(Tab, binary_to_list(Lang), Fun, Acc);
  iter_procs(Tab, Lang, Fun, Acc) ->
      Pattern = #proc{lang=Lang, client=nil, _='_'},
      MSpec = [{Pattern, [], ['$_']}],
  
negative.code:                             gen_server:cast(Pid, stop);
                          _ ->
                              ok
                      end,
                      State0#state{
                          proc_counts=dict:update_counter(Lang, -1, Counts)
                      };
                  {ok, _} ->
                      State0
              end;
          _ ->
              State0
      end,
      {noreply, State};
  handle_cast(reload_config, State) ->
  