python: SEGFAULT when certain modules are used in a project with Alpine 3.6/Python 3.6

We’ve recently upgraded several of our containers to Python 3.6.

Some of the containers always segfault immediately after starting Django’s runserver command.

In a couple of the containers upgrading our dependencies fixed the issue, but on some of the other containers that didn’t have the issue upgrading the dependencies caused it to start happening.

In one example upgrading requests from 2.12.1 to 2.18.1 stopped the SEGFAULTs but adding the latest version of boto3 caused them to start again.

The issue only happens when run under docker.

I have a simple example repo here and would like to offer a bounty of $100 to whoever can either fix the issue or explain it in a way that allows me to fix it. ✨

Edit: we’ve tested with 3-alpine and 3-alpine3.6; same result.

Also SEGFAULTs on 3.6.2rc2-alpine3.6.

More clues: requests is also implicated in boto3 which includes botocore–which includes a vendored requests 2.7.0.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 29 (5 by maintainers)

Commits related to this issue

Most upvoted comments

awesome! Please donate it to Doctors Without Borders or a similar charity.

finally verifying that I made that donation ✨

screenshot

I have solved SEGFAULT issue by adding this code to my python script:

import threading; threading.stack_size(2*1024*1024)

It looks to me that this code does the same job as xyzz’s solution but in python. I found this solution on https://github.com/esnme/ultrajson/issues/254

@xyzz done! i assume I’ll get some confirmation once it goes through (I did it through work) and am happy to send that along when I get it 😃

also: do you happen to know where the best place to escalate this issue might be? it seems like an ‘alpine’s use of musl’ issue, correct?

screenshot

@sobolevn It works fine with alpine 3.7 so if you can, just upgrade switch to it.

@xyzz that works! where shall I send your $100? 😃

The same here. I have a segfault when running from rest_framework import routers. Link to the Dockerfile: https://github.com/wemake-services/wemake-django-template/blob/master/{{cookiecutter.project_name}}/docker/django/Dockerfile

Right now I am using --noreload option for runserver, since it only affects my development containers.

A couple pointers, don’t install python3 or python3-dev from apk if you are using the python docker image. The python in the docker image is installed from source so anything that links to or uses the apk installed python will probably have issues. But this is not the source of the segfault.

Don’t remove dependencies in a later RUN line as this won’t save any image size. Don’t apk update on its own RUN line (use && to chain it to the command using it). Don’t apk upgrade in a container; it is best to wait for the base image to update.

Following are the two Dockerfiles that I ran your compose on; both still segfault. The first is using just the python provided in the docker python image (alpine based). The second is using python3 from Alpine Linux (apk). So, my opinion is that this is not a bug in the docker python image, but I would suspect is one of the “package expects glibc but has muslcas noted on the readme from Docker Hub. Therefore, it is most likely to need a fix in python upstream or muslc. @ncopa, do you think you could help debug where the fix is needed?

FROM python:3.6-alpine3.6

# logging to the console breaks without this
ENV PYTHONUNBUFFERED 1
ENV PYTHONFAULTHANDLER 1

RUN apk add --no-cache \
        bash \
        build-base \
        gettext \
        linux-headers \
        musl-dev \
        postgresql-client \
        postgresql-dev

RUN mkdir -p /app/

WORKDIR /app/

# so we can cache the installed python modules apart from the app files
COPY *.txt /app/

RUN pip3 install --upgrade pip setuptools
RUN pip3 install -r requirements.txt -r dev-requirements.txt

COPY . /app/
FROM alpine:3.6

# logging to the console breaks without this
ENV PYTHONUNBUFFERED 1
ENV PYTHONFAULTHANDLER 1

RUN apk add --no-cache \
        bash \
        build-base \
        gettext \
        linux-headers \
        musl-dev \
        postgresql-client \
        postgresql-dev \
        python3 \
        python3-dev \
# alpine doesn't provide a `python` symlink to `python3`, so create your own
    && ln -s /usr/bin/python3 /usr/local/bin/python

RUN mkdir -p /app/

WORKDIR /app/

# so we can cache the installed python modules apart from the app files
COPY *.txt /app/

RUN pip3 install --upgrade pip setuptools
RUN pip3 install -r requirements.txt -r dev-requirements.txt

COPY . /app/

This Dockerfile, on the other hand, works fine.

# Debian based image
FROM python:3.6

RUN mkdir -p /app/

WORKDIR /app/

# so we can cache the installed python modules apart from the app files
COPY *.txt /app/

RUN pip3 install --upgrade pip setuptools
RUN pip3 install -r requirements.txt -r dev-requirements.txt

COPY . /app/