mkleehammer / pyodbc

Python ODBC bridge
https://github.com/mkleehammer/pyodbc/wiki
MIT No Attribution
2.95k stars 563 forks source link

macOS Sonoma - pyodbc 5.0.0b2 crashes - zsh: bus error python3 #1284

Closed carlashley closed 9 months ago

carlashley commented 1 year ago

Please first make sure you have looked at:

Environment

To diagnose, we usually need to know the following, including version numbers. On Windows, be sure to specify 32-bit Python or 64-bit:

Issue

When calling pyodbc.connect(dsn, ansi=True), it terminates with zsh: bus error python3 as the error; expected behaviour is the connection is established without crashing.

dsn="DSN=[redacted];UID=[redacted];PWD=[redacted];Encrypt=YES;TrustServerCertificate=YES;sslca=/usr/local/etc/openssl/certs/[redacted].crt"

ODBC trace:

[ODBC][39899][1696303011.832209][__handles.c][493]
        Exit:[SQL_SUCCESS]
            Environment = 0x12c050c00
[ODBC][39899][1696303011.841977][SQLSetEnvAttr.c][192]
        Entry:
            Environment = 0x12c050c00
            Attribute = SQL_ATTR_ODBC_VERSION
            Value = 0x3
            StrLen = 4
[ODBC][39899][1696303011.847820][SQLSetEnvAttr.c][384]
        Exit:[SQL_SUCCESS]
[ODBC][39899][1696303011.860956][SQLAllocHandle.c][469]
        Entry:
            Handle Type = 2
            Input Handle = 0x12c050c00
[ODBC][39899][1696303011.869945][SQLAllocHandle.c][585]
        Exit:[SQL_SUCCESS]
            Output Handle = 0x12e846400
