Apple Mac Book Pro M1 Chip segmentation Error when using python requests library

Viewed 2182

Hardware:

  • Apple Mac Book Pro with M1 Chip
  • Mac OS BigSur 11.6

Problem:

Long story short fix!

I started developing in my python module connecting a rest api using the python requests library over OAuth1 to interact with.

I was using a docker container to develop locally.

  • Ubuntu 20.04
  • Python 3.8.x
  • openssl 1.1.1f

When I ran a requests.delete command in my code it returned always a segmentation error (SIGSEGV).

Fatal Python error: Segmentation fault

2 Answers

After a lot of debugging and searching for this issue I found that the error occurs in the ssl library. And this was the entry point how to solve that issue:

Troubleshoot:

Updating to the latest Python Version of 3.9 and using at least openssl==1.1.1g version.

Here is the Dockerfile how to update Python and openssl on Ubuntu 20.04.

FROM ubuntu:20.04
RUN apt update
# SET PYTHON 3.9 AS DEFAULT PYTHON VERSION ON UBUNTU
RUN apt install -y software-properties-common
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt -y install python3.9
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
RUN update-alternatives --config python3
WORKDIR /<your_work_dir>
COPY requirements.txt ./
RUN apt install -y pip \
    && pip install -r requirements.txt
# FROM HERE OPENSSL
RUN apt install -y wget
RUN wget https://www.openssl.org/source/openssl-1.1.1l.tar.gz
RUN tar -zxf openssl-1.1.1l.tar.gz
WORKDIR /<your_work_dir>/openssl-1.1.1l
RUN ./config
RUN make
RUN make install
RUN mv /usr/bin/openssl ~/tmp
RUN ln -s /usr/local/bin/openssl /usr/bin/openssl
RUN ldconfig
ENV LD_LIBRARY_PATH="/usr/local/lib:${LD_LIBRARY_PATH}"

Notice:

The error occurs obviously on a docker container ubuntu instance when creating it by a M1 chip. Checking the same code on a Windows created docker container (code with the wrong dependencies) on a Windows machine works.

I had the same problem and could solve it with your solution.

I was using Docker, Flask, Python and Requests on Mac M1.

sometimes when doing requests to a specific Path i got:

Fatal Python error: Segmentation fault

Current thread 0x0000ffff9c931b40 (most recent call first):
  File "/usr/local/lib/python3.6/ssl.py", line 642 in write
  File "/usr/local/lib/python3.6/ssl.py", line 944 in send
  File "/usr/local/lib/python3.6/ssl.py", line 975 in sendall
  File "/usr/local/lib/python3.6/http/client.py", line 1006 in send
  File "/usr/local/lib/python3.6/http/client.py", line 1046 in _send_output
  File "/usr/local/lib/python3.6/http/client.py", line 1286 in endheaders
  File "/usr/local/lib/python3.6/http/client.py", line 1337 in _send_request
  File "/usr/local/lib/python3.6/http/client.py", line 1291 in request
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 387 in _make_request
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 672 in urlopen
  File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 449 in send
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 646 in send
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 533 in request
  File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 60 in request
  File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 116 in post
  File "<stdin>", line 1 in <module>
Segmentation fault

To Debug Segmentation fault use:

https://docs.python.org/3/library/faulthandler.html

https://blog.richard.do/2018/03/18/how-to-debug-segmentation-fault-in-python/

Related