Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observed Behavior: pyodbc.drivers() not thread safe #715

Closed
ourcookiemonster opened this issue Feb 26, 2020 · 5 comments
Closed

Observed Behavior: pyodbc.drivers() not thread safe #715

ourcookiemonster opened this issue Feb 26, 2020 · 5 comments

Comments

@ourcookiemonster
Copy link

pyodb.drivers() method can return an incomplete list of drivers if multiple threads call this method at the same time.

Based on the ODBC SQLDrivers method API, the c++ code must iterate through a list of drivers, specifying the start or the next available driver. If two threads have entered the for loop of mod_drivers() in pyodbcmodule.cpp one or both of the threads may miss a driver that the other thread has incremented through.

@ourcookiemonster
Copy link
Author

It is likely mod_datasources() suffers the same behavior.

@ourcookiemonster
Copy link
Author

import threading
import pyodbc

true_driver_count = len(pyodbc.drivers())
disparity_found = False

def test_drivers():
    global disparity_found
    while not disparity_found:
        thread_driver_count = len(pyodbc.drivers())
        if(thread_driver_count < true_driver_count):
            print('Driver Count Difference Detected')            
            disparity_found = True;

if __name__ == "__main__":
    thread_one = threading.Thread(target=test_drivers)
    thread_two = threading.Thread(target=test_drivers)

    thread_one.start()
    thread_two.start()

    thread_one.join()
    thread_two.join()

@v-makouz
Copy link
Contributor

v-makouz commented Mar 4, 2020

I can reproduce and will look into it

@v-makouz
Copy link
Contributor

v-makouz commented Mar 5, 2020

I made a PR that should fix it:
#722

I ran your repro and after several million iterations there were no mismatches

mkleehammer pushed a commit that referenced this issue Jan 22, 2021
* Merging updates. (#1)

Merging updates.

* fix for smalldatetime issue

* Fixed a bad merge

* Fix for inserting high unicode chars

* merge with main branch

* Fix for function sequence error

* reverted unnecessary file changes

* removed obsolete include

* fix for 540

* fix for TVP type mismatch issue

* Combined the IFs

* Fix for threading issues when using SQLDrivers and SQLDataSources

Co-authored-by: v-chojas <25211973+v-chojas@users.noreply.github.com>
Co-authored-by: v-chojas <v-chojas@microsoft.com>
@keitherskine
Copy link
Collaborator

Fixed by #722.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants