Fokko commented on PR #4464:
URL: https://github.com/apache/iceberg-python/pull/4464#issuecomment-7068401093

   Thanks for digging into this @ialarmedalien! The traceback on close is 
annoying, but I'm not sure this is the right place to fix it.
   
   **The branch mostly repeats what SQLAlchemy already does.** The pysqlite 
dialect already does `pysqlite_opts.setdefault("check_same_thread", not 
self._is_url_file_db(url))`. File-backed databases already get 
`check_same_thread=False` with a `QueuePool`, and a `check_same_thread` in the 
URL query is already respected. So the only behavior this changes is for 
in-memory databases, where SQLAlchemy keeps the check on deliberately. The 
`"check_same_thread" not in url.query` guard is only needed because 
`connect_args` overrides the URL. That puts the rule partly in our code and 
partly in the dialect, and the driver-name check misses variants like 
`pysqlite_numeric`.
   
   **It doesn't fix the underlying problem.** With `sqlite:///:memory:` and 
`SingletonThreadPool`, every thread gets its own empty database. 
`test_sql_catalog_sqlite_memory_preserves_thread_isolation` actually asserts 
that a worker thread gets `no such table`. A catalog created in a worker and 
used or closed on the main thread was never usable across threads, and this PR 
only stops the error at close time. I don't think we should lock that behavior 
in with a test.
   
   If we want `InMemoryCatalog` to work across threads, I'd scope the fix to 
`InMemoryCatalog` and use SQLAlchemy's documented recipe for sharing a memory 
database:
   
   ```python
   engine = create_engine("sqlite://", poolclass=StaticPool, 
connect_args={"check_same_thread": False})
   ```
   
   Access has to be serialized, but the `InMemoryCatalog` docstring already 
says it doesn't support concurrent access. I tried it locally: a catalog 
created in a worker thread can be read from both threads and closes cleanly 
from the main thread, with no pool error or `ResourceWarning`. One test 
covering "create in a worker, use and close on the main thread" would be enough.
   
   For `SqlCatalog` itself, I'd leave the engine construction alone. Users who 
want different behavior can already pass `?check_same_thread=false` in the URI 
or pass their own `engine=`.
   
   What do you think?
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to