Skip to content

Commit

Permalink
Fix PyUnknownFields memory leak
Browse files Browse the repository at this point in the history
Properly release internal data structure on deallocation.
Fix #7301
  • Loading branch information
agrenott committed Sep 29, 2020
1 parent 3d8943f commit 8800e19
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
17 changes: 17 additions & 0 deletions python/google/protobuf/internal/unknown_fields_test.py
Expand Up @@ -39,6 +39,7 @@
import unittest2 as unittest #PY26
except ImportError:
import unittest
import tracemalloc
from google.protobuf import map_unittest_pb2
from google.protobuf import unittest_mset_pb2
from google.protobuf import unittest_pb2
Expand Down Expand Up @@ -312,6 +313,22 @@ def testClear(self):
self.assertIn('UnknownFields does not exist.',
str(context.exception))

def testUnknownFieldsNoMemoryLeak(self):
# Call to UnknownFields must not leak memory
nb_leaks = 1234
def leaking_function():
for _ in range(nb_leaks):
self.empty_message.UnknownFields()
tracemalloc.start()
snapshot1 = tracemalloc.take_snapshot()
leaking_function()
snapshot2 = tracemalloc.take_snapshot()
top_stats = snapshot2.compare_to(snapshot1, 'lineno')
tracemalloc.stop()
# There's no easy way to look for a precise leak source.
# Rely on a "marker" count value while checking allocated memory.
self.assertEqual([], [x for x in top_stats if x.count_diff == nb_leaks])

def testSubUnknownFields(self):
message = unittest_pb2.TestAllTypes()
message.optionalgroup.a = 123
Expand Down
1 change: 1 addition & 0 deletions python/google/protobuf/pyext/unknown_fields.cc
Expand Up @@ -142,6 +142,7 @@ static void Dealloc(PyObject* pself) {
}
Py_CLEAR(self->parent);
self->~PyUnknownFields();
Py_TYPE(pself)->tp_free(pself);
}

static PySequenceMethods SqMethods = {
Expand Down

0 comments on commit 8800e19

Please sign in to comment.