Skip to content

Instantly share code, notes, and snippets.

@pirate pirate/libssl_bug.py
Created Jan 19, 2017

Embed
What would you like to do?
Code needed to reproduce libssl bug on python3.6 on mac
#!/usr/bin/env python3.6
"""
Code needed to reproduce a libssl segfault with python3.6 on mac.
To run:
brew upgrade python3
pip3 install websocket-client
python3.6 libssl_bug.py
> 'python3.6 libssl_bug.py' terminated by signal SIGSEGV (Address boundary error)
Full crash dump: https://gist.github.com/pirate/e1485110093d2d9fc49596e6ff481777
"""
import threading
from time import sleep
from websocket import create_connection # pip install websocket-client
URL = 'wss://echo.websocket.org/' # simple echo server, with SSL
SSL_OPTS = {'cert_reqs': 0} # the 0 is equivalent to ssl.CERT_NONE
NUM_THREADS = 4
class SocketThread(threading.Thread):
keep_running = True
def run(self):
self.ws = create_connection(URL, sslopt=SSL_OPTS)
self.ws.send('{"type": "Test Message"}')
self.ws.recv()
while self.keep_running:
try:
self.ws.send('{"type": "Test Message"}')
self.ws.recv()
except Exception:
if self.keep_running:
raise
def single_thread_test():
# works fine
sock = create_connection('wss://localhost', sslopt=SSL_OPTS)
sock.send('{"type": "Test Message"}')
sock.recv()
sleep(3)
sock.close()
def multi_thread_test(close_after):
# causes segfault when closing websockets, somewhere in a libssl destructor
threads = []
for _ in range(NUM_THREADS):
t = SocketThread()
t.start()
threads.append(t)
sleep(4)
for t in threads:
t.keep_running = False
if close_after:
t.ws.close() # libssl segfaults on python3.6 when closing a wss:// connection with VERIFY_SSL=False
t.join()
if __name__ == '__main__':
# single_thread_test() # works fine
# multi_thread_test(close_after=False) # works fine
multi_thread_test(close_after=True) # 'python3.6 libssl_bug.py' terminated by signal SIGSEGV (Address boundary error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.