This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: reference counter issue in signal module
Type: Stage: resolved
Components: Versions: Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, lukasz.langa, malin, miss-islington, nanjekyejoannah, vstinner
Priority: release blocker Keywords: patch

Created on 2019-09-05 11:14 by malin, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
reproduce.py malin, 2019-09-05 11:14
Pull Requests
URL Status Linked Edit
PR 15701 closed malin, 2019-09-05 14:13
PR 15753 merged malin, 2019-09-09 10:47
PR 15779 merged miss-islington, 2019-09-09 13:48
Messages (7)
msg351196 - (view) Author: Ma Lin (malin) * Date: 2019-09-05 11:14
Adding these two lines to /Objects/longobject.c will disable the "preallocated small integer pool":

    #define NSMALLPOSINTS  0
    #define NSMALLNEGINTS  0

Then run this reproduce code (attached):

    from enum import IntEnum
    import _signal

    class Handlers(IntEnum):
        A = _signal.SIG_DFL
        B = _signal.SIG_IGN

When the interpreter exits, will get this error:

    d:\dev\cpython\PCbuild\win32>python_d.exe d:\a.py
    d:\dev\cpython\include\object.h:541: _Py_NegativeRefcount: Assertion failed: object has negative ref count
    <object: freed>
    Fatal Python error: _PyObject_AssertFailed

    Current thread 0x0000200c (most recent call first):

3.8 and 3.9 branches are affected.
I'm sorry, this issue is beyond my ability.
msg351200 - (view) Author: Ma Lin (malin) * Date: 2019-09-05 14:20
I did a Git bisect, this is the first bad commit:
https://github.com/python/cpython/commit/9541bd321a94f13dc41163a5d7a1a847816fac84

nosy involved mates.
msg351249 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-09-06 13:44
This issue is a Python 3.8 regression.

Joannah: Would you mind to have a look?

    x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
    if (PyModule_AddObject(m, "SIG_DFL", x))
        goto finally;

This change is not easy to read.

DefaultHandler must be a strong reference: finisignal() calls Py_CLEAR(DefaultHandler);

Previously, the code uses PyDict_SetItemString(d, "SIG_DFL", x): PyDict_SetItemString increases the reference counter, whereas PyModule_AddObject leaves the reference counter unchanged (yeah, it's a strange/bad C API).

I guess than an INCREF() is needed somewhere.

Compare it to:

int
PyModule_AddIntConstant(PyObject *m, const char *name, long value)
{
    PyObject *o = PyLong_FromLong(value);
    if (!o)
        return -1;
    if (PyModule_AddObject(m, name, o) == 0)
        return 0;
    Py_DECREF(o);
    return -1;
}
msg351250 - (view) Author: Joannah Nanjekye (nanjekyejoannah) * (Python committer) Date: 2019-09-06 13:48
I will look after my next meeting which is in 10 minutes. In the meantime have you perused this PR : https://github.com/python/cpython/pull/15701 If it can solve this?
msg351466 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-09-09 13:46
New changeset 77643c486fd22d8030e0d82c13012684b4ab6df1 by Victor Stinner (animalize) in branch 'master':
bpo-38037: Fix reference counters in signal module (GH-15753)
https://github.com/python/cpython/commit/77643c486fd22d8030e0d82c13012684b4ab6df1
msg351483 - (view) Author: miss-islington (miss-islington) Date: 2019-09-09 14:42
New changeset b150d0bf1bb4c3203bb3293625e32aed01b25887 by Miss Islington (bot) in branch '3.8':
bpo-38037: Fix reference counters in signal module (GH-15753)
https://github.com/python/cpython/commit/b150d0bf1bb4c3203bb3293625e32aed01b25887
msg351485 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-09-09 14:44
Thanks for the fix animalize.
History
Date User Action Args
2022-04-11 14:59:19adminsetgithub: 82218
2019-09-09 14:44:15vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg351485

stage: patch review -> resolved
2019-09-09 14:42:38miss-islingtonsetnosy: + miss-islington
messages: + msg351483
2019-09-09 13:48:18miss-islingtonsetpull_requests: + pull_request15432
2019-09-09 13:46:29vstinnersetmessages: + msg351466
2019-09-09 10:47:44malinsetpull_requests: + pull_request15407
2019-09-07 03:59:37malinsettitle: Assertion failed: object has negative ref count -> reference counter issue in signal module
2019-09-06 13:48:33nanjekyejoannahsetmessages: + msg351250
2019-09-06 13:44:39vstinnersetpriority: normal -> release blocker
nosy: + vstinner, lukasz.langa
messages: + msg351249

2019-09-05 14:20:00malinsetnosy: + berker.peksag, nanjekyejoannah
messages: + msg351200
2019-09-05 14:13:03malinsetkeywords: + patch
stage: patch review
pull_requests: + pull_request15355
2019-09-05 11:14:08malincreate