wbolster / plyvel

Plyvel, a fast and feature-rich Python interface to LevelDB
https://plyvel.readthedocs.io/
Other
518 stars 75 forks source link

Late import of plyvel can trigger leveldb bug where unsafe dynamic load of libtcmalloc causes a crash #115

Closed CypherGrue closed 4 years ago

CypherGrue commented 4 years ago

I've run into a strange issue and not sure which project to get help from. My program exhibits undefined behaviour when plyvel is imported after wxpython, but not vice versa.

I have narrowed it down to a single line test case.

% echo "import plyvel; import wx; print ('done')" | python3
done
% echo "import wx; import plyvel; print ('done')" | python3
done
src/tcmalloc.cc:332] Attempt to free invalid pointer 0x7fb206639e40
zsh: done       echo "import wx; import plyvel; print ('done')" |
zsh: abort      python3

Just loading the plyvel library instead of the whole package is sufficient to trigger the issue e.g.:

from ctypes import CDLL
CDLL('/usr/local/lib/python3.7/site-packages/wx/_core.cpython-37m-darwin.so')
CDLL('/usr/local/lib/python3.7/site-packages/plyvel/_plyvel.cpython-37m-darwin.so')

Python2 fails to "pip install plyvel" out-of-the-box, but I can force a build with compile flags:

CFLAGS="-stdlib=libc++ -I/usr/local/Cellar/leveldb/1.22/include/" LDFLAGS="-stdlib=libc++ -L/usr/local/Cellar/leveldb/1.22/lib/" python -m pip install --upgrade --force-reinstall plyvel

And thusly built python2 exhibits the same issue. Same with python3.8.

I'm a bit lost where to go from here. Could this be an issue with my computer, the stdlib changes in OSX, or brew, cython, wxpython or plyvel?

MacOS 10.15.4
brew 2.2.16
python 3.7.7
wxpython 4.1.0
cython 0.29.17
plyvel 1.2.0
wbolster commented 4 years ago

sorry, this does not ring any bell for me :cry:

plyvel does not interact with wx in any way (at least not directly), nor does it use tcmalloc directly.

it indeed seems to be related to some conflicts in the libraries somehow, but i have no clue if this is caused by the compiler/linker/loader/whatever... sorry.

CypherGrue commented 4 years ago

red herring 🔴🐟

CypherGrue commented 4 years ago

Updated test case, plyvel might be off the hook

from ctypes import CDLL

### Any of these wx libs first
#CDLL('/usr/local/lib/python3.7/site-packages/wx/_core.cpython-37m-darwin.so')
CDLL('/usr/local/lib/python3.7/site-packages/wx/libwx_osx_cocoau_core-3.1.4.0.0.dylib')

### Followed by any of these plyvel dependencies
#CDLL('/usr/local/lib/python3.7/site-packages/plyvel/_plyvel.cpython-37m-darwin.so')
#CDLL('/usr/local/opt/leveldb/lib/libleveldb.1.dylib')
CDLL('/usr/local/Cellar/gperftools/2.7/lib/libtcmalloc.4.dylib')

### Give tcmalloc crash after execution:
# src/tcmalloc.cc:332] Attempt to free invalid pointer 0x7fcf91c3aaf0
# zsh: abort      python3
CypherGrue commented 4 years ago

https://github.com/gperftools/gperftools/issues/1066

CypherGrue commented 4 years ago

Known leveldb bug

https://github.com/google/leveldb/issues/634

wbolster commented 4 years ago

thanks, closing since this is not within plyvel's scope.