File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -16486,7 +16486,10 @@ mpack_decode_ext(
1648616486 else if (type->types & MS_TYPE_EXT) {
1648716487 data = PyBytes_FromStringAndSize(data_buf, size);
1648816488 if (data == NULL) return NULL;
16489- return Ext_New(code, data);
16489+ /* Ext_New doesn't steal the reference to data */
16490+ out = Ext_New(code, data);
16491+ Py_DECREF(data);
16492+ return out;
1649016493 }
1649116494 else if (!(type->types & MS_TYPE_ANY)) {
1649216495 return ms_validation_error("ext", type, path);
@@ -16503,7 +16506,10 @@ mpack_decode_ext(
1650316506 else if (self->ext_hook == NULL) {
1650416507 data = PyBytes_FromStringAndSize(data_buf, size);
1650516508 if (data == NULL) return NULL;
16506- return Ext_New(code, data);
16509+ /* Ext_New doesn't steal the reference to data */
16510+ out = Ext_New(code, data);
16511+ Py_DECREF(data);
16512+ return out;
1650716513 }
1650816514 else {
1650916515 pycode = PyLong_FromLong(code);
Original file line number Diff line number Diff line change @@ -1339,6 +1339,16 @@ def test_roundtrip_typed_decoder(self, size):
13391339 out = dec .decode (buf )
13401340 assert out == ext
13411341
1342+ @pytest .mark .parametrize ("type" , [None , msgspec .msgpack .Ext ])
1343+ def test_decode_ext_no_reference_leak (self , type ):
1344+ """https://github.com/msgspec/msgspec/issues/1108"""
1345+ buf = msgspec .msgpack .encode (msgspec .msgpack .Ext (5 , b"ext-leak-test-payload" ))
1346+ kwargs = {} if type is None else {"type" : type }
1347+ ext = msgspec .msgpack .decode (buf , ** kwargs )
1348+ data = ext .data
1349+ del ext
1350+ assert sys .getrefcount (data ) <= 2 # `data` + getrefcount argument
1351+
13421352 def test_typed_decoder_skips_ext_hook (self ):
13431353 def ext_hook (code , data ):
13441354 assert False , "shouldn't ever get called"
You can’t perform that action at this time.
0 commit comments