We've come across this when our MS SQL server became unreachable. This caused a bug in our code that brought our program to a screeching halt and of course pitchforks and torches of users to our door. We've been able to boil down our problem to this: If a user, Bob, attempts to connect to the downed database he will of course wait while the program attempts to connect. If at this point while Bob is waiting, a second user, Joe, attempts to connect and he will wait as well. After awhile Bob will timeout and get a proper error raised. However Joe's connection will timeout and a segmentation fault occurs bringing everything to a screeching halt.
We've been able to reliably reproduce this error with the following code
import threading
import datetime
import time
import pymssql
class ThreadClass(threading.Thread):
def run(self):
now = datetime.datetime.now()
print "%s connecting at time: %s" % (self.getName(), now)
conn = pymssql.connect(host="10.255.255.1", database='blah',
user="blah", password="pass")
for i in range(2):
t = ThreadClass()
t.start()
time.sleep(1)
This will cause a segfault after the first thread raises it's error. Is there a way to stop this segfault, make it properly raise an error, or is there something I'm missing here?
Pymssql version 1.0.2 and python 2.6.6.
We went and asked the question over at pymssql's user group as well to cover all our bases. According to one of the developers pymssql is not thread safe in the current stable release. It sounds like it might be in the development 1.9 release or in the next major 2.0 release. We will probably switch to a different module or maybe use some sort of connection pooler with it but that's probably more of a bandage fix and not really ideal.