diff --git a/src/aof.cpp b/src/aof.cpp index 6ed5652b0..d7094e54f 100644 --- a/src/aof.cpp +++ b/src/aof.cpp @@ -1460,9 +1460,9 @@ int rewriteAppendOnlyFileRio(rio *aof) { serverPanic("Unknown object type"); } /* Save the expire time */ - std::unique_lock ul(g_expireLock); - expireEntry *pexpire = db->getExpire(&key); - if (pexpire != nullptr) { + if (o->FExpires()) { + std::unique_lock ul(g_expireLock); + expireEntry *pexpire = db->getExpire(&key); for (auto &subExpire : *pexpire) { if (subExpire.subkey() == nullptr) { @@ -1480,7 +1480,6 @@ int rewriteAppendOnlyFileRio(rio *aof) { if (rioWriteBulkLongLong(aof,subExpire.when()) == 0) return false; // common } } - ul.unlock(); /* Read some diff from the parent process from time to time. */ if (aof->processed_bytes > processed+AOF_READ_DIFF_INTERVAL_BYTES) { diff --git a/src/db.cpp b/src/db.cpp index ab9bb64d4..c892e765f 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -1738,15 +1738,15 @@ void propagateSubkeyExpire(redisDb *db, int type, robj *key, robj *subkey) /* Check if the key is expired. Note, this does not check subexpires */ int keyIsExpired(const redisDbPersistentDataSnapshot *db, robj *key) { + /* Don't expire anything while loading. It will be done later. */ + if (g_pserver->loading) return 0; + std::unique_lock ul(g_expireLock); const expireEntry *pexpire = db->getExpire(key); mstime_t now; if (pexpire == nullptr) return 0; /* No expire for this key */ - /* Don't expire anything while loading. It will be done later. */ - if (g_pserver->loading) return 0; - long long when = -1; for (auto &exp : *pexpire) { diff --git a/src/rdb.cpp b/src/rdb.cpp index 68435d347..2bee255d1 100644 --- a/src/rdb.cpp +++ b/src/rdb.cpp @@ -1150,11 +1150,14 @@ int saveKey(rio *rdb, const redisDbPersistentDataSnapshot *db, int flags, size_t robj key; initStaticStringObject(key,(char*)keystr); - std::unique_lock ul(g_expireLock); - const expireEntry *pexpire = db->getExpire(&key); - serverAssert((o->FExpires() && pexpire != nullptr) || (!o->FExpires() && pexpire == nullptr)); - if (pexpire == nullptr) - ul.unlock(); // no need to hold the lock if we're not saving the expire + std::unique_lock ul(g_expireLock, std::defer_lock); + const expireEntry *pexpire = nullptr; + if (o->FExpires()) + { + ul.lock(); + pexpire = db->getExpire(&key); + serverAssert((o->FExpires() && pexpire != nullptr) || (!o->FExpires() && pexpire == nullptr)); + } if (rdbSaveKeyValuePair(rdb,&key,o,pexpire) == -1) return 0;