Skip to content

Commit 688b59a

Browse files
committed
CCBC-1636: deallocate old packet when updating collection ID
mcreq_renew_packet() requires the caller to deallocate original copy, otherwise the memory will be only released by pipeline destructor. Change-Id: I833c19313c96bcc3b785f414f55938eb8d3d2e64 Reviewed-on: https://review.couchbase.org/c/libcouchbase/+/206375 Tested-by: Build Bot <build@couchbase.com> Reviewed-by: Michael Reiche <michael.reiche@couchbase.com>
1 parent a24ea8c commit 688b59a

3 files changed

Lines changed: 10 additions & 6 deletions

File tree

src/mc/mcreq.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ static mc_PACKET *check_collection_id(mc_PIPELINE *pipeline, mc_PACKET *packet)
288288
if (collection_id_length == 0) {
289289
// but collection id prefix was not encoded, we should assume default collection and prepend zero as
290290
// a collection identifier
291-
packet = mcreq_set_cid(packet, 0);
291+
packet = mcreq_set_cid(pipeline, packet, 0);
292292
}
293293
break;
294294

@@ -698,10 +698,13 @@ static void mcreq_set_cid_field(mc_PACKET *packet, uint32_t cid)
698698
packet->flags |= MCREQ_F_HASCID;
699699
}
700700

701-
mc_PACKET *mcreq_set_cid(mc_PACKET *packet, uint32_t cid)
701+
mc_PACKET *mcreq_set_cid(mc_PIPELINE *pipeline, mc_PACKET *packet, uint32_t cid)
702702
{
703703
if ((packet->flags & MCREQ_F_DETACHED) == 0) {
704-
packet = mcreq_renew_packet(packet);
704+
mc_PACKET *copy = mcreq_renew_packet(packet);
705+
mcreq_wipe_packet(pipeline, packet);
706+
mcreq_release_packet(pipeline, packet);
707+
packet = copy;
705708
}
706709
mcreq_set_cid_field(packet, cid);
707710
return packet;

src/mc/mcreq.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ uint32_t mcreq_get_size(const mc_PACKET *packet);
705705

706706
uint32_t mcreq_get_cid(lcb_INSTANCE *instance, const mc_PACKET *packet, int *cid_set);
707707

708-
mc_PACKET *mcreq_set_cid(mc_PACKET *packet, uint32_t cid);
708+
mc_PACKET *mcreq_set_cid(mc_PIPELINE *pipeline, mc_PACKET *packet, uint32_t cid);
709709

710710
/**
711711
* @brief Get the vBucket for the request

src/mcserver/mcserver.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,10 @@ bool Server::handle_unknown_collection(MemcachedResponse &resp, mc_PACKET *oldpk
299299
wrapper.pkt = mcreq_renew_packet(oldpkt);
300300
wrapper.instance = instance;
301301
wrapper.timeout = LCB_NS2US(MCREQ_PKT_RDATA(wrapper.pkt)->deadline - now);
302-
auto operation = [orig_status](const lcb_RESPGETCID *, packet_wrapper *wrp) {
302+
mc_PIPELINE *pipeline{this};
303+
auto operation = [orig_status, pipeline](const lcb_RESPGETCID *, packet_wrapper *wrp) {
303304
if ((wrp->pkt->flags & MCREQ_F_NOCID) == 0) {
304-
wrp->pkt = mcreq_set_cid(wrp->pkt, wrp->cid);
305+
wrp->pkt = mcreq_set_cid(pipeline, wrp->pkt, wrp->cid);
305306
}
306307
/** Reschedule the packet again .. */
307308
wrp->pkt->flags &= ~MCREQ_STATE_FLAGS;

0 commit comments

Comments
 (0)