[Sqlalchemy-tickets] Issue #2964: Race condition in dialect initialization (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
|
From: dairiki <iss...@bi...> - 2014-02-18 22:21:42
|
New issue 2964: Race condition in dialect initialization https://bitbucket.org/zzzeek/sqlalchemy/issue/2964/race-condition-in-dialect-initialization dairiki: There is a race condition in `DefaultEngineStrategy` around [line 161](https://bitbucket.org/zzzeek/sqlalchemy/src/fdedb69d9c6d995fc85105c22f0f537ea25a0c44/lib/sqlalchemy/engine/strategies.py?at=master#cl-161) where `dialect.initialize()` gets called from a listener attached to the pool `first_connect` event. If there are multiple threads all rushing to query the database, however, the second thread to get a connection can start running queries before the first thread has finished initializing the dialect. (In my case, `dialect.returns_unicode_strings` was intermittently not being initialized before the first queries were performed. This resulted in a UnicodeResultProcessor being used when it shouldn't have been in turn leading to UnicodeEncodeError.) Not sure what the best fix is. My guess is that `dialect.initialized` should be hooked into the `connect` event instead of `first_connect`, with a wrapper which ensures that `dialect.initialize` only gets called once by the first thread to try — blocking subsequent threads until the initialization is complete. (The locking/blocking could perhaps be folded into `util.only_once`.) |