-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix concurrency issue in XmppSession::WriteReady
It's possible that XmppSession::WriteReady gets called after the xmpp connection for the session has already been deleted. Prevent WriteReady from from accessing freed memory by clearing the back pointer to the xmpp connection in the session when the session in the xmpp connection is cleared. The above fix is not sufficient since there's a concurrency issue wherein XmppSession::WriteReady could try to access the connection the connection is trying to clear the back pointer in the session. Fix is to make XmppSession::WriteReady enqueue sessions to a work queue and process the work queue in context of bgp::Config task. Add XmppConnectionManager class so that this can be done for both XmppClient and XmppServer. Other common functionality can be moved into this class in future. Remove dead code for XmppStream. Change-Id: Id9ea3311e6618a0afc20058c3571330a1627fcec Closes-Bug: 1463122
- Loading branch information
Nischal Sheth
committed
Jun 9, 2015
1 parent
410efef
commit dffb019
Showing
15 changed files
with
246 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. | ||
*/ | ||
|
||
#include "xmpp/xmpp_connection_manager.h" | ||
|
||
#include <boost/bind.hpp> | ||
|
||
#include "base/lifetime.h" | ||
#include "base/task_annotations.h" | ||
#include "xmpp/xmpp_session.h" | ||
|
||
// | ||
// Constructor for XmppConnectionManager. | ||
// | ||
XmppConnectionManager::XmppConnectionManager(EventManager *evm, | ||
boost::asio::ssl::context::method m, | ||
bool ssl_enabled, bool ssl_handshake_delayed) | ||
: SslServer(evm, m, ssl_enabled, ssl_handshake_delayed), | ||
session_queue_(TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), | ||
0, boost::bind(&XmppConnectionManager::DequeueSession, this, _1)) { | ||
} | ||
|
||
// | ||
// Shutdown the XmppConnectionManager. | ||
// | ||
// Register an exit callback to the session WorkQueue so that we can retry | ||
// deletion when the WorkQueue becomes empty. | ||
// | ||
void XmppConnectionManager::Shutdown() { | ||
TcpServer::Shutdown(); | ||
session_queue_.SetExitCallback( | ||
boost::bind(&XmppConnectionManager::WorkQueueExitCallback, this, _1)); | ||
session_queue_.Shutdown(); | ||
} | ||
|
||
// | ||
// Concurrency: called in the context of io thread. | ||
// | ||
// Add a XmppSession to the queue of write ready sessions. | ||
// Take a reference to make sure that XmppSession doesn't get deleted before | ||
// it's processed. | ||
// | ||
void XmppConnectionManager::EnqueueSession(XmppSession *session) { | ||
if (deleter()->IsDeleted()) | ||
return; | ||
session_queue_.Enqueue(TcpSessionPtr(session)); | ||
} | ||
|
||
// | ||
// Concurrency: called in the context of bgp::Config task. | ||
// | ||
// Handler for XmppSessions that are dequeued from the session WorkQueue. | ||
// | ||
// The Xmpp[Client|Server] doesn't get destroyed if the WorkQueue is non-empty. | ||
// | ||
bool XmppConnectionManager::DequeueSession(TcpSessionPtr tcp_session) { | ||
CHECK_CONCURRENCY("bgp::Config"); | ||
XmppSession *session = static_cast<XmppSession *>(tcp_session.get()); | ||
session->ProcessWriteReady(); | ||
return true; | ||
} | ||
|
||
// | ||
// Exit callback for the session WorkQueue. | ||
// | ||
void XmppConnectionManager::WorkQueueExitCallback(bool done) { | ||
CHECK_CONCURRENCY("bgp::Config"); | ||
if (!deleter()->IsDeleted()) | ||
return; | ||
deleter()->RetryDelete(); | ||
} | ||
|
||
// | ||
// Return size of WorkQueue of write ready XmppSessions. | ||
// | ||
size_t XmppConnectionManager::GetSessionQueueSize() const { | ||
CHECK_CONCURRENCY("bgp::Config"); | ||
return session_queue_.Length(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. | ||
*/ | ||
|
||
#ifndef XMPP_XMPP_CONNECTION_MANAGER_H | ||
#define XMPP_XMPP_CONNECTION_MANAGER_H | ||
|
||
#include "base/queue_task.h" | ||
#include "io/ssl_server.h" | ||
|
||
class LifetimeActor; | ||
class XmppSession; | ||
|
||
// | ||
// Common class to represent XmppClient and XmppServer | ||
// | ||
class XmppConnectionManager : public SslServer { | ||
public: | ||
XmppConnectionManager(EventManager *evm, | ||
boost::asio::ssl::context::method m, | ||
bool ssl_enabled, bool ssl_handshake_delayed); | ||
|
||
void Shutdown(); | ||
void EnqueueSession(XmppSession *session); | ||
size_t GetSessionQueueSize() const; | ||
virtual LifetimeActor *deleter() = 0; | ||
|
||
private: | ||
bool DequeueSession(TcpSessionPtr tcp_session); | ||
void WorkQueueExitCallback(bool done); | ||
|
||
WorkQueue<TcpSessionPtr> session_queue_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(XmppConnectionManager); | ||
}; | ||
|
||
#endif // XMPP_XMPP_CONNECTION_MANAGER_H |
Oops, something went wrong.