On 03/14/2011 02:12 PM, Carl Trieloff wrote:
On 03/14/2011 10:07 AM, Gordon Sim wrote:
Its the iterator that is the issue, not the smart pointer that it
points to. You test (it == bindingCache.end()) outside the lock and
that is not safe.

FYI -- .end() function is independent of the validity of the iterator.
'it' will either == the const of end, or ref the smart pointer which
is then safe, even if cleared. I maintain it it safe.

I maintain it is not and offer the attached test as evidence of that. This reliably crashes the broker after your change but not when that change is reverted.
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include <qpid/client/Connection.h>
#include <qpid/client/AsyncSession.h>
#include <qpid/client/Session.h>
#include <qpid/sys/Runnable.h>
#include <qpid/sys/Thread.h>
#include <boost/bind.hpp>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <iostream>
#include <vector>

using namespace qpid::client;
using namespace qpid::sys;

class Client : public Runnable
{
    Thread thread;
    bool failed;

    void run() 
    {
        try {
            execute();
        } catch(const std::exception& error) {
            std::cout << error.what() << std::endl;
            failed = true;
        }
    }
  protected:
    virtual void execute() = 0;

  public:
    Client () : failed(false) {}
    virtual ~Client() {}

    void start() 
    {
        thread = Thread(*this);
    }


    void wait()
    {
        thread.join();
    }
};

typedef std::vector<std::string> Strings;

class Binder : public Client
{
    const ConnectionSettings settings;
    const std::string exchange;
    const Strings queues;
    const Strings keys;

    void execute()
    {
        Connection connection;
        connection.open(settings);
        Session session = connection.newSession();
        while (true) {
            for (Strings::const_iterator q = queues.begin(); q != queues.end(); q++) {
                session.queueDeclare(arg::queue=*q);
                for (Strings::const_iterator i = keys.begin(); i != keys.end(); i++) {
                    session.exchangeBind(arg::exchange=exchange, arg::queue=*q, arg::bindingKey=*i);
                }
            }
            for (Strings::const_iterator q = queues.begin(); q != queues.end(); q++) {
                for (Strings::const_iterator i = keys.begin(); i != keys.end(); i++) {
                    session.exchangeUnbind(arg::exchange=exchange, arg::queue=*q, arg::bindingKey=*i);
                }
                session.queueDelete(arg::queue=*q);
            }
        }
    }

  public:
    Binder(const ConnectionSettings& s, const std::string& e, const Strings& q, const Strings& k) : 
        settings(s), exchange(e), queues(q), keys(k) {}

};

class Checker : public Client
{
    const ConnectionSettings settings;
    const std::string exchange;
    const Strings queues;
    const Strings keys;

    void checkKeysForQueue(Session& session, const std::string& q)
    {
        if (keys.empty()) {
            session.exchangeBound(arg::exchange=exchange, arg::queue=q);
        } else {
            for (Strings::const_iterator k = keys.begin(); k != keys.end(); k++) {
                session.exchangeBound(arg::exchange=exchange, arg::queue=q, arg::bindingKey=*k);
            }
        }
    }

    void execute()
    {
        Connection connection;
        connection.open(settings);
        Session session = connection.newSession();
        while (true) {
            if (queues.empty()) {
                checkKeysForQueue(session, "");
            } else {
                for (Strings::const_iterator q = queues.begin(); q != queues.end(); q++) {
                    session.queueDeclare(arg::queue=*q);
                    checkKeysForQueue(session, *q);
                }
            }
        }
    }

  public:
    Checker(const ConnectionSettings& s, const std::string& e, const Strings& q, const Strings& k) : 
        settings(s), exchange(e), queues(q), keys(k) {}
};

class Sender : public Client
{
    const ConnectionSettings settings;
    const std::string exchange;
    const Strings keys;

    void execute()
    {
        Connection connection;
        connection.open(settings);
        AsyncSession session = connection.newSession();
        Message message;
        while (true) {
            for (Strings::const_iterator k = keys.begin(); k != keys.end(); k++) {
                message.getDeliveryProperties().setRoutingKey(*k);
                session.messageTransfer(arg::destination=exchange, arg::content=message);
            }
        }
    }

  public:
    Sender(const ConnectionSettings& s, const std::string& e, const Strings& k) : 
        settings(s), exchange(e), keys(k) {}
};


typedef boost::ptr_vector<Client> Clients;

std::string getString(const std::string& base, uint i)
{
    return boost::lexical_cast<std::string>(boost::format("%1%:%2%") % base % i);
}

Strings getStrings(const std::string& base, uint count)
{
    Strings strings;
    for (uint i = 0; i < count; i++) {
        strings.push_back(getString(base, i));
    }
    return strings;
}

int main(int argc, char** argv) {
    try {
        std::string exchange = "amq.topic";
        ConnectionSettings settings;
        if (argc > 1) settings.host = argv[1];
        if (argc > 2) settings.port = atoi(argv[2]);
        if (argc > 3) exchange = argv[3];

        Clients clients;
        clients.push_back(new Checker(settings, exchange, getStrings("dummy", 1), Strings()));
        for (uint i = 0; i < 10; i++) {
            clients.push_back(new Binder(settings, exchange, getStrings((boost::format("queue%1%") % i).str(), 1000), getStrings("k", 1000)));
            clients.push_back(new Sender(settings, exchange, getStrings("k", 1000)));
        }

        std::for_each(clients.begin(), clients.end(), boost::bind(&Client::start, _1));
        std::for_each(clients.begin(), clients.end(), boost::bind(&Client::wait, _1));
        return 0;
    } catch(const std::exception& error) {
        std::cout << error.what() << std::endl;
    }
    return 1;  
}

---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:dev-subscr...@qpid.apache.org

Reply via email to