Skip to content

Fix python crash due to missing Py_None reference counting increase#3851

Merged
andypugh merged 1 commit intoLinuxCNC:masterfrom
BsAtHome:fix_py-none-crash
Mar 9, 2026
Merged

Fix python crash due to missing Py_None reference counting increase#3851
andypugh merged 1 commit intoLinuxCNC:masterfrom
BsAtHome:fix_py-none-crash

Conversation

@BsAtHome
Copy link
Contributor

@BsAtHome BsAtHome commented Mar 8, 2026

This fixes #3850 by adding reference count increase before returning Py_None.

@andypugh
Copy link
Collaborator

andypugh commented Mar 9, 2026

I can't even pretend to understand this bug, or the fix. But thanks.

@andypugh andypugh merged commit 441adf8 into LinuxCNC:master Mar 9, 2026
14 checks passed
@BsAtHome
Copy link
Contributor Author

BsAtHome commented Mar 9, 2026

I can't even pretend to understand this bug, or the fix. But thanks.

Quite simple... I introduced the bug when fixing cppcheck errors a long while ago.

Background, every object in python has a reference count. When you get an object, you increase its reference count (informing that the object is in use). When you no longer need it, you decrease the count. Once the count reaches zero, python will automatically discard the object and free the memory. This is true for all objects, including the None object (in C it is called Py_None).

Halmodule had originally return statements using Py_RETURN_NONE, which is a macro that cppcheck is very disagreeable with. These got changed into return Py_None, but that forgot that the object is referenced (in use) one more time at return time and should have had its reference count increased. When the returned None object becomes unused, somewhere else in the code, then the reference count gets decreased again. At some stage, the None object gets discarded because it has experienced more reference decreases than increases, which is, to say it mildly, a bad thing to happen. When it happens, python correctly asserts a programmer too stupid error and crashes the program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LCNC 2.10-pre1: Crash (none_dealloc) in gladevcp/hal_widgets.py with HAL_RadioButton

2 participants