Skip to content

Commit

Permalink
Merge pull request #1439 from globalid/feat-IDENT-3372-askar-session-…
Browse files Browse the repository at this point in the history
…deadlock-v2

Fixed potential deadlocks by opening sessions only on demand
  • Loading branch information
ianco authored Oct 15, 2021
2 parents 1ec676a + 87cb2f8 commit fec69f1
Show file tree
Hide file tree
Showing 19 changed files with 360 additions and 331 deletions.
119 changes: 61 additions & 58 deletions aries_cloudagent/multitenant/admin/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,20 @@ async def wallets_list(request: web.BaseRequest):
"""

context: AdminRequestContext = request["context"]
profile = context.profile

query = {}
wallet_name = request.query.get("wallet_name")
if wallet_name:
query["wallet_name"] = wallet_name

async with context.session() as session:
try:
try:
async with profile.session() as session:
records = await WalletRecord.query(session, tag_filter=query)
results = [format_wallet_record(record) for record in records]
results.sort(key=lambda w: w["created_at"])
except (StorageError, BaseModelError) as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err
results = [format_wallet_record(record) for record in records]
results.sort(key=lambda w: w["created_at"])
except (StorageError, BaseModelError) as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err

return web.json_response({"results": results})

Expand All @@ -255,16 +256,17 @@ async def wallet_get(request: web.BaseRequest):
"""

context: AdminRequestContext = request["context"]
profile = context.profile
wallet_id = request.match_info["wallet_id"]

async with context.session() as session:
try:
try:
async with profile.session() as session:
wallet_record = await WalletRecord.retrieve_by_id(session, wallet_id)
result = format_wallet_record(wallet_record)
except StorageNotFoundError as err:
raise web.HTTPNotFound(reason=err.roll_up) from err
except BaseModelError as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err
result = format_wallet_record(wallet_record)
except StorageNotFoundError as err:
raise web.HTTPNotFound(reason=err.roll_up) from err
except BaseModelError as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err

return web.json_response(result)

Expand Down Expand Up @@ -306,17 +308,16 @@ async def wallet_create(request: web.BaseRequest):
if image_url:
settings["image_url"] = image_url

async with context.session() as session:
try:
multitenant_mgr = session.inject(BaseMultitenantManager)
try:
multitenant_mgr = context.profile.inject(BaseMultitenantManager)

wallet_record = await multitenant_mgr.create_wallet(
settings, key_management_mode
)
wallet_record = await multitenant_mgr.create_wallet(
settings, key_management_mode
)

token = multitenant_mgr.create_auth_token(wallet_record, wallet_key)
except BaseError as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err
token = multitenant_mgr.create_auth_token(wallet_record, wallet_key)
except BaseError as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err

result = {
**format_wallet_record(wallet_record),
Expand Down Expand Up @@ -368,15 +369,15 @@ async def wallet_update(request: web.BaseRequest):
if image_url is not None:
settings["image_url"] = image_url

async with context.session() as session:
try:
multitenant_mgr = session.inject(BaseMultitenantManager)
wallet_record = await multitenant_mgr.update_wallet(wallet_id, settings)
result = format_wallet_record(wallet_record)
except StorageNotFoundError as err:
raise web.HTTPNotFound(reason=err.roll_up) from err
except WalletSettingsError as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err
try:
multitenant_mgr = context.profile.inject(BaseMultitenantManager)
wallet_record = await multitenant_mgr.update_wallet(wallet_id, settings)

result = format_wallet_record(wallet_record)
except StorageNotFoundError as err:
raise web.HTTPNotFound(reason=err.roll_up) from err
except WalletSettingsError as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err

return web.json_response(result)

Expand All @@ -400,22 +401,23 @@ async def wallet_create_token(request: web.BaseRequest):
body = await request.json()
wallet_key = body.get("wallet_key")

async with context.session() as session:
try:
multitenant_mgr = session.inject(BaseMultitenantManager)
profile = context.profile
try:
multitenant_mgr = profile.inject(BaseMultitenantManager)
async with profile.session() as session:
wallet_record = await WalletRecord.retrieve_by_id(session, wallet_id)

if (not wallet_record.requires_external_key) and wallet_key:
raise web.HTTPBadRequest(
reason=f"Wallet {wallet_id} doesn't require"
" the wallet key to be provided"
)
if (not wallet_record.requires_external_key) and wallet_key:
raise web.HTTPBadRequest(
reason=f"Wallet {wallet_id} doesn't require"
" the wallet key to be provided"
)

token = multitenant_mgr.create_auth_token(wallet_record, wallet_key)
except StorageNotFoundError as err:
raise web.HTTPNotFound(reason=err.roll_up) from err
except WalletKeyMissingError as err:
raise web.HTTPUnauthorized(reason=err.roll_up) from err
token = multitenant_mgr.create_auth_token(wallet_record, wallet_key)
except StorageNotFoundError as err:
raise web.HTTPNotFound(reason=err.roll_up) from err
except WalletKeyMissingError as err:
raise web.HTTPUnauthorized(reason=err.roll_up) from err

return web.json_response({"token": token})

Expand Down Expand Up @@ -444,22 +446,23 @@ async def wallet_remove(request: web.BaseRequest):
body = await request.json()
wallet_key = body.get("wallet_key")

async with context.session() as session:
try:
multitenant_mgr = session.inject(BaseMultitenantManager)
profile = context.profile
try:
multitenant_mgr = profile.inject(BaseMultitenantManager)
async with profile.session() as session:
wallet_record = await WalletRecord.retrieve_by_id(session, wallet_id)

if (not wallet_record.requires_external_key) and wallet_key:
raise web.HTTPBadRequest(
reason=f"Wallet {wallet_id} doesn't require"
" the wallet key to be provided"
)

await multitenant_mgr.remove_wallet(wallet_id, wallet_key)
except StorageNotFoundError as err:
raise web.HTTPNotFound(reason=err.roll_up) from err
except WalletKeyMissingError as err:
raise web.HTTPUnauthorized(reason=err.roll_up) from err
if (not wallet_record.requires_external_key) and wallet_key:
raise web.HTTPBadRequest(
reason=f"Wallet {wallet_id} doesn't require"
" the wallet key to be provided"
)

await multitenant_mgr.remove_wallet(wallet_id, wallet_key)
except StorageNotFoundError as err:
raise web.HTTPNotFound(reason=err.roll_up) from err
except WalletKeyMissingError as err:
raise web.HTTPUnauthorized(reason=err.roll_up) from err

return web.json_response({})

Expand Down
Loading

0 comments on commit fec69f1

Please sign in to comment.