Detect pid reuse

Dinislam Salikhov Dinislam.Salikhov@REDACTED
Thu Jul 9 11:03:49 CEST 2020


Unfortunately, registering a process with a name doesn't help much. It reduces a time window where the race may occur though.
For instance, when gen_server:call/3 is invoked, the library code calls whereis(Name) to get the pid and then sends it a message {'$gen_call,...}. So between erlang:whereis/1 and erlang:send/2, the pid may be reused (actually, it is between erlang:whereis/1 and erlang:monitor/2 followed by erlang:send/2, so we will monitor the wrong process).
See lib/stdlib/src/gen.erl which is used by lib/stdlib/src/gen_server.erl

> If you have multiple connections to any given db (a pool of pools, if
you will), using a process group module like pg makes this easy.

Never used it before. I'll have a look. Thanks for the reference.

Dinislam Salikhov
________________________________________
From: Aaron Seigo <aseigo@REDACTED>
Sent: Thursday, July 9, 2020 10:26 AM
To: Dinislam Salikhov
Cc: erlang-questions@REDACTED
Subject: Re: Detect pid reuse

On 2020-07-06 14:09, Dinislam Salikhov wrote:
> If I want to send a command to the database, I search for the pid of
> the corresponding connection (in supervisor's children list). And

Perhaps register the processes with a name so that instead of searching
for a literal pid, which may indeed change and requires more bookkeeping
in your application code, you lookup the relevant connect by a name in a
process registry. Should the old connection go away, the new one takes
over the same name.

If you have multiple connections to any given db (a pool of pools, if
you will), using a process group module like pg makes this easy.

Even then, you'll obviously need to handle the failure case of the
process exiting between the message being sent and the response being
received, but at least the lookup will be consistent.

--
Aaron Seigo


More information about the erlang-questions mailing list