Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed several reference cycles to prevent memory leaks. #2967

Merged
merged 5 commits into from
Mar 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,5 @@ Contributors
- Mikko Ohtamaa, 2016/12/6

- Martin Frlin, 2016/12/7

- Kirill Kuzminykh, 2017/03/01
4 changes: 3 additions & 1 deletion pyramid/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ def __init__(self, func):

@reify
def value(self):
return self.func()
result = self.func()
del self.func
return result

def resolve(self):
return self.value
Expand Down
28 changes: 28 additions & 0 deletions pyramid/tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# -*- coding: utf-8 -*-

import datetime
import gc
import locale
import os
import unittest

from pyramid.wsgi import wsgiapp
from pyramid.view import view_config
from pyramid.static import static_view
from pyramid.testing import skip_on
from pyramid.compat import (
text_,
url_quote,
Expand Down Expand Up @@ -741,3 +743,29 @@ def _assertBody(body, filename):
data = data.replace(b'\r', b'')
data = data.replace(b'\n', b'')
assert(body == data)


class MemoryLeaksTest(unittest.TestCase):

def tearDown(self):
import pyramid.config
pyramid.config.global_registries.empty()

def get_gc_count(self):
last_collected = 0
while True:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually guaranteed to converge? The possibility here for an infinite while loop concerns me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know :) May be it possible in some cases with threads that works simultaneously with this test. Then GC will collects different count of objects in each step of cycle.

collected = gc.collect()
if collected == last_collected:
break
last_collected = collected
return len(gc.get_objects())

@skip_on('pypy')
def test_memory_leaks(self):
from pyramid.config import Configurator
Configurator().make_wsgi_app() # Initialize all global objects

initial_count = self.get_gc_count()
Configurator().make_wsgi_app()
current_count = self.get_gc_count()
self.assertEqual(current_count, initial_count)
9 changes: 6 additions & 3 deletions pyramid/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,20 @@ def add(self, item):
self._order.remove(oid)
self._order.append(oid)
return
ref = weakref.ref(item, lambda x: self.remove(item))
ref = weakref.ref(item, lambda x: self._remove_by_id(oid))
self._items[oid] = ref
self._order.append(oid)

def remove(self, item):
def _remove_by_id(self, oid):
""" Remove an item from the set."""
oid = id(item)
if oid in self._items:
del self._items[oid]
self._order.remove(oid)

def remove(self, item):
""" Remove an item from the set."""
self._remove_by_id(id(item))

def empty(self):
""" Clear all objects from the set."""
self._items = {}
Expand Down