acapy: :bug: Concurrent issuance of revocable AnonCreds credential can lead to duplicate cred_rev_id and unrevocable credentials
When an issuer attempts to concurrently issue many AnonCreds to a holder, using a revocable credential definition, duplicate cred_rev_id’s can be created, leading to revocation of those credentials becoming impossible, as the revoke request will always fail later with a duplicate records error.
The bug is tested for here: https://github.com/didx-xyz/acapy-cloud/pull/1581
In the test we have an issuer, holder, and a revocable cred def. We issue 10 revocable credentials concurrently, and fetch cred_rev records before attempting to revoke.
Out of the box, the test fails with (our own log, not acapy’s):
Duplicate cred_rev_id found: 5 for credential v2-dd01350d-b9b4-498a-b400-6958cf18b549
(Note: it’s not 5 duplicates; “5” is the cred_rev_id that is duplicated)
Adding a module-level lock to acapy_agent/anoncreds/revocation.py for the _create_credential method solved the problem for us, and allows above test to pass. (Commit showing module lock: https://github.com/openwallet-foundation/acapy/commit/7c1072c21ae0e78173e2e96c4338e730de3efcd3)
Help is wanted for a cleaner solution, please.
About this issue
- Original URL
- State: open
- Created a year ago
- Comments: 15 (15 by maintainers)
This is the reason we manage the cred_rev_id assignment outside of Credo in our deployments. It’s basically pushing the problem to the user of the framework, but it’s actually quite simple to prevent duplicated identifiers being assigned outside of the framework. We have a postgres database where we increase the current indexCounter by one each time, due to how postgres handles this it will never produce duplicate ids. We then have a constraint that rejects the update if the index counter will exceed the max size of the revocation registry.
We do have a redis lock then to manage the updates to status lists, as well as creation of new revocation registries (to avoid multiple revocation registries being created concurrently for the same cred def for concurrent issuance).
AFAIK Askar does support locking of records (I believe triggered by the
for_updatewhen retrieving the record). This locks the record meaning it can solve the problem of concurrent updated to a record (since it seems the index is stored in the record and incremented by 1 each time).Edit: The locking from Askar uses the native postgres locking, and doesn’t seem to work for Askar SQLite: https://www.postgresql.org/docs/current/explicit-locking.html#LOCKING-ROWS
It is just postgres and sqlite. I’m not sure what to do about sqlite. Maybe we can just make it a condition that it can only be used for single instance deployments and if/when we have a redis support it could transition to that. I’m actually not sure how askar works for multi-instance sqlite, or if it does, currently.
I agree with basically having our own super lightweight of a locking service like sherlock for use in acapy. Postgres, or askar using postgres, would be the most pressing option to support.
It should be possible. Redis/Valkey has the benefit of writing values with expiry times, and returning False if the same key is being set again. That makes a Redis lock mechanism pretty straightforward.
So in Askar - try create a record, have a unique constraint to fail if it already exists, and have some TTL logic to cleanup locks on the off chance a lock isn’t removed as expected. Then it’s basically the same.
I’m not so clued about the Askar functionality that can solve this out-of-the-box, but there must be a way