[ODBC][39899][1696303011.881915][SQLDriverConnectW.c][298]
        Entry:
            Connection = 0x12e846400
            Window Hdl = 0x0
            Str In = [DSN=TASS;UID=tass;PWD=********************;Encrypt=YES;TrustServerCertificate=YES;sslca=/usr/local/etc/openssl/certs/*****...][length = 152 (SQL_NTS)]
            Str Out = 0x0
            Str Out Max = 0
            Str Out Ptr = 0x0
            Completion = 0
        Threading Level set from Driver Entry in ODBCINST.INI 99 from '99'

        Threading Level set from [ODBC] Section in ODBCINST.INI 0 from '0'

        UNICODE Using encoding ASCII 'UTF-8' and UNICODE 'UCS-2-INTERNAL'

I haven't been able to catch a crash log for Python 3.12, but the issue occurs with pyodbc 4.0.39 and Python 3.11.6. Issue is not present when using pyodbc 4.0.34 and Python 3.10.10.

-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Process:               Python [36980]
Path:                  /Library/Frameworks/Python.framework/Versions/3.11/Resources/Python.app/Contents/MacOS/Python
Identifier:            org.python.python
Version:               3.11.6 (3.11.6)
Code Type:             ARM-64 (Native)
Parent Process:        zsh [1352]
Responsible:           Terminal [1072]
User ID:               501

Date/Time:             2023-10-03 10:12:22.6217 +1000
OS Version:            macOS 14.0 (23A344)
Report Version:        12
Anonymous UUID:        [redacted]

Time Awake Since Boot: 12000 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGBUS)
Exception Codes:       KERN_PROTECTION_FAILURE at 0x00006000031682a0
Exception Codes:       0x0000000000000002, 0x00006000031682a0

Termination Reason:    Namespace SIGNAL, Code 10 Bus error: 10
Terminating Process:   exc handler [36980]

VM Region Info: 0x6000031682a0 is in 0x600000000000-0x600020000000;  bytes after start: 51806880  bytes before end: 485064031
      REGION TYPE                    START - END         [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      commpage (reserved)        1000000000-7000000000   [384.0G] ---/--- SM=NUL  ...(unallocated)
      GAP OF 0x5f9000000000 BYTES
--->  MALLOC_NANO              600000000000-600020000000 [512.0M] rw-/rwx SM=PRV  
      UNUSED SPACE AT END

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   ???                                 0x6000031682a0 ???
1   libcrypto.1.1.dylib                    0x122acf844 BIO_free_all + 32
2   libssl.1.1.dylib                       0x122819a6c SSL_free + 192
3   libtdsodbc.0.so                        0x107566538 tds_ssl_init + 1104 (tls.c:1076)
4   libtdsodbc.0.so                        0x10754e514 tds71_do_login + 8 (login.c:1405) [inlined]
5   libtdsodbc.0.so                        0x10754e514 tds_connect + 2452 (login.c:634)
6   libtdsodbc.0.so                        0x10754dc58 tds_connect + 216 (login.c:497)
7   libtdsodbc.0.so                        0x10754db74 tds_connect_and_login + 24 (login.c:729)
8   libtdsodbc.0.so                        0x10753b108 odbc_connect + 272 (odbc.c:410)
9   libtdsodbc.0.so                        0x107534b90 _SQLDriverConnect + 392 (odbc.c:659)
10  libtdsodbc.0.so                        0x107534cb0 SQLDriverConnect + 112 (odbc_export.h:49)
11  libodbc.2.dylib                        0x10479c28c SQLDriverConnect + 3072 (SQLDriverConnect.c:1455)
12  pyodbc.cpython-311-darwin.so           0x103203e34 Connection_New(_object*, bool, bool, long, bool, _object*, Object&) + 820
13  pyodbc.cpython-311-darwin.so           0x103210c60 mod_connect(_object*, _object*, _object*) + 1000
14  Python                                 0x10410d784 cfunction_call + 60
15  Python                                 0x1040a8d78 _PyObject_MakeTpCall + 128
16  Python                                 0x1041d006c _PyEval_EvalFrameDefault + 52804
17  Python                                 0x1041d59ec _PyEval_Vector + 156
18  Python                                 0x1040a9098 _PyObject_FastCallDictTstate + 96
19  Python                                 0x104136754 slot_tp_init + 180
20  Python                                 0x10412d0d8 type_call + 136
21  Python                                 0x1040a8d78 _PyObject_MakeTpCall + 128
22  Python                                 0x1041d006c _PyEval_EvalFrameDefault + 52804
23  Python                                 0x1041c1f7c PyEval_EvalCode + 276
24  Python                                 0x10423bae4 PyRun_InteractiveOneObjectEx + 836
25  Python                                 0x10423a818 _PyRun_InteractiveLoopObject + 136
26  Python                                 0x10423a050 _PyRun_AnyFileObject + 76
27  Python                                 0x10423eff4 PyRun_AnyFileExFlags + 132
28  Python                                 0x104263240 pymain_run_stdin + 160
29  Python                                 0x1042626b4 Py_RunMain + 1204
30  Python                                 0x104263aa8 pymain_main + 52
31  Python                                 0x1042642e0 Py_BytesMain + 40
32  dyld                                   0x1857d9058 start + 2224

Thread 1:
0   libsystem_pthread.dylib                0x185b4de28 start_wqthread + 0

Thread 2:
0   libsystem_pthread.dylib                0x185b4de28 start_wqthread + 0

Thread 0 crashed with ARM Thread State (64-bit):
    x0: 0x0000600002774000   x1: 0x0000000000000001   x2: 0x0000000000000000   x3: 0x0000000000000000
    x4: 0x0000000000000000   x5: 0x0000000000000001   x6: 0x00000c0100000c83   x7: 0x0000000000000b00
    x8: 0x0000000000000000   x9: 0x00006000031682a0  x10: 0x0000000000000600  x11: 0x000000009ac3880e
   x12: 0x00000000000007fb  x13: 0x00000000000007fd  x14: 0x000000009ae3900d  x15: 0x000000000000000d
   x16: 0x0000000122acf824  x17: 0x0000000000039000  x18: 0x0000000000000000  x19: 0x0000600002774000
   x20: 0x0000000129004f60  x21: 0x0000000129845a00  x22: 0x0000600002774090  x23: 0x0000600002774090
   x24: 0x0000000107588000  x25: 0x0000000000000003  x26: 0x000000010757f7db  x27: 0x0000000000000000
   x28: 0x0000000000000001   fp: 0x000000016cfd7660   lr: 0x0000000122ace96c
    sp: 0x000000016cfd7650   pc: 0x00006000031682a0 cpsr: 0x60001000
   far: 0x00006000031682a0  esr: 0x8200000f (Instruction Abort) Permission fault
<snip>
v-chojas commented 1 year ago

Could you try using the official Microsoft ODBC Driver for SQL Server and see if the same crash reproduces there?

carlashley commented 1 year ago

Ah, seems like Microsoft ships their ODBC driver via homebrew (I can't use homebrew in my org on Macs for various internal reasons). I'll see if I can install it without homebrew and update the ticket if it works.

v-chojas commented 1 year ago

You can download driver directly from the links in here: https://github.com/microsoft/homebrew-mssql-release

carlashley commented 1 year ago

Sorry I haven't got back, got distracted with other issues.

I've installed the MSSQL driver into /usr/local matching all the relevant paths per the downloaded arm64 tarball that the MS homebrew recipe downloads, and I've configured odbcinst.ini with the driver details, etc, but I bump into the same problem as #1116 , except of course all the file paths are based in /usr/local/...

>>> import pyodbc
>>> pyodbc.drivers()
['FreeTDS', 'ODBC Driver 18 for SQL Server']
>>> 
[host]:lib # otool -L /usr/local/bin/odbcinst 
/usr/local/bin/odbcinst:
    /usr/local/lib/libodbcinst.2.dylib (compatibility version 3.0.0, current version 3.0.0)
    /usr/local/lib/libltdl.7.dylib (compatibility version 11.0.0, current version 11.1.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.0.0)
[host]:lib # lipo -info /usr/local/lib/libmsodbcsql.18.dylib 
Non-fat file: /usr/local/lib/libmsodbcsql.18.dylib is architecture: arm64
[host]:lib # otool -L /usr/local/bin/isql                                                                                            
/usr/local/bin/isql:
    /usr/local/lib/libodbc.2.dylib (compatibility version 3.0.0, current version 3.0.0)
    /usr/local/lib/libltdl.7.dylib (compatibility version 11.0.0, current version 11.1.0)
    /usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
    /usr/lib/libedit.3.dylib (compatibility version 2.0.0, current version 3.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.0.0)
[host]:lib # otool -L /Library/Frameworks/Python.framework/Versions/Current/lib/python3.12/site-packages/pyodbc.cpython-312-darwin.so 
/Library/Frameworks/Python.framework/Versions/Current/lib/python3.12/site-packages/pyodbc.cpython-312-darwin.so (architecture x86_64):
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1600.151.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.0.0)
/Library/Frameworks/Python.framework/Versions/Current/lib/python3.12/site-packages/pyodbc.cpython-312-darwin.so (architecture arm64):
    /usr/local/lib/libodbc.2.dylib (compatibility version 3.0.0, current version 3.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1600.151.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.0.0)
[host]:lib # odbcinst -j show
unixODBC 2.3.12
DRIVERS............: /usr/local/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: /Users/[redacted]/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
[host]:lib # cat /usr/local/etc/odbcinst.ini 
[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsodbc.so

[ODBC Driver 18 for SQL Server]
Description=Microsoft ODBC Driver 18 for SQL Server
Driver=/usr/local/lib/libmsodbcsql.18.dylib
[host]:msodbcsql-18.3.2.1-arm64 #  isql -v -k "DRIVER={ODBC Driver 18 for SQL Server};DSN=[redacted];UID=[redacted];PWD=[redacted];SERVER=[redacted]" 
[08001][Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [OpenSSL library could not be loaded, make sure OpenSSL 1.0, 1.1, or 3.0 is installed]
[08001][Microsoft][ODBC Driver 18 for SQL Server]Client unable to establish connection. For solutions related to encryption errors, see https://go.microsoft.com/fwlink/?linkid=2226722
[ISQL]ERROR: Could not SQLDriverConnect
[host]:msodbcsql-18.3.2.1-arm64 #  isql -v -k "DRIVER={ODBC Driver 18 for SQL Server};DSN=[redacted];UID=[redacted];PWD=[redacted];Encrypt=YES;TrustServerCertificate=YES;sslca=/usr/local/etc/openssl/certs/[redacted].crt;SERVER=[redacted]" 
[08001][Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [OpenSSL library could not be loaded, make sure OpenSSL 1.0, 1.1, or 3.0 is installed]
[08001][Microsoft][ODBC Driver 18 for SQL Server]Client unable to establish connection
[01S00][Microsoft][ODBC Driver 18 for SQL Server]Invalid connection string attribute
[ISQL]ERROR: Could not SQLDriverConnect

I'm not bothered by the openssl error thrown by isql as it suggest to me that isql is working fine with the MS driver (but fails because it can't find openssl libraries where it's expecting them, this is a "stock" install of macOS with the standard openssl included in the OS).

This Can't open lib '/usr/local/lib/libmsodbcsql.18.dylib' : file not found error occurs in Python 3.10.10 to 3.12 (the system hardware and OS is the same, M1 Mac, macOS 14.0).

carlashley commented 1 year ago

Just noting here, I've installed pymssql v 2.1.5 (compiled), to test if that would work, and it is working fine.

carlashley commented 1 year ago

Some further info:

tsql -S [freetds server] -U [user] -P [password] with dumplog enabled:

Log:

log.c:187:Starting log file for FreeTDS 1.4.2
        on 2023-10-31 13:53:08 with debug flags 0x4fff.
iconv.c:371:tds_iconv_open(0x12b604630, UTF-8, 1)
iconv.c:356:Using trivial iconv
iconv.c:202:local name for ISO-8859-1 is ISO-8859-1
iconv.c:202:local name for UTF-8 is UTF-8
iconv.c:202:local name for UCS-2LE is UCS-2LE
iconv.c:202:local name for UCS-2BE is UCS-2BE
iconv.c:390:setting up conversions for client charset "UTF-8"
iconv.c:392:preparing iconv for "UTF-8" <-> "UCS-2LE" conversion
iconv.c:431:tds_iconv_open: done
net.c:391:Connecting with protocol version 7.4
net.c:318:Connecting to [redacted] port 1433
net.c:340:tds_setup_socket: connect(2) returned "Operation now in progress"
net.c:528:tds_open_socket() succeeded
packet.c:852:Sending packet
0000 12 01 00 3a 00 00 00 00-00 00 1a 00 06 01 00 20 |...:.... ....... |
0010 00 01 02 00 21 00 0c 03-00 2d 00 04 04 00 31 00 |....!... .-....1.|
0020 01 ff 09 00 00 00 00 00-01 4d 53 53 51 4c 53 65 |.<FF>...... .MSSQLSe|
0030 72 76 65 72 00 40 1f 00-00 00                   |rver.@.. ..|

packet.c:410:Received packet
0000 04 01 00 2b 00 00 01 00-00 00 1a 00 06 01 00 20 |...+.... ....... |
0010 00 01 02 00 21 00 01 03-00 22 00 00 04 00 22 00 |....!... ."....".|
0020 01 ff 0e 00 07 ff 00 00-01 00 00                |.<FF>...<FF>.. ...|

login.c:1387:detected crypt flag 1
tls.c:1019:setting default openssl cipher to:HIGH:!SSLv2:!aNULL:-DH
tls.c:147:in tds_push_func_login
tls.c:117:in tds_pull_func_login
packet.c:852:Sending packet
<connection established>

However when attempting a connection (now using pyobdc 5.0.1 and Python 3.12):

log.c:187:Starting log file for FreeTDS 1.4.2
        on 2023-10-31 13:12:22 with debug flags 0x4fff.
iconv.c:371:tds_iconv_open(0x11d65d550, UTF-8, 1)
iconv.c:356:Using trivial iconv
iconv.c:202:local name for ISO-8859-1 is ISO-8859-1
iconv.c:202:local name for UTF-8 is UTF-8
iconv.c:202:local name for UCS-2LE is UCS-2LE
iconv.c:202:local name for UCS-2BE is UCS-2BE
iconv.c:390:setting up conversions for client charset "UTF-8"
iconv.c:392:preparing iconv for "UTF-8" <-> "UCS-2LE" conversion
iconv.c:431:tds_iconv_open: done
net.c:391:Connecting with protocol version 7.4
net.c:318:Connecting to [redacted] port 1433
net.c:340:tds_setup_socket: connect(2) returned "Operation now in progress"
net.c:528:tds_open_socket() succeeded
packet.c:852:Sending packet
0000 12 01 00 3a 00 00 00 00-00 00 1a 00 06 01 00 20 |...:.... ....... |
0010 00 01 02 00 21 00 0c 03-00 2d 00 04 04 00 31 00 |....!... .-....1.|
0020 01 ff 09 00 00 00 00 00-01 4d 53 53 51 4c 53 65 |.<FF>...... .MSSQLSe|
0030 72 76 65 72 00 c2 1d 00-00 00                   |rver.<C2>.. ..|

packet.c:410:Received packet
0000 04 01 00 2b 00 00 01 00-00 00 1a 00 06 01 00 20 |...+.... ....... |
0010 00 01 02 00 21 00 01 03-00 22 00 00 04 00 22 00 |....!... ."....".|
0020 01 ff 0e 00 07 ff 00 00-01 00 00                |.<FF>...<FF>.. ...|

login.c:1387:detected crypt flag 1
tls.c:1019:setting default openssl cipher to:HIGH:!SSLv2:!aNULL:-DH
tls.c:1035:handshake failed with -1 12 1

If I use Python 3.10, as per earlier comments, it works fine.

Compile-time settings (established with the "configure" script)
                            Version: freetds v1.4.2
             freetds.conf directory: /usr/local/etc
     MS db-lib source compatibility: no
        Sybase binary compatibility: no
                      Thread safety: yes
                      iconv library: no
                        TDS version: auto
                              iODBC: no
                           unixodbc: yes
              SSPI "trusted" logins: no
                           Kerberos: no
                            OpenSSL: yes
                             GnuTLS: no
                               MARS: yes
carlashley commented 1 year ago

I've been able to get approval to use homebrew on a test machine....

Running macOS 14.1 (23B74) with brew versions of unixodbc installed and the Microsoft SQL driver installed as per, and installing pyodbc with pip3 install --no-binary :all: pyodbc, can confirm that pyodbc works as expected.

I need to do a test on another machine that is a fresh macOS install to verify if the self compiled unixodbc + freetds is failing and that it's not something related to my OS install.

mkleehammer commented 9 months ago

We've just released 5.1.0 with binaries for both ARM & Intel Macs. Can you try this version?

I'm going to close this for now since it is pretty old and we have other Mac issues and discussions. Feel free to reopen if this is still a problem.