My favorites | Sign in
Project Home Downloads Wiki
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions
Issue 15: python.exe crashes while connecting to MSSQL Database using pyodbc.connect
2 people starred this issue and may be notified of changes. Back to list
Status:  WontFix
Owner:  ----
Closed:  Mar 2009


 
Reported by siddhartha.veedaluru@gmail.com, Dec 17, 2008
What steps will reproduce the problem?
1. i have MSSQL Database on a remote machine
2. i have created a System DSN in my local machine using python script 
which uses ctypes module
3. using pyodbc module i'm trying to connect to remote database.

What is the expected output? What do you see instead?
establish connection without crashing the python

What version of the product are you using? On what operating system?
OS-: win 2003 server, 32bit
Installed python products: python 2.5,pyodbc-2.0.52,pywin32-211

Please provide any additional information below.
1) The script just exists with return code 128.
2) Sometime after(approx 20-30 mins) if i run the scripts,i was able to 
connect and no crash found.
3)After establishing the connection,if we terminate (Keyboard Interrupt)
the script without closing the connection and once again try to 
connect,python.exe crashes

The scripts terminates abnormally and doesnt give any traceback.
i can see the dumps only when i reboot the machine.

Following is the code snippet of my script that are used to do the above 
tasks
Script to Create a System DSN
----------------------------------------------
def create_sys_dsn(driver, **kw):
    "Create a  system DSN"
    ODBC_ADD_SYS_DSN = 4
    nul = chr(0)
    attributes = []
    for attr in kw.keys():
        atrbStr = "%s=%s" % (attr, kw[attr])
        #print atrbStr
        attributes.append(atrbStr)
    #print driver
    return ctypes.windll.ODBCCP32.SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, 
driver, nul.join(attributes))

retCode = create_sys_dsn("SQL Server", SERVER=server, DESCRIPTION="TestDB 
DSN", DSN="TestDB", Database="testDB")
if retCode != 1:
    mesg = "Cannot Create TestDB DSN"
    print mesg
    sys.exit(1)
else:
    mesg = "TestDB DSN Created"
    print mesg
    sys.exit(0)
---------------------------------------------------------------------------

Script to connect to database
---------------------------------------------------------------------------
import pyodbc
try:
            DBCon = pyodbc.connect("DSN=TestDB;UID=tester;PWD=tester")
except pyodbc.Error, erno:
            mesg = "Unable to connect testDB Database check the UserID and 
Password"
            print mesg,erno
            return 1
return 0
:
:
:
:
DBCon.close()

Dec 17, 2008
Project Member #1 mkleehammer
I have seen an error like this on Windows 20003 (only) due to a bug in COM.  The SQL
Server driver calls CoInitializeEx because it might be used in COM+ transactions
(IIRC) and there was a race condition.  I reported this to Microsoft and received a
hotfix which fixed the issue.  The fix was rolled into the standard Microsoft patches
about a year ago (I think).  Do make sure you the latest Windows 2003 patches.

A quick way to test this (and a workaround) is to call CoInitializeEx yourself before
connecting.  Try adding this to your test script:

  import ctypes
  ole = ctypes.windll.ole32
  ole.CoInitializeEx(0, 0)

If that doesn't work, please provide an ODBC trace of the script and I'll go from there.


Status: Investigating
Dec 17, 2008
Project Member #2 mkleehammer
Further info: We have discussed this via email and he has tested this with the latest
2.1.x builds.

Dec 23, 2008
#3 siddhartha.veedaluru@gmail.com
Attached is the odbc trace file.
Sorry for the delay in getting it

Thanks
Siddhartha
SQL.LOG
2.0 KB   View   Download
Dec 24, 2008
Project Member #4 mkleehammer
Have you tried the following yet?

  import ctypes
  ole = ctypes.windll.ole32
  ole.CoInitializeEx(0, 0)

Please put this somewhere at the beginning of your program, before you import pyodbc,
and let me know if it fixes it.

I don't see anything unusual in the log, except for the connection string, but I
assume that is the logging.  When I create a trace, it looks like:

 WCHAR * 0x6BD2B1C0 [      -3] <Invalid string length!>

Yours looks like:

 WCHAR * 0x4BF78088 [      -3] "******\ 0"

I'm assuming you put something normal in there and the ODBC version you have is
printing slightly differently.  (BTW, -3 is SQL_NTS, which means "null terminated
string", so neither is an error.

Dec 24, 2008
Project Member #5 mkleehammer
One more thing, can you try passing a Unicode connection string:

  DBCon = pyodbc.connect(u"DSN=TestDB;UID=tester;PWD=tester")

(Notice the 'u' in front of the connection string)

When passed an ANSI string, pyodbc first tries to convert it to Unicode and call the
Unicode connection function.  (This is needed by Microsoft Access.)  If you pass a
Unicode string, no conversion is necessary.  It is possible that the conversion is in
error.
Mar 23, 2009
#7 siddhartha.veedaluru@gmail.com
After adding the below mentioned code.Its not crashing now
  import ctypes
  ole = ctypes.windll.ole32
  ole.CoInitializeEx(0, 0)

Which was mentioned by mkleehammer
Thanks hammer

rgds,
Sid
Mar 23, 2009
Project Member #8 mkleehammer
Thanks for the confirmation.  As mentioned before, this was a race condition that I
reported to MS, so there isn't anything pyodbc can do.  (I thought about putting the
CoInitializeEx call into pyodbc, but I think that would be a bad idea.)

FYI: I know there was a hotfix for this and I thought it went into a service pack.  
Status: WontFix
Jun 3, 2009
#9 sebastia...@googlemail.com
I just had exactly the same problem. The call to CoInitializeEx helped me out too.
Thank you very much for investigating this (was about to try adodbapi).

Interestingly I didn't have this problem in my testing evironment where SP2 is
installed. The production system had SP1 so I assume the patch comes with SP2.

Powered by Google Project Hosting