python-cffi


Callback from “multiprocessing” with CFFI segfaults after ~100 iterations


A PyPy callback, that works perfectly (in an infinite loop) when implemented (straightforwardly) as method of a Python object, segfaults after approximately 100 iterations when I move the Python object into a separate multiprocessing process.
In the main code I have:
import multiprocessing as mp
class Task(object):
def __init__(self, com, lib):
self.com = com # communication queue
self.lib = lib # ffi library
self.proc = mp.Process(target=self.spawn, args=(self.com,))
self.register_callback()
def spawn(self, com):
print('%s spawned.'%self.name)
# loop (keeping 'self' alive) until BREAK:
while True:
cmd = com.get()
if cmd == self.BREAK:
break
print("%s stopped."%self.name)
#ffi.calback("int(void*, Data*"): # old cffi (ABI mode)
def callback(self, data):
# <work on data>
return 1
def register_callback(self):
s = ffi.new_handle(self)
self.lib.register_callback(s, self.callback) # C-call
The idea is that multiple tasks should serve an equal number of callbacks concurrently. I have no clue what may cause the segfault, especially since it runs fine for the first ~100 iterations or so. Help much appreciated!
Solution
Handle 's' is garbage collected when returning from 'register_callback()'. Making the handle an attribute of 'self' and passing keeps it alive.
Standard CPython (cffi 1.6.0) segfaulted at the first iteration (i.e. gc was immediate) and provided me a crucial informative error message. PyPy on the other hand segfaulted after approximately 100 iterations without providing a message... Both run fine now.

Related Links

Callback from “multiprocessing” with CFFI segfaults after ~100 iterations
Multiple ffi definitions with ffi.include(…)

Categories

HOME
lambda
kendo-dropdown
long-integer
concurrency
subdomain
plaid
odata
cypher
elk-stack
phpseclib
mutation-testing
azure-graph-api
locationmanager
watch-os-3
vimeo
pm2
pushdown-automaton
pickle
infrared
raml
spring-shell
kamailio
maven-surefire-plugin
channel
router
plyr
bonita
openbr
matlab-app-designer
publishing
caesar-cipher
serversocket
love2d
cartopy
xll
reset
latexml
docker-ucp
solr-query-syntax
line-intersection
palindrome
mobile-angular-ui
sidewaffle
d3v4
always-on-top
ajaxmin
rails-engines
currency-exchange-rates
android-expansion-files
pack
cryptojs
perceptron
http.client
nastran
screen-orientation
gluon-desktop
gql
componentart
ampersand
com0com
physicsjs
kotlin-android-extensions
prism-4
webautomation
property-injection
libxml-js
itunes-sdk
xmlwriter
multiple-dispatch
websphere-6.1
system-information
mmc3
delphi-prism
motodev-studio
haiku
nosetests
page-curl
git-log
ios-4.2
aggregator
outlook-2007-addin
pinax
android-input-method
fgetc
raw-data
objectinstantiation
account-management

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App