MemoryError: Cannot allocate write+execute memory for ffi.callback() requests報錯


本機環境:Mac OS 11.2 python3.8

在requests的時候存在問題。
r = requests.get(url, headers=headers, timeout =10)
報的錯誤是這樣的:
MemoryError: Cannot allocate write+execute memory for ffi.callback(). You might be running on a system that prevents this. For more information, see https://cffi.readthedocs.io/en/latest/using.html#callbacks
並且伴隨着一堆ssl認證失敗。

問題診斷:和ssl相關的某個庫沒有寫和執行權限,經過資料查詢初步確定為pyopenssl,openssl是有寫和執行權限的,而pyopenssl則沒有。我是因為下載了Scrapy,多了個這玩意。
解決辦法:
看看自己有沒有pyopenssl這個庫,如果有則刪除之:
執行:

pip3 uninstall pyopenssl

參考:

pyOpenSSL is not compatible with systems that prevent writeable and executable memory pages. You either have to disable the security feature, use another TLS library like the builtin ssl module, or redesign+rewrite pyOpenSSL's callback system.

it's a problem in pyOpenSSL. OpenSSL is fine.
I see how my comment can be understand the wrong way. I was referring to another Python TLS library such as ssl module from Python's standard library. OpenSSL does not require executable+writable memory. The problem only effects pyOpenSSL.
It's an implementation artifact of pyOpenSSL's glue code that wraps OpenSSL's C-API and makes it available for Python. pyOpenSSL uses CFFI, which is a Python interface to libffi. libffi (library for foreign function interface) uses dynamic code creation for dynamic callbacks. Every time the code passes a Python method to OpenSSL, it has to wrap the Python method into machine code, so it looks like a C function to the C-API of OpenSSL. It writes dynamic code to an executable memory page. CFFI calls this old style callbacks.
There are ways to work around the problem, but it's complicated and lots of work. Nobody has contributed a solution yet.

具體可參考:https://github.com/pyca/pyopenssl/issues/873


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM