Skip to content

Commit

Permalink
Merge pull request #13 from mattbennett/fix-memory-leak
Browse files Browse the repository at this point in the history
Fix memory leak
  • Loading branch information
mattbennett authored Apr 11, 2017
2 parents 1ab4292 + 58b493c commit b4a6089
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nameko_sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ def worker_result(self, worker_ctx, result, exc_info):

self.capture_exception(worker_ctx, exc_info)

def worker_teardown(self, worker_ctx):
self.client.context.clear(deactivate=True)

def capture_exception(self, worker_ctx, exc_info):
message = self.format_message(worker_ctx, exc_info)

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"flake8==2.5.0",
"pylint==1.5.1",
"pytest==2.8.3",
"objgraph==3.1.0"
]
},
zip_safe=True,
Expand Down
51 changes: 51 additions & 0 deletions test_nameko_sentry.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import gc
import json
import logging
import socket

import eventlet
import objgraph
import pytest
from eventlet.event import Event
from mock import ANY, Mock, patch, PropertyMock
from nameko.extensions import DependencyProvider
from nameko.exceptions import RemoteError
from nameko.rpc import rpc
from nameko.standalone.rpc import ServiceRpcProxy
Expand Down Expand Up @@ -883,6 +886,54 @@ def called_twice(worker_ctx, res, exc_info):
assert expected_crumb_q2 not in breadcrumbs_map['q1']


@pytest.mark.usefixtures('patched_sentry')
class TestReleaseMemory(object):

@pytest.fixture
def log(self):
return logging.getLogger("test")

@pytest.fixture
def service_cls(self, log):

class Unsafe(DependencyProvider):

def get_dependency(self, worker_ctx):
return worker_ctx.container._worker_threads[worker_ctx]

class Service(object):
name = "service"

sentry = SentryReporter()
unsafe = Unsafe()

@rpc
def broken(self):
log.info("breadcrumb %s", self.unsafe)
raise CustomException("Error!")

return Service

def test_leak(self, container_factory, service_cls, config):
# regression test for
# https://github.com/mattbennett/nameko-sentry/issues/12

container = container_factory(service_cls, config)
container.start()

gc.collect()
count_before = objgraph.count('raven.breadcrumbs.BreadcrumbBuffer')

with entrypoint_hook(container, 'broken') as hook:
for _ in range(5):
with pytest.raises(CustomException):
hook()

gc.collect()
count_after = objgraph.count('raven.breadcrumbs.BreadcrumbBuffer')
assert count_before == count_after


class TestEndToEnd(object):

@pytest.fixture
Expand Down

0 comments on commit b4a6089

Please sign in to comment.