Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix setting monitors on calling processes
When process was in the waiting list it was monitored twice and only one monitor
was canceled.
  • Loading branch information
ppikula committed Oct 14, 2014
1 parent 8222fef commit 4439504
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/poolboy.erl
Expand Up @@ -279,8 +279,7 @@ handle_checkin(Pid, State) ->
monitors = Monitors,
overflow = Overflow} = State,
case queue:out(Waiting) of
{{value, {{FromPid, _} = From, _}}, Left} ->
Ref = erlang:monitor(process, FromPid),
{{value, {From, Ref}}, Left} ->
true = ets:insert(Monitors, {Pid, Ref}),
gen_server:reply(From, Pid),
State#state{waiting = Left};
Expand Down
25 changes: 25 additions & 0 deletions test/poolboy_tests.erl
Expand Up @@ -50,6 +50,9 @@ pool_test_() ->
},
{<<"Pool returns status">>,
fun pool_returns_status/0
},
{<<"Server demonitors previously wating processes">>,
fun demonitors_previously_waiting_processes/0
}
]
}.
Expand Down Expand Up @@ -392,6 +395,28 @@ pool_returns_status() ->
?assertEqual({full, 0, 0, 0}, poolboy:status(Pool4)),
ok = pool_call(Pool4, stop).

demonitors_previously_waiting_processes() ->
{ok, Pool} = new_pool(1,0),
SelfPid = self(),
spawn(fun() ->
P = poolboy:checkout(Pool),
SelfPid ! ok,
timer:sleep(2000),
poolboy:checkin(Pool, P),
timer:sleep(400)
end),
% wait for checkout in the process above
receive ok -> ok end,
Pid = poolboy:checkout(Pool),
?assertEqual(1, length(get_monitors(Pool, self()))),
poolboy:checkin(Pool, Pid),
timer:sleep(400),
?assertEqual(0, length(get_monitors(Pool, self()))).

get_monitors(Pid, MonitoredProcess) ->
[{monitors, Monitors}] = erlang:process_info(Pid, [monitors]),
lists:filter(fun({process, P}) -> P == MonitoredProcess end, Monitors).

new_pool(Size, MaxOverflow) ->
poolboy:start_link([{name, {local, poolboy_test}},
{worker_module, poolboy_test_worker},
Expand Down

0 comments on commit 4439504

Please sign in to comment.