Skip to content

Commit

Permalink
Handle error message during storage backend init better
Browse files Browse the repository at this point in the history
  • Loading branch information
agoscinski committed Feb 2, 2025
1 parent ca17924 commit 54f8fe3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/aiida/manage/configuration/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,15 +526,14 @@ def create_profile(
)

LOGGER.report('Initialising the storage backend.')
profile.storage_cls.initialise(profile)
try:
# not sure if scope is good, at least put it in log
#with contextlib.redirect_stdout(io.StringIO()):
#profile.storage_cls.initialise(profile)
pass
profile.storage_cls.initialise(profile)
except Exception as exception:
raise StorageMigrationError(
f'Storage backend initialisation failed, probably because the configuration is incorrect:\n{exception}'
) from exception
f'During initialisation of storage backend. Please check above traceback for cause.'
)
LOGGER.report('Storage initialisation completed.')

self.add_profile(profile)
Expand Down
6 changes: 3 additions & 3 deletions tests/manage/configuration/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ def test_create_profile_raises(config_with_profile, monkeypatch, entry_points):
config = config_with_profile
profile_name = uuid.uuid4().hex

def raise_storage_migration_error(*args, **kwargs):
raise exceptions.StorageMigrationError()
def raise_storage_migration_error(*_, **__):
raise exceptions.StorageMigrationError("Monkey patchted error")

monkeypatch.setattr(SqliteTempBackend, 'initialise', raise_storage_migration_error)
entry_points.add(InvalidBaseStorage, 'aiida.storage:core.invalid_base')
Expand All @@ -460,7 +460,7 @@ def raise_storage_migration_error(*args, **kwargs):
with pytest.raises(ValueError, match=r'The entry point `.*` could not be loaded'):
config.create_profile(profile_name, 'core.non_existant', {})

with pytest.raises(exceptions.StorageMigrationError, match='Storage backend initialisation failed.*'):
with pytest.raises(exceptions.StorageMigrationError, match='During initialisation of storage backend. Please check above traceback for cause.*'):
config.create_profile(profile_name, 'core.sqlite_temp', {})


Expand Down
18 changes: 18 additions & 0 deletions tests/storage/sqlite/test_container.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Test container initialization."""
import psutil, os

def test_file_descriptor_closed(aiida_profile):
"""Checks if the number of open file descriptors change during a reset."""
def list_open_file_descriptors():
process = psutil.Process(os.getpid())
return process.open_files()
# We have some connections active due to aiida profile during the first
# reset these are destroyed. We check the second time changes the number of
# open file descriptors.
# TODO The fix should keep the existing connections alive and just reuse them
# then one does not need to do two resets.
aiida_profile.reset_storage()
open_file_descriptors_before = list_open_file_descriptors()
aiida_profile.reset_storage()
open_file_descriptors_after = list_open_file_descriptors()
assert len(open_file_descriptors_before) == len(open_file_descriptors_after), f"Before these file descriptor were open:\n{open_file_descriptors_before}\n Now these are open:\n{open_file_descriptors_after}"

0 comments on commit 54f8fe3

Please sign in to comment.