Skip to content

Commit

Permalink
Merge pull request #27 from EMQ-YangM/master
Browse files Browse the repository at this point in the history
fix(mc_topology): fix potential process leaks
  • Loading branch information
sdzx-1 authored Apr 1, 2022
2 parents 3a388dd + ef9e891 commit e017c4e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
19 changes: 17 additions & 2 deletions src/mongoc/mc_topology.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
-include("mongoc.hrl").

%% API
-export([start_link/3, drop_server/2, update_topology/1, get_state/1, get_pool/2, get_pool/4, get_pool/1, disconnect/1, get_state_part/1]).
-export([start_link/3, drop_server/2, update_topology/1, get_state/1, get_pool/2, get_pool/4, get_pool/5, get_pool/1, disconnect/1, get_state_part/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
Expand Down Expand Up @@ -115,7 +115,8 @@ get_pool(Topology, Options) ->
get_pool(RPMode, RPTags, State) ->
TO = State#topology_state.topology_opts,
ServerSelectionTimeoutMS = mc_utils:get_value(serverSelectionTimeoutMS, TO, 30000),
Pid = spawn(?MODULE, get_pool, [self(), State, RPMode, RPTags]),
Caller = self(),
Pid = spawn(?MODULE, get_pool, [self(), State, RPMode, RPTags, Caller]),
receive
{Pid, {error, Reason}, _} ->
{error, Reason};
Expand All @@ -138,6 +139,20 @@ get_pool(From, #topology_state{self = Topology, get_pool_timeout = TM} = State,
get_pool(From, State, RPMode, Tags)
end.

get_pool(From, #topology_state{self = Topology, get_pool_timeout = TM} = State, RPMode, Tags, Caller) ->
case is_process_alive(Caller) of
false -> ok;
true ->
case mc_selecting_logics:select_server(Topology, RPMode, Tags) of
#mc_server{pid = Pid, type = Type} ->
Pool = mc_server:get_pool(Pid, TM),
From ! {self(), Pool, Type};
undefined ->
timer:sleep(100),
get_pool(From, State, RPMode, Tags, Caller)
end
end.


%%%===================================================================
%%% Handlers
Expand Down
2 changes: 1 addition & 1 deletion src/mongodb.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%% ex: ts=4 sw=4 noexpandtab syntax=erlang
{application, mongodb, [
{description, "Client interface to MongoDB, also known as the driver. See www.mongodb.org"},
{vsn, "3.0.12"},
{vsn, "3.0.13"},
{registered, []},
{applications, [kernel, stdlib, bson, crypto, poolboy, pbkdf2]},
{mod, {mongo_app, []}}
Expand Down
9 changes: 7 additions & 2 deletions src/mongodb.appup.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
%% -*- mode: erlang -*-
{"3.0.12",
[
{"3.0.13",
[{"3.0.12", [
{load_module, mc_topology, brutal_purge, soft_purge, []}
]},
{"3.0.11", [
{add_module, mc_util},
{load_module, mc_monitor, brutal_purge, soft_purge, []},
Expand Down Expand Up @@ -37,6 +39,9 @@
{<<".*">>, []}
],
[
{"3.0.12", [
{load_module, mc_topology, brutal_purge, soft_purge, []}
]},
{"3.0.11", [
{delete_module, mc_util},
{load_module, mc_monitor, brutal_purge, soft_purge, []},
Expand Down

0 comments on commit e017c4e

Please sign in to comment.