Giampaolo Rodola' <g.rod...@gmail.com> added the comment:
To further elaborate on the creation time solution, the idea in pseudo-code is the following: class Popen: def __init__(self, ...): self._execute_child(...) try: self._ctime = get_create_time(self.pid) except ProcessLookupError: self._ctime = None def send_signal(self, sig): if self._ctime is not None: if self._ctime != get_create_time(self.pid): raise ProecssLookupError("PID has been reused") os.kill(self.pid, sig) Technically there is still a race condition between _execute_child() and get_create_time(), but: 1) the time between the two calls is supposed to be extremely short 2) an OS should not reuse a PID that fast, precisely in order to minimize the chances of such a race condition to occur 3) as a general rule, on most systems PIDs are supposed to be assigned sequentially and wrap around. E.g. on Linux: ~$ for i in $(seq 20); do ps; done | grep ps 3199 pts/0 00:00:00 ps 3200 pts/0 00:00:00 ps 3201 pts/0 00:00:00 ps 3202 pts/0 00:00:00 ps 3203 pts/0 00:00:00 ps 3204 pts/0 00:00:00 ps 3205 pts/0 00:00:00 ps 3207 pts/0 00:00:00 ps 3208 pts/0 00:00:00 ps 3209 pts/0 00:00:00 ps 3210 pts/0 00:00:00 ps 3213 pts/0 00:00:00 ps 3214 pts/0 00:00:00 ps 3215 pts/0 00:00:00 ps 3216 pts/0 00:00:00 ps 3217 pts/0 00:00:00 ps 3218 pts/0 00:00:00 ps 3219 pts/0 00:00:00 ps 3221 pts/0 00:00:00 ps 3223 pts/0 00:00:00 ps As for Windows, the termination is based on the process handle instead of the PID, so I assume that means Windows is safe in this regard. There was a prior discussion about this actually: https://bugs.python.org/issue36067#msg336243 Getting process creation time is relatively easy, even though platform-dependent (and should be done in C). pidfd API would be nicer to use, but it seems it's not portable and relatively recent. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue38630> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com