TOSIT-IO / tdp-server

REST API for tdp-lib orchestration
2 stars 2 forks source link

106 reconfigure action cant be logged in the db #121

Closed mdrutel closed 1 year ago

mdrutel commented 1 year ago

Which issue(s) this PR fixes

Fixes #106

Additional comments

This fix was tested with a SQLite database and a PostgreSQL, although this bug only appears with SQLite DB, and with the /reconfigure and /resume endpoints. The other endpoints don't need the DB connection to be closed : if I add that, a sqlalchemy.orm.exc.DetachedInstanceError is raised :

sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <DeploymentLog at 0x7feb034b5760> is not bound to a Session; lazy load operation of attribute 'operations' cannot proceed (Background on this error at: https://sqlalche.me/e/14/bhk3)

Agreements

mdrutel commented 1 year ago

I activated the DEBUG mode, and noted that a nullPool was used with a SQLite DB :+1:

2023-03-07 15:19:55,379 DEBUG sqlalchemy.pool.impl.NullPool Created new connection <sqlite3.Connection object at 0x7f099125e990> 2023-03-07 15:19:55,379 DEBUG sqlalchemy.pool.impl.NullPool Connection <sqlite3.Connection object at 0x7f099125e990> checked out from pool 2023-03-07 15:19:55,379 DEBUG sqlalchemy.pool.impl.NullPool Connection <sqlite3.Connection object at 0x7f099125e990> is fresh, skipping pre-ping

If we use the parameter mode=memory in the DB connection, it's a sqlalchemy.pool.impl.SingletonThreadPool which is used :

2023-03-07 15:49:57,155 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Created new connection <sqlite3.Connection object at 0x7f6970d2b990> 2023-03-07 15:49:57,155 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Connection <sqlite3.Connection object at 0x7f6970d2b990> checked out from pool 2023-03-07 15:49:57,155 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Connection <sqlite3.Connection object at 0x7f6970d2b990> is fresh, skipping pre-ping [...] 2023-03-07 15:49:57,170 INFO sqlalchemy.engine.Engine ROLLBACK 2023-03-07 15:49:57,171 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Connection <sqlite3.Connection object at 0x7f6970d2b990> being returned to pool 2023-03-07 15:49:57,171 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Connection <sqlite3.Connection object at 0x7f6970d2b990> rollback-on-return

The internal error doen't appear with this modification.

mdrutel commented 1 year ago

Correction : with a SQLite database, the only way I have found to avoid this error is to add the option connect_args={'check_same_thread': False} when you create the engine. But this option is specific to SQLite.