TOSIT-IO / tdp-server

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

Reconfigure action can't be logged in the db #106

Open mehdibn opened 1 year ago

mehdibn commented 1 year ago

After reconfigure action, this error was generated (SQLite objects created in a thread can only be used in that same thread.):

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1057, in _rollback_impl
    self.engine.dialect.do_rollback(self.connection)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 683, in do_rollback
    dbapi_connection.rollback()
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 140652912154432 and this is thread id 140651983267584.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/uvicorn/protocols/http/httptools_impl.py", line 376, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/fastapi/applications.py", line 269, in __call__
    await super().__call__(scope, receive, send)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/applications.py", line 124, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/middleware/base.py", line 69, in __call__
    await response(scope, receive, send)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/responses.py", line 260, in __call__
    await wrap(partial(self.listen_for_disconnect, receive))
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/anyio/_backends/_asyncio.py", line 662, in __aexit__
    raise exceptions[0]
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/anyio/_backends/_asyncio.py", line 702, in _run_wrapped_task
    await coro
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/responses.py", line 256, in wrap
    await func()
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/responses.py", line 245, in stream_response
    async for chunk in self.body_iterator:
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/middleware/base.py", line 58, in body_stream
    raise app_exc
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/middleware/base.py", line 36, in coro
    await self.app(scope, request.receive, send_stream.send)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/middleware/gzip.py", line 24, in __call__
    await responder(scope, receive, send)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/middleware/gzip.py", line 43, in __call__
    await self.app(scope, receive, self.send_with_gzip)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/middleware/cors.py", line 92, in __call__
    await self.simple_response(scope, receive, send, request_headers=headers)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/middleware/cors.py", line 147, in simple_response
    await self.app(scope, receive, send)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/exceptions.py", line 93, in __call__
    raise exc
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/starlette/exceptions.py", line 82, in __call__
    await self.app(scope, receive, sender)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/contextlib2/__init__.py", line 748, in __aexit__
    raise exc_details[1]
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/contextlib2/__init__.py", line 731, in __aexit__
    cb_suppress = await cb(*exc_details)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/contextlib2/__init__.py", line 235, in __aexit__
    await self.gen.__anext__()
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/fastapi/concurrency.py", line 46, in contextmanager_in_threadpool
    cm.__exit__, None, None, None, limiter=exit_limiter
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/anyio/to_thread.py", line 32, in run_sync
    func, *args, cancellable=cancellable, limiter=limiter
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/contextvars/__init__.py", line 38, in run
    return callable(*args, **kwargs)
  File "/usr/lib/python3.6/contextlib.py", line 88, in __exit__
    next(self.gen)
  File "./tdp_server/api/dependencies.py", line 60, in get_db
    yield db
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1167, in __exit__
    self.close()
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1811, in close
    self._close_impl(invalidate=False)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1853, in _close_impl
    transaction.close(invalidate)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 923, in close
    transaction.close()
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2416, in close
    self._do_close()
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2639, in _do_close
    self._close_impl()
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2625, in _close_impl
    self._connection_rollback_impl()
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2617, in _connection_rollback_impl
    self.connection._rollback_impl()
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1059, in _rollback_impl
    self._handle_dbapi_exception(e, None, None, None, None)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2125, in _handle_dbapi_exception
    sqlalchemy_exception, with_traceback=exc_info[2], from_=e
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 211, in raise_
    raise exception
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1057, in _rollback_impl
    self.engine.dialect.do_rollback(self.connection)
  File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 683, in do_rollback
    dbapi_connection.rollback()
sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 140652912154432 and this is thread id 140651983267584.
(Background on this error at: https://sqlalche.me/e/14/f405
mdrutel commented 1 year ago

Hi,

I reproduced the problem with a call to /api/v1/plan/reconfigure (an async endpoint). The problem does not occur with sync endpoint. When there is nothing to restart, the server is supposed to answer a HTTP 400 status. But instead, a HTTP 500 is sent. This HTTP 500 is caused by the exception listed below :

File "/home/diode-ben-aissa01/dev/tdp-lib/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 683, in do_rollback dbapi_connection.rollback() sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 140652912154432 and this is thread id 140651983267584.

The server tried to rollback the transaction but couldn't because the SQL session is not in the same thread. One correction could be to close the session after each request.

mdrutel commented 1 year ago

Here is the discussion from the PR #121 :

I will close the PR and delete the branch.