[issue39492] reference cycle affecting Pickler instances (Python3.8+)

2020-01-29 Thread Pierre Glaser


New submission from Pierre Glaser :

The new Pickler reducer_override mechanism introduced in `Python3.8` generates 
a reference cycle: for optimization purposes, a the pickler.reducer_override 
bound method is referenced into the reducer_override attribute of the Pickler's 
struct. Thus, until as a gc.collect call is performed, both the Pickler and all 
the elements it pickled (as they are part of its memo), wont be collected.

We should break this cycle a the end of the dump() method.

See reproducer below:
```
import threading
import weakref
import pickle
import io


class MyClass:
pass


my_object = MyClass()
collect = threading.Event()
_ = weakref.ref(my_object, lambda obj: collect.set())  # noqa


class MyPickler(pickle.Pickler):
def reducer_override(self, obj):
return NotImplemented


my_pickler = MyPickler(io.BytesIO())
my_pickler.dump(my_object)
del my_object
del my_pickler

# import gc
# gc.collect()

for i in range(5):
collected = collect.wait(timeout=0.1)
if collected:
print('my_object was successfully collected')
break
```

--
components: Library (Lib)
messages: 360995
nosy: pierreglaser, pitrou
priority: normal
severity: normal
status: open
title: reference cycle affecting Pickler instances (Python3.8+)
type: resource usage
versions: Python 3.8, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39492] reference cycle affecting Pickler instances (Python3.8+)

2020-01-29 Thread Pierre Glaser


Change by Pierre Glaser :


--
keywords: +patch
pull_requests: +17643
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/18266

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39492] reference cycle affecting Pickler instances (Python3.8+)

2020-02-02 Thread miss-islington


miss-islington  added the comment:


New changeset 0f2f35e15f9fbee44ce042b724348419d8136bc5 by Pierre Glaser in 
branch 'master':
bpo-39492: Fix a reference cycle between reducer_override and a Pickler 
instance (GH-18266)
https://github.com/python/cpython/commit/0f2f35e15f9fbee44ce042b724348419d8136bc5


--
nosy: +miss-islington

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39492] reference cycle affecting Pickler instances (Python3.8+)

2020-02-02 Thread Antoine Pitrou


Change by Antoine Pitrou :


--
pull_requests: +17692
pull_request: https://github.com/python/cpython/pull/18316

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39492] reference cycle affecting Pickler instances (Python3.8+)

2020-02-02 Thread Antoine Pitrou


Antoine Pitrou  added the comment:


New changeset 17236873392533ce0c5d7bbf52cbd61bca171c59 by Antoine Pitrou in 
branch '3.8':
[3.8] bpo-39492: Fix a reference cycle between reducer_override and a Pickler 
instance (GH-18266) (#18316)
https://github.com/python/cpython/commit/17236873392533ce0c5d7bbf52cbd61bca171c59


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39492] reference cycle affecting Pickler instances (Python3.8+)

2020-02-02 Thread Antoine Pitrou


Antoine Pitrou  added the comment:

Thank you for the PR!

--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com