classification
Title: super.__init__ leaks memory if called multiple times
Type: resource usage Stage: resolved
Components: Interpreter Core Versions: Python 3.6, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: Kevin Modzelewski, brett.cannon, gvanrossum, python-dev, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2016-04-09 01:02 by Kevin Modzelewski, last changed 2016-04-13 12:38 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
super_init_leaks.patch serhiy.storchaka, 2016-04-12 15:43 review
super_init_leaks_2.patch serhiy.storchaka, 2016-04-12 18:47 review
Messages (12)
msg263053 - (view) Author: Kevin Modzelewski (Kevin Modzelewski) Date: 2016-04-09 01:02
The super() __init__ function fills in the fields of a super object without checking if they were already set.  If someone happens to call __init__ again, the previously-set references will end up getting forgotten and leak memory.

For example:

import sys
print(sys.gettotalrefcount())
sp = super(int, 1)
for i in range(100000):
  super.__init__(sp, float, 1.0)
print(sys.gettotalrefcount())
msg263073 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-04-09 07:05
If super used __new__ instead of __init__, this issue probably wouldn't arise.

I'm surprised that super is subclassable.
msg263103 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-04-09 16:28
Any uses of super() beyond the documented idioms are uninteresting, except they should not be usable as crash or DoS vectors.
msg263195 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-04-11 18:01
Based on Guido's feedback and the fact that this isn't documented usage of super() and hence no promises to not re-initialize, I'm closing as "not a bug". Sorry, Kevin.
msg263196 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-04-11 18:05
Actually, the refcount bug is still a bug.
msg263232 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-04-12 06:31
Possible solutions:

1. Correctly decref old values.
2. Raise an exception if super.__init__ is caled multiple times.
3. Remove super.__init__ and add super.__new__.

What is more preferable?
msg263249 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-04-12 15:03
Do #1.

--Guido (mobile)
On Apr 11, 2016 11:31 PM, "Serhiy Storchaka" <report@bugs.python.org> wrote:

>
> Serhiy Storchaka added the comment:
>
> Possible solutions:
>
> 1. Correctly decref old values.
> 2. Raise an exception if super.__init__ is caled multiple times.
> 3. Remove super.__init__ and add super.__new__.
>
> What is more preferable?
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue26718>
> _______________________________________
>
msg263253 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-04-12 15:43
Here is a patch.
msg263254 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-04-12 15:45
super_init_leaks.patch LGTM, it fixes. I confirm that the patch fixes the refleak. I checked with:

$ ./python -m test -R 3:3 test_super
msg263270 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-04-12 18:47
Added more comments as suggested by Guido.
msg263276 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-04-12 20:42
super_init_leaks_2.patch LGTM.
msg263331 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-04-13 12:30
New changeset 450f36750cb9 by Serhiy Storchaka in branch '3.5':
Issue #26718: super.__init__ no longer leaks memory if called multiple times.
https://hg.python.org/cpython/rev/450f36750cb9

New changeset 4680438f486f by Serhiy Storchaka in branch '2.7':
Issue #26718: super.__init__ no longer leaks memory if called multiple times.
https://hg.python.org/cpython/rev/4680438f486f

New changeset 55f4c1f8ca6a by Serhiy Storchaka in branch 'default':
Issue #26718: super.__init__ no longer leaks memory if called multiple times.
https://hg.python.org/cpython/rev/55f4c1f8ca6a
History
Date User Action Args
2016-04-13 12:38:38serhiy.storchakasetstatus: open -> closed
assignee: serhiy.storchaka
resolution: fixed
stage: patch review -> resolved
2016-04-13 12:30:01python-devsetnosy: + python-dev
messages: + msg263331
2016-04-12 20:42:07vstinnersetmessages: + msg263276
2016-04-12 18:47:29serhiy.storchakasetfiles: + super_init_leaks_2.patch

messages: + msg263270
2016-04-12 15:45:28vstinnersetmessages: + msg263254
2016-04-12 15:43:40serhiy.storchakasetfiles: + super_init_leaks.patch
messages: + msg263253

keywords: + patch
type: behavior -> resource usage
stage: patch review
2016-04-12 15:29:03vstinnersetnosy: + vstinner
2016-04-12 15:03:18gvanrossumsetmessages: + msg263249
2016-04-12 06:31:28serhiy.storchakasetmessages: + msg263232
2016-04-11 18:05:41gvanrossumsetstatus: closed -> open
resolution: not a bug -> (no value)
messages: + msg263196
2016-04-11 18:01:29brett.cannonsetstatus: open -> closed
resolution: not a bug
messages: + msg263195
2016-04-09 16:28:52gvanrossumsetmessages: + msg263103
2016-04-09 07:05:47serhiy.storchakasetnosy: + gvanrossum, serhiy.storchaka
messages: + msg263073
2016-04-09 03:31:37SilentGhostsetnosy: + brett.cannon

type: behavior
versions: + Python 3.5
2016-04-09 01:02:03Kevin Modzelewskicreate