6

I'm facing an issue with a Python script that connects to a mirrored MS SQL server DB. It's throwing a segmentation fault when I try connecting to the DB for the second time. Both the app server and the DB instances are running on Google Compute Engine.

Here's some code replicating the issue:

import pyodbc

params = {
   'autocommit': True,
   'uid': 'myuser',
   'tds_version': '8.0',
   'DRIVER': '{mssql}',
   'pwd': 'mypassword',
   'server': 'sql-server-01',
   'database': 'mydb',
   'port': 1433,
}

c1 = pyodbc.connect(**params)
c2 = pyodbc.connect(**params)

The first connection (c1) succeeds, but the second connection (c2) fails immediately with segfault. "mydb" is mirrored to a second server (sql-server-02). Using a non-mirrored DB, or disabling mirroring for this DB, makes it go away.

We have tried upgrading several libs, and that didn't fix the issue. Versions:

  • Microsoft SQL Server: 12.00.2000 (latest)
  • Python: 2.7.6
  • pyodbc: 3.0.10 (latest)
  • unixODBC: 2.2.14p2-5ubuntu5, 2.3.0, 2.3.4 (latest)
  • MS ODBC driver for RedHat: 11.0.1790.0, 11.0.2270.0 (latest)

To add here, Java code performing the same steps works fine.

Any ideas?

Max Twardowski
  • 117
  • 1
  • 6

2 Answers2

3

The MSODBC driver has a lot of known issues, especially with multithreading. It sounds like you're running into this. I ran into it with Django's runserver; it would only work (and still with bugs in SQLRowCount) with the --nothreading option for Django's runserver.

Fortunately, Microsoft is now assembling a team to make a better performing, reliable driver (thank you, MS!). In the meantime, I use FreeTDS 0.95 (which supports up to TDS version 7.3, a la SQL Server 2008), which has treated me very well. Give that a try? Good luck.

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
  • Thanks for your answer. In fact, we used FreeTDS until MS published their own driver; the switch is possible, although we'll have to weigh pros and cons. Are there any links about the MS's new driver effort? – Max Twardowski Oct 13 '15 at 00:16
  • 1
    MS has admitted their driver quality is poor; here's a link to the discussion: https://groups.google.com/forum/#!topic/django-developers/FbBcUCzrSZo The meeting is actually happening in Seattle this week with some of the folks who've worked most closely on Django SQL Server support. – FlipperPA Oct 13 '15 at 10:03
  • I have tried using the --nothreading flag and I am still getting a segmentation fault – Jasonca1 Feb 25 '19 at 16:43
  • This was a few years ago, @Jasonca1; the Microsoft driver has vastly improved since then (as has Django). I'd open a new question with specifics on which version of the driver (FreeTDS or MSODBC), Django, pyodbc, and django-pyodbc-azure you are using, as much has changed since this answer. – FlipperPA Feb 25 '19 at 22:47
-1

It is a known issue in certain versions of MS ODBC + unixODBC. By upgrading to unixODBC-2.3.2 and the latest MS ODBC driver, solves the issue on some distributions of linux, e.g. ubuntu and debian. You can follow the steps for upgrade here: https://blog.afoolishmanifesto.com/posts/install-and-configure-the-ms-odbc-driver-on-debian/ it solved the double connection issue - can open multiple connections without segfaulting.

The same upgrade / steps did not work for me on RedHat 7.1 though - you still get a segfault on opening a connection twice in same session.

Fermion Portal
  • 956
  • 1
  • 7
  • 14