Skip to content

Commit

Permalink
Fix SQLAlchemy threading error in DB server
Browse files Browse the repository at this point in the history
We cannot hold the DB connection forever in the worker thread as
the server thread accesses the database too.
  • Loading branch information
soininen committed Jan 21, 2025
1 parent 0d8d78d commit 5604944
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions spinedb_api/spine_db_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,23 +354,23 @@ def _do_work(self):
except Exception as error: # pylint: disable=broad-except
self._out_queue.put(error)
return
with self._db_map:
while True:
input_ = self._in_queue.get()
if input_ == self._CLOSE:
break
request, args, kwargs = input_
handler = {
"query": self._do_query,
"filtered_query": self._do_filtered_query,
"import_data": self._do_import_data,
"export_data": self._do_export_data,
"call_method": self._do_call_method,
"apply_filters": self._do_apply_filters,
"clear_filters": self._do_clear_filters,
}[request]
while True:
input_ = self._in_queue.get()
if input_ == self._CLOSE:
break
request, args, kwargs = input_
handler = {
"query": self._do_query,
"filtered_query": self._do_filtered_query,
"import_data": self._do_import_data,
"export_data": self._do_export_data,
"call_method": self._do_call_method,
"apply_filters": self._do_apply_filters,
"clear_filters": self._do_clear_filters,
}[request]
with self._db_map:
result = handler(*args, **kwargs)

Check warning on line 372 in spinedb_api/spine_db_server.py

View check run for this annotation

Codecov / codecov/patch

spinedb_api/spine_db_server.py#L371-L372

Added lines #L371 - L372 were not covered by tests
self._out_queue.put(result)
self._out_queue.put(result)

def run(self, request, args, kwargs):
with self._lock:
Expand Down Expand Up @@ -445,7 +445,7 @@ def open_db_map(self, server_address, db_url, upgrade, memory):
worker = self._workers.get(server_address)
if worker is None:
try:
worker = self._workers[server_address] = _DBWorker(db_url, upgrade, memory)
self._workers[server_address] = _DBWorker(db_url, upgrade, memory)

Check warning on line 448 in spinedb_api/spine_db_server.py

View check run for this annotation

Codecov / codecov/patch

spinedb_api/spine_db_server.py#L448

Added line #L448 was not covered by tests
except Exception as error: # pylint: disable=broad-except
return {"error": str(error)}
return {"result": True}
Expand Down

0 comments on commit 5604944

Please sign in to comment.