Damian Barabonkov <dami...@mit.edu> added the comment:

As per Guido's comment 
(https://github.com/python/cpython/pull/21516#issuecomment-668110711), I'm 
going to use this space to discuss ways to go forward with resource tracking 
and SharedMemory.

Taking inspiration from Vinay (https://bugs.python.org/issue37754#msg351445), I 
think the simplest and best way forward is to use a small section of the shared 
memory at the start as a reference counter. 

Every time a process latches onto a shared memory block, it does and atomic 
increment to the reference counter. And if it detaches, it does an atomic 
decrement. This atomic operations are available in C via hardware specific 
instructions. This would require modifying the Python C code posixshmem.c. It 
should not be a difficult change.

This would then change the SharedMemory API such that a call to `close()` could 
check the reference count at the end, and aromatically unlink if it reaches 0. 
Basically, the purpose of the explicit `unlink()` call is dissolved.

I think this would also play nice with the current implementation of the 
`resource_tracker`. A small change would need to take place such that it calls 
`close()` instead of `unlink()` as the clean up function. Nonetheless, it would 
keep track if all attachments of shared memory call `close()` at the end, which 
they should, and issue a warning if they do not. It would do this with the 
current code, no need to change anything.

----------
nosy: +damian.barabonkov

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue38119>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to