Skip to content

Commit 50c2491

Browse files
gh-159399: Fix crash in codecs.encode() on a concurrently unregistered codec (#159400)
* gh-159399: Fix crash in codecs.encode() on a concurrently unregistered codec In the free-threaded build, codec_getitem() released the CodecInfo tuple returned by _PyCodec_Lookup() before taking a reference to the encoder or decoder it returns. If another thread cleared the lookup cache in between with codecs.unregister(), the tuple could hold the last reference to the item, which was then freed before it was increfed. Take the reference before releasing the tuple, as codec_getitem_checked() already does. * Remove the free-threading stress test
1 parent 16c0adc commit 50c2491

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash in :func:`codecs.encode` and :func:`codecs.decode` in the
2+
:term:`free-threaded build` when another thread concurrently unregisters a
3+
codec search function. Patch by Christian Aurich Zanettini Martins.

‎Python/codecs.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ PyObject *codec_getitem(const char *encoding, int index)
272272
codecs = _PyCodec_Lookup(encoding);
273273
if (codecs == NULL)
274274
return NULL;
275-
v = PyTuple_GET_ITEM(codecs, index);
275+
v = Py_NewRef(PyTuple_GET_ITEM(codecs, index));
276276
Py_DECREF(codecs);
277-
return Py_NewRef(v);
277+
return v;
278278
}
279279

280280
/* Helper functions to create an incremental codec. */

0 commit comments

Comments
 (0)