Skip to content

Commit

Permalink
Fix threading issue when accessing user instance in _get_codes of Com…
Browse files Browse the repository at this point in the history
…putationalResourceWidgets (aiidalab#543)

fixes aiidalab/aiidalab-qe#582

The _get_codes method is used in the multi-threading case by QEapp, and causes the issue. The error comes from we use `user.collection.get_default` which will return the same object which is not thread-safe. The workaround is to get the user by email (which is the unique index for user) with using `user.collectio.get(email=<emaill>)` which will return a new object.
  • Loading branch information
unkcpz authored Jan 22, 2024
1 parent 1acd3ec commit 9134305
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions aiidalab_widgets_base/computational_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def __init__(

self._setup_new_code_output = ipw.Output(layout={"width": self._output_width})

self._default_user_email = orm.User.collection.get_default().email

children = [
ipw.HBox([self.code_select_dropdown, self.btn_setup_new_code]),
self._setup_new_code_output,
Expand All @@ -123,8 +125,8 @@ def __init__(

def _get_codes(self):
"""Query the list of available codes."""
user = orm.User.collection.get(email=self._default_user_email)

user = orm.User.collection.get_default()
filters = (
{"attributes.input_plugin": self.default_calc_job_plugin}
if self.default_calc_job_plugin
Expand Down Expand Up @@ -660,6 +662,7 @@ class AiidaComputerSetup(ipw.VBox):

def __init__(self, **kwargs):
self._on_setup_computer_success = []
self._default_user_email = orm.User.collection.get_default().email

# List of widgets to be displayed.
self.label = ipw.Text(
Expand Down Expand Up @@ -828,7 +831,7 @@ def observe_memory_per_machine(change):

def _configure_computer(self, computer: orm.Computer, transport: str):
# Use default AiiDA user
user = orm.User.collection.get_default()
user = orm.User.collection.get(email=self._default_user_email)
if transport == "core.ssh":
self._configure_computer_ssh(computer, user)
elif transport == "core.local":
Expand Down Expand Up @@ -1313,6 +1316,7 @@ def __init__(self, description="Select computer:", **kwargs):
description (str): Text to display before dropdown.
"""
self._default_user_email = orm.User.collection.get_default().email

self.output = ipw.HTML()
self._dropdown = ipw.Dropdown(
Expand Down Expand Up @@ -1351,7 +1355,7 @@ def _get_computers(self) -> list:
"""Get the list of available computers."""

# Getting the current user.
user = orm.User.collection.get_default()
user = orm.User.collection.get(email=self._default_user_email)

return [
(c[0].label, c[0].uuid)
Expand Down

0 comments on commit 9134305

Please sign in to comment.