openwallet-foundation / credo-ts

Typescript framework for building decentralized identity and verifiable credential solutions
https://credo.js.org
Apache License 2.0
276 stars 202 forks source link

RecordDuplicateError: CacheRecord: Record with id DID_POOL_CACHE already exists #968

Closed dinkar-jain closed 1 year ago

dinkar-jain commented 2 years ago

While accepting multiple credential offers at the same time the above error mentioned in the title is originating. Error Line - await agent.credentials.acceptOffer(payload.credentialRecord.id); Error :- /home/ubuntu/CertificateX/backend/node_modules/@aries-framework/core/build/storage/IndyStorageService.js:123 throw new error_1.RecordDuplicateError(Record with id ${record.id} already exists, { recordType: record.type }); ^ RecordDuplicateError: CacheRecord: Record with id DID_POOL_CACHE already exists at IndyStorageService.save (/home/ubuntu/CertificateX/backend/node_modules/@aries-framework/core/build/storage/IndyStorageService.js:123:23) at async PersistedLruCache.fetchCacheRecord (/home/ubuntu/CertificateX/backend/node_modules/@aries-framework/core/build/cache/PersistedLruCache.js:38:13) at async PersistedLruCache.getCache (/home/ubuntu/CertificateX/backend/node_modules/@aries-framework/core/build/cache/PersistedLruCache.js:23:33) at async PersistedLruCache.get (/home/ubuntu/CertificateX/backend/node_modules/@aries-framework/core/build/cache/PersistedLruCache.js:13:23) at async IndyPoolService.getPoolForDid (/home/ubuntu/CertificateX/backend/node_modules/@aries-framework/core/build/modules/ledger/services/IndyPoolService.js:53:35) at async IndyLedgerService.getCredentialDefinition (/home/ubuntu/CertificateX/backend/node_modules/@aries-framework/core/build/modules/ledger/services/IndyLedgerService.js:136:26) at async CredentialService.createRequest (/home/ubuntu/CertificateX/backend/node_modules/@aries-framework/core/build/modules/credentials/services/CredentialService.js:346:38) at async CredentialsModule.acceptOffer (/home/ubuntu/CertificateX/backend/node_modules/@aries-framework/core/build/modules/credentials/CredentialsModule.js:177:51) at async EventEmitter.<anonymous> (/home/ubuntu/CertificateX/backend/Holder Backend/server.js:63:21) { [cause]: undefined } Screenshot (18)

TimoGlastra commented 2 years ago

Interesting @dinkar-jain. This is probably because you're calling multiple methods that try to create the record at the same time. We have another issue like this here: https://github.com/hyperledger/aries-framework-javascript/issues/899

We should put a lock on such actions so it won't be possible to executre such actions in parallel and they'll instead wait on each other, or we already need to create the records at initialization.

I've been using for async-mutex for the multi-tenancy work recently and that works suprisingly well, maybe we can start leveraging if for other parts of the codebase

berendsliedrecht commented 2 years ago

+1 for using a mutex to add locks in AFJ. However, is there a reason why we the cache record has a default DID_POOL_CACHE id? I am very unfamiliar with the caching system that we use.

dinkar-jain commented 2 years ago

Thanks @TimoGlastra. async-mutex library worked like a charm. The code is properly working now.

TimoGlastra commented 2 years ago

Great to hear! Reopening this issue as it would be nice to fix it in AFJ itself

dinkar-jain commented 2 years ago

@TimoGlastra Sure. Should we use mutex in AFJ itself??