Skip to content

[heads up] segmentation fault with Python 3.15 when running test_badcompresscopy -> when zlib_Compress_copy initialization fails #75

@befeleme

Description

@befeleme

I am testing Python libraries in Fedora Linux 45 with Python 3.15 and have encountered a segmentation fault with python-zlib-ng.

This issue comes when running test_badcompresscopy, and it's related to how zlib_Compress_copy works.
In case initialization fails, goto error is executed:

if (!self->is_initialised) {
PyErr_SetString(PyExc_ValueError, "Cannot copy flushed objects.");
goto error;

That, as a first thing, calls LEAVE_ZLIB, which substitutes PyThread_release_lock((obj)->lock);

error:
LEAVE_ZLIB(self);
Py_XDECREF(return_value);
return NULL;

This, however, cannot be successful, because the thread wasn't acquired in the first place: ENTER_ZLIB is called AFTER the initialization check, on line 810:

ENTER_ZLIB(self);

This happens with Python 3.15 and not before, because the implementation of PyThread_release_lock has changed to use PyMutex objects, which apparently behaves differently, aborting entirely: python/cpython#134745

To fix this, the goto error part could be replaced with just the decref and return, without the LEAVE_ZLIB part.

-        goto error;                                                                                       
+        Py_DECREF(return_value);                                                                          
+        return NULL;  

I have verified the fix works in my testing environment (the tests passes, package relying on this one also builds). I can submit it if you deem it correct for the issue.

Minimal reproducer:

import copy
from zlib_ng import zlib_ng

c = zlib_ng.compressobj()
c.compress(b"x")
c.flush()
copy.copy(c)

When run with Python 3.14 it raises ValueError: Cannot copy flushed objects., with Python 3.15 it's a segmentation fault.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions