From a766e63dc982a52d74b8850bd12099875077f2ae Mon Sep 17 00:00:00 2001
From: David Goulet <dgoulet@efficios.com>
Date: Tue, 4 Dec 2012 18:17:55 -0500
Subject: [PATCH 2/2] Fix: don't steal key when adding a metadata stream

This was causing a stream corruption of the node key if the stream->key
of the metadata was matching a stream wait_fd making the stream not
findable and asserting when getting out of the metadata poll wait.

Now we lookup the stream before adding it to make sure it's unique and
don't try to steal the key anymore since wait_fd is unique to the
consumer.

Signed-off-by: David Goulet <dgoulet@efficios.com>
---
 src/common/consumer.c |   14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/src/common/consumer.c b/src/common/consumer.c
index 7acdda2..d276c2c 100644
--- a/src/common/consumer.c
+++ b/src/common/consumer.c
@@ -1866,6 +1866,8 @@ static int consumer_add_metadata_stream(struct lttng_consumer_stream *stream,
 {
 	int ret = 0;
 	struct consumer_relayd_sock_pair *relayd;
+	struct lttng_ht_iter iter;
+	struct lttng_ht_node_ulong *node;
 
 	assert(stream);
 	assert(ht);
@@ -1880,6 +1882,15 @@ static int consumer_add_metadata_stream(struct lttng_consumer_stream *stream,
 	 */
 
 	rcu_read_lock();
+
+	/*
+	 * Lookup the stream just to make sure it does not exist in our internal
+	 * state. This should NEVER happen.
+	 */
+	lttng_ht_lookup(ht, (void *)((unsigned long) stream->wait_fd), &iter);
+	node = lttng_ht_iter_get_node_ulong(&iter);
+	assert(node);
+
 	/* Find relayd and, if one is found, increment refcount. */
 	relayd = consumer_find_relayd(stream->net_seq_idx);
 	if (relayd != NULL) {
@@ -1900,9 +1911,6 @@ static int consumer_add_metadata_stream(struct lttng_consumer_stream *stream,
 		uatomic_dec(&stream->chan->nb_init_streams);
 	}
 
-	/* Steal stream identifier to avoid having streams with the same key */
-	consumer_steal_stream_key(stream->key, ht);
-
 	lttng_ht_add_unique_ulong(ht, &stream->node);
 
 	/*
-- 
1.7.10.4

