diff --git a/CHANGES b/CHANGES
index 2beb452651c7d9ee4b9d6af7035659115d13d1d7..c997dd92d11cea6cb0d21a3f04478bab1ec85e7d 100644
--- a/CHANGES
+++ b/CHANGES
@@ -7,6 +7,7 @@ OpenLDAP 2.4.24 Engineering
 	Fixed slapd modify to return actual error (ITS#6581)
 	Fixed slapd-bdb entry cache delete failure (ITS#6577)
 	Fixed slapd-null back-config support (ITS#6624)
+	Fixed slapo-pcache callback freeing (ITS#6640)
 	Fixed slapo-ppolicy don't update opattrs on consumers (ITS#6608)
 	Fixed slapo-syncprov to send error if consumer is newer (ITS#6606)
 
diff --git a/servers/slapd/overlays/pcache.c b/servers/slapd/overlays/pcache.c
index 46ce69ce695195a4f48b89b2be637c6bae38074b..88b90c3a32ece82d6bbbeda632cf83f1d1bad343 100644
--- a/servers/slapd/overlays/pcache.c
+++ b/servers/slapd/overlays/pcache.c
@@ -2389,8 +2389,7 @@ over:;
 					entry_free(si->head);
 				}
 			}
-			op->o_callback = op->o_callback->sc_next;
-			op->o_tmpfree( cb, op->o_tmpmemctx );
+
 		} else if ( si->caching_reason != PC_IGNORE ) {
 			CachedQuery *qc = qm->addfunc(op, qm, &si->query,
 				si->qtemp, si->caching_reason, 1 );
@@ -2446,6 +2445,9 @@ over:;
 		} else {
 			filter_free( si->query.filter );
 		}
+
+		op->o_callback = op->o_callback->sc_next;
+		op->o_tmpfree( cb, op->o_tmpmemctx );
 	}
 
 	return SLAP_CB_CONTINUE;