[issue36880] Returning None from a callback with restype py_object decrements None's refcount too much

2021-03-12 Thread Eryk Sun
Change by Eryk Sun : -- versions: +Python 3.10, Python 3.9 -Python 3.5, Python 3.6, Python 3.7 ___ Python tracker ___ ___

[issue36880] Returning None from a callback with restype py_object decrements None's refcount too much

2019-05-16 Thread dgelessus
Change by dgelessus : -- keywords: +patch pull_requests: +13273 stage: -> patch review ___ Python tracker ___ ___ Python-bugs-list

[issue36880] Returning None from a callback with restype py_object decrements None's refcount too much

2019-05-13 Thread anthony shaw
anthony shaw added the comment: If you can write a test similar to the AnotherLeak.test_callback test case, and commit that first. It will show in the CI/CD log as failed and verify the issue (incase it comes up in PR review) Then add another commit with the patch itself and we should see

[issue36880] Returning None from a callback with restype py_object decrements None's refcount too much

2019-05-13 Thread dgelessus
dgelessus added the comment: Thank you for looking into this! I can confirm that Eryk Sun's change fixes the issue for me locally. I'm up for making the patch for this. Regarding tests, I see there are already some refcount-related ctypes tests in Lib/ctypes/test/test_refcounts.py - should

[issue36880] Returning None from a callback with restype py_object decrements None's refcount too much

2019-05-11 Thread anthony shaw
anthony shaw added the comment: Thanks Eryk that saved a lot of debugging. dgelessus - if you want to write a patch for CPython am happy to take you through this and get it over the line. Else: am Happy to write a test against the gc counter and a patch for this --

[issue36880] Returning None from a callback with restype py_object decrements None's refcount too much

2019-05-11 Thread Russell Keith-Magee
Change by Russell Keith-Magee : -- nosy: +freakboy3742 ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe:

[issue36880] Returning None from a callback with restype py_object decrements None's refcount too much

2019-05-11 Thread Eryk Sun
Eryk Sun added the comment: This is due to an oversight in _CallPythonObject in Modules/_ctypes/callbacks.c. The setfunc protocol is to return None if there's no object to keep alive. This isn't applicable to py_object (i.e. O_set in Modules/_ctypes/cfield.c). So the problem is the

[issue36880] Returning None from a callback with restype py_object decrements None's refcount too much

2019-05-11 Thread anthony shaw
anthony shaw added the comment: In the stack it's calling none_dealloc() which should never happen. Assume this is being triggered by ctypes PyCFuncPtr_call. The stacktrace I'm getting comes after the double decref so it's not showing the root cause. Someone who knows ctypes better might be

[issue36880] Returning None from a callback with restype py_object decrements None's refcount too much

2019-05-11 Thread anthony shaw
anthony shaw added the comment: Full trace for reference: (lldb) bt all * thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT * frame #0: 0x7fff5984f2c6 libsystem_kernel.dylib`__pthread_kill + 10 frame #1: 0x7fff59904bf1 libsystem_pthread.dylib`pthread_kill

[issue36880] Returning None from a callback with restype py_object decrements None's refcount too much

2019-05-11 Thread anthony shaw
anthony shaw added the comment: >From lldb (lldb) run ~/cpython/test_gc_ctypes.py Process 20059 launched: '/Users/anthonyshaw/CLionProjects/cpython/python.exe' (x86_64) Fatal Python error: deallocating None Current thread 0x0001005c85c0 (most recent call first): File

[issue36880] Returning None from a callback with restype py_object decrements None's refcount too much

2019-05-11 Thread anthony shaw
anthony shaw added the comment: The documentation says: >> Note Make sure you keep references to CFUNCTYPE() objects as long as they are used from C code. ctypes doesn’t, and if you don’t, they may be garbage collected, crashing your program when a callback is made. Also, note that if the

[issue36880] Returning None from a callback with restype py_object decrements None's refcount too much

2019-05-10 Thread anthony shaw
anthony shaw added the comment: Thanks, I'll check this out -- assignee: -> anthonypjshaw nosy: +anthonypjshaw ___ Python tracker ___

[issue36880] Returning None from a callback with restype py_object decrements None's refcount too much

2019-05-10 Thread dgelessus
New submission from dgelessus : This occurs when writing a ctypes callback in Python whose restype is ctypes.py_object. If the callback returns None, the refcount of None is decremented once too often. This happens every time the callback is called, and if done often enough, Python attempts