Bug #89889 Program received signal SIGSEGV, Segmentation fault
Submitted: 2 Mar 2018 13:25 Modified: 28 Mar 2018 3:32
Reporter: weikang peng Email Updates:
Status: Can't repeat Impact on me:
None 
Category:Connector / Python Severity:S2 (Serious)
Version:8.0.6 OS:Ubuntu (Ubuntu 16.04.2 LTS)
Assigned to: CPU Architecture:x86

[2 Mar 2018 13:25] weikang peng
Description:
When I try to import random and mysql.connector, I got a  Segmentation fault.

How to repeat:
install mysql-connector-python:

pip install mysql-connector-python==8.0.5

root@localhost:~# python 
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> import mysql.connector
Segmentation fault

Suggested fix:
I don't have enough knowledge to address this bug, sorry.
[2 Mar 2018 13:32] weikang peng
Sorry, I wrote the wrong version number of mysql-connector-python. It shoud be 

pip install mysql-connector-python==8.0.6
[20 Mar 2018 10:22] Chiranjeevi Battula
Hello weikang peng,

Thank you for the bug report.
I could not repeat the issue at my end using Connector / Python 8.0.6 with MySQL 5.7.21 and  not seeing any issues.
If you can provide more information, feel free to add it to this bug and change the status back to 'Open'.

Thank you for your interest in MySQL.

Thanks,
Chiranjeevi.
[20 Mar 2018 10:22] Chiranjeevi Battula
root@XenialXerus:/home/cbattula# dpkg -l|grep mysql
ii  mysql-apt-config                           0.8.9-1                                      all          Auto configuration for MySQL APT Repo.
ii  mysql-client                               5.7.21-1ubuntu16.04                          amd64        MySQL Client meta package depending on latest version
ii  mysql-common                               5.7.21-1ubuntu16.04                          amd64        MySQL Common
ii  mysql-community-client                     5.7.21-1ubuntu16.04                          amd64        MySQL Client
ii  mysql-community-server                     5.7.21-1ubuntu16.04                          amd64        MySQL Server
ii  mysql-server                               5.7.21-1ubuntu16.04                          amd64        MySQL Server meta package depending on latest version
ii  mysql-shell:amd64                          8.0.4-rc-1ubuntu16.04                        amd64        MySQL Shell (part of MySQL Server) 8.0
root@XenialXerus:/home/cbattula# 
root@XenialXerus:/home/cbattula# cd Downloads/
root@XenialXerus:/home/cbattula/Downloads# ls -l *.deb
-rw-rw-r-- 1 cbattula cbattula  35950 Mar 19 02:29 mysql-apt-config_0.8.9-1_all.deb
-rw-rw-r-- 1 cbattula cbattula 150236 Mar 20 04:25 mysql-connector-python_8.0.6-rc-1ubuntu16.04_all.deb
root@XenialXerus:/home/cbattula/Downloads# dpkg -i mysql-connector-python_8.0.6-rc-1ubuntu16.04_all.deb 
Selecting previously unselected package mysql-connector-python.
(Reading database ... 249102 files and directories currently installed.)
Preparing to unpack mysql-connector-python_8.0.6-rc-1ubuntu16.04_all.deb ...
Unpacking mysql-connector-python (8.0.6-rc-1ubuntu16.04) ...
Setting up mysql-connector-python (8.0.6-rc-1ubuntu16.04) ...
root@XenialXerus:/home/cbattula/Downloads# 
root@XenialXerus:/home/cbattula/Downloads# dpkg -l|grep mysql
ii  mysql-apt-config                           0.8.9-1                                      all          Auto configuration for MySQL APT Repo.
ii  mysql-client                               5.7.21-1ubuntu16.04                          amd64        MySQL Client meta package depending on latest version
ii  mysql-common                               5.7.21-1ubuntu16.04                          amd64        MySQL Common
ii  mysql-community-client                     5.7.21-1ubuntu16.04                          amd64        MySQL Client
ii  mysql-community-server                     5.7.21-1ubuntu16.04                          amd64        MySQL Server
ii  mysql-connector-python                     8.0.6-rc-1ubuntu16.04                        all          MySQL database driver written in Python
ii  mysql-server                               5.7.21-1ubuntu16.04                          amd64        MySQL Server meta package depending on latest version
ii  mysql-shell:amd64                          8.0.4-rc-1ubuntu16.04                        amd64        MySQL Shell (part of MySQL Server) 8.0
root@XenialXerus:/home/cbattula/Downloads# 
root@XenialXerus:/home/cbattula/Downloads# python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> import mysql.connector
>>> 
root@XenialXerus:/home/cbattula/Downloads# cat /etc/*release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.4 LTS"
NAME="Ubuntu"
VERSION="16.04.4 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.4 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
[28 Mar 2018 3:32] weikang peng
Hello Chiranjeevi Battula,

You can try this to repeat the bug,

git clone https://github.com/pengwk/mysql_bug_89889
cd mysql_bug_89889
sudo bash repeat.sh

Thank you!
[22 Jan 2019 7:16] Masaaki HIROSE
It seems that segmentaion fault occurs when firstly load system's libcrypto.so and then load mysql-connector-python's libssl.so.

mysql-connector-python has own libcrypto.so and libssl.so and load both so files at import time.
Python's random package load libcrypto.so from system library path.
Python's ssl package load libcrypto.so and libssl.so from system library path.

`import random; import mysql.connector` cause that at first libcrypto.so is loaded from system library path then libssl.so is loaded from mysql-connector-python's mysql-vendor/ directory. It seems to me that system libcrypto.so and mysql-connector-python libssl.so are incompatible, so cause segmentation fault.

In case of `import mysql.connector; import random`, at first both libcrypto.so and libsso.so are loaded from mysql-vendor/ directory, then random package requests to load libcrypto.so and use already loaded (from mysql-vendor/ directory) libcrypto.so. It doesn't cause segmentation fault.

Additional note:
On distributes which have libssl1.1 like Ubuntu 18.04, python's packages (random, ssl) load libcrypto.so.1.1 and libssl.so.1.1, so segmentation fault doesn't occur because mysql-connector-python's are different version (libcrypto.so.1.0.0 and libssl.so.1.0.0).

How to repeat:

$ docker run -i -t ubuntu:16.04 /bin/bash

# apt update
# apt install python3 python3-pip strace
# pip3 install mysql-connector-python

# python3 -c 'import mysql.connector; print("OK")'
OK
# strace python3 -c 'import mysql.connector; print("OK")' |& grep -e libcrypto -e libssl
open("/usr/local/lib/python3.5/dist-packages/mysql-vendor/libssl.so.1.0.0", O_RDONLY|O_CLOEXEC) = 3
open("/usr/local/lib/python3.5/dist-packages/mysql-vendor/libcrypto.so.1.0.0", O_RDONLY|O_CLOEXEC) = 3

# python3 -c 'import random; import mysql.connector; print("OK")'
Segmentation fault
# strace python3 -c 'import random; import mysql.connector; print("OK")' |& grep -e libcrypto -e libssl
open("/lib/x86_64-linux-gnu/libcrypto.so.1.0.0", O_RDONLY|O_CLOEXEC) = 3
open("/usr/local/lib/python3.5/dist-packages/mysql-vendor/libssl.so.1.0.0", O_RDONLY|O_CLOEXEC) = 3

# python3 -c 'import mysql.connector; import random; print("OK")'
OK
# strace python3 -c 'import mysql.connector; import random; print("OK")' |& grep -e libcrypto -e libssl
open("/usr/local/lib/python3.5/dist-packages/mysql-vendor/libssl.so.1.0.0", O_RDONLY|O_CLOEXEC) = 3
open("/usr/local/lib/python3.5/dist-packages/mysql-vendor/libcrypto.so.1.0.0", O_RDONLY|O_CLOEXEC) = 3

# python3 -c 'import ssl; import mysql.connector; print("OK")'
OK
# strace python3 -c 'import ssl; import mysql.connector; print("OK")' |& grep -e libcrypto -e libssl
open("/lib/x86_64-linux-gnu/libssl.so.1.0.0", O_RDONLY|O_CLOEXEC) = 3
open("/lib/x86_64-linux-gnu/libcrypto.so.1.0.0", O_RDONLY|O_CLOEXEC) = 3
[10 Feb 2019 21:32] Andrew Beveridge
I just encountered this too, though with the setuptools pkg_resources module rather than with random.

For me, I'm able to run my python script fine directly with:
# python3 test_script.py
OK

but unable to run it by using the entry point added by setuptools:
# test_script
Segmentation fault (core dumped)

I did a little bit more digging to isolate the issue, and it looks like the same issue with libcrypto.
The are the results I get (on Ubuntu 16.04.5 LTS), in a clean virtualenv:

# python3 -c 'import mysql.connector; print("OK")'
OK

# strace python3 -c 'import mysql.connector; print("OK")' |& grep -e libcrypto -e libssl
open("venv/lib/python3.7/site-packages/mysql-vendor/libssl.so.1.0.0", O_RDONLY|O_CLOEXEC) = 3
open("venv/lib/python3.7/site-packages/mysql-vendor/libcrypto.so.1.0.0", O_RDONLY|O_CLOEXEC) = 3

# python3 -c 'from pkg_resources import load_entry_point; import mysql.connector; print("OK")'
Segmentation fault (core dumped)

# strace python3 -c 'from pkg_resources import load_entry_point; import mysql.connector; print("OK")' |& grep -e libcrypto -e libssl
open("/lib/x86_64-linux-gnu/libcrypto.so.1.0.0", O_RDONLY|O_CLOEXEC) = 3
open("venv/lib/python3.7/site-packages/mysql-vendor/libssl.so.1.0.0", O_RDONLY|O_CLOEXEC) = 3

I'm not really sure I understand why the version of libcrypto installed in my virtualenv doesn't match the one in the system location:
# ls -l /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
-rw-r--r-- 1 root root 2365952 Dec  4 20:07 /lib/x86_64-linux-gnu/libcrypto.so.1.0.0

# ls -l venv/lib/python3.7/site-packages/mysql-vendor/libcrypto.so.1.0.0
-rwxrwxrwx 1 andrew andrew 2601160 Feb 10 20:09 venv/lib/python3.7/site-packages/mysql-vendor/libcrypto.so.1.0.0*

I guess something in setuptools is loading the system version rather than the venv version, but I'm not really sure where to go from here.

Any suggestions for a workaround?
[12 Feb 2019 4:35] Masaaki HIROSE
workaround: install "--implementation py" (not inlcude libssl.so and libcrypto.so) instead of cp.

pip3 download --no-deps --implementation py --only-binary=:all: mysql-connector-python
pip3 install ./mysql_connector_python-8.0.14-py2.py3-none-any.whl
[17 Apr 2019 15:12] Dan Cvrcek
I had the same problem with 8.0.15 mysql.connector. I experienced segmentation faults repeatedly after my application started. Error messages eventually started pointing towards a problem in openssl library.

The work around suggested (12 Feb) worked for me.