Detect pid reuse

Dinislam Salikhov Dinislam.Salikhov@REDACTED
Mon Jul 6 14:27:32 CEST 2020

Yes, when an old connection terminates, its pid is reused for newly spawned process. I stumbled upon that the new process (with the same old pid) is the connection to another database.
From: erlang-questions <erlang-questions-bounces@REDACTED> on behalf of Yao Bao <by@REDACTED>
Sent: Monday, July 6, 2020 3:22 PM
To: erlang-questions@REDACTED
Subject: Re: Detect pid reuse


I am considering will the newly spawned process reuse the pid?


> 在 2020年7月6日,20:09,Dinislam Salikhov <Dinislam.Salikhov@REDACTED> 写道:
> Hello,
> I have a bunch of connections (processes) to different instances of the same database (instances are physically separated and don't share data). The connect may be unstable, so the connections are handled with a supervisor: if the connect is lost, the process terminates and supervisor starts a new one.
> 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 between the search and actual sending command (i.e. via gen_server:call(Pid,...)), the process may terminate and its pid will be reused by another connection (to different database). So I end up with sending a command to the wrong database (as gen_server:call/3 thinks that the pid is the correct one).
> Is there a way to detect that such restart has occurred?
> I have an idea to keep some kind of token for the connection, so a process sending a command should pass the token and the connection process must check that the token is right. The code for connection process is 3rd party, so I would like to find some other solution.
> Dinislam Salikhov

More information about the erlang-questions mailing list