Skip to content

Commit

Permalink
Correct lifetime of outstanding open-file request.
Browse files Browse the repository at this point in the history
Previously, the presence of a pointer in m_file indicated an open-
file request was outstanding.  However, after the file was opened,
the unique_ptr was moved to the Source object (which does a synchronous
network query in its constructor).  Hence, it appeared there was
no outstanding file-open and a second one could start.

Now, we keep a separate boolean flag that is only set to false when
the HandleResponse method exits.
  • Loading branch information
bbockelm committed Jan 29, 2016
1 parent 119385e commit 2ef8f99
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 10 additions & 1 deletion Utilities/XrdAdaptor/src/XrdRequestManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,9 @@ XrdAdaptor::RequestManager::OpenHandler::~OpenHandler()
void
XrdAdaptor::RequestManager::OpenHandler::HandleResponseWithHosts(XrdCl::XRootDStatus *status_ptr, XrdCl::AnyObject *, XrdCl::HostList *hostList_ptr)
{
// Make sure that we set m_outstanding_open to false on exit from this function.
std::unique_ptr<char, std::function<void(char*)>> outstanding_guard(nullptr, [&](char*){m_outstanding_open=false;});

std::shared_ptr<Source> source;
std::unique_ptr<XrdCl::XRootDStatus> status(status_ptr);
std::unique_ptr<XrdCl::HostList> hostList(hostList_ptr);
Expand Down Expand Up @@ -1105,7 +1108,7 @@ XrdAdaptor::RequestManager::OpenHandler::open()
// and make a call into xrootd (when it invokes m_file.reset()). Hence, our callback
// holds our mutex and attempts to grab an Xrootd mutex; RequestManager::requestFailure holds
// an Xrootd mutex and tries to hold m_mutex. This is a classic deadlock.
if (m_file.get())
if (m_outstanding_open)
{
return m_shared_future;
}
Expand All @@ -1118,6 +1121,11 @@ XrdAdaptor::RequestManager::OpenHandler::open()
std::string new_name = manager.m_name + ((manager.m_name.find("?") == manager.m_name.npos) ? "?" : "&") + opaque;
edm::LogVerbatim("XrdAdaptorInternal") << "Trying to open URL: " << new_name;
m_file.reset(new XrdCl::File());
m_outstanding_open = true;

// Always make sure we release m_file and set m_outstanding_open to false on error.
std::unique_ptr<char, std::function<void(char*)>> exit_guard(nullptr, [&](char*){m_outstanding_open = false; m_file.reset();});

XrdCl::XRootDStatus status;
if (!(status = m_file->Open(new_name, manager.m_flags, manager.m_perms, this)).IsOK())
{
Expand All @@ -1131,6 +1139,7 @@ XrdAdaptor::RequestManager::OpenHandler::open()
manager.addConnections(ex);
throw ex;
}
exit_guard.release();
// Have a strong self-reference for as long as the callback is in-progress.
m_self = self_ptr;
return m_shared_future;
Expand Down
5 changes: 4 additions & 1 deletion Utilities/XrdAdaptor/src/XrdRequestManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ class RequestManager : boost::noncopyable {
OpenHandler(std::weak_ptr<RequestManager> manager);
std::shared_future<std::shared_ptr<Source> > m_shared_future;
std::promise<std::shared_ptr<Source> > m_promise;
// When this is not null, there is a file-open in process
// Set to true only when there is an outstanding open request; not
// protected by m_mutex, so the caller is required to know it is in a
// thread-safe context.
std::atomic<bool> m_outstanding_open {false};
// Can only be touched when m_mutex is held.
std::unique_ptr<XrdCl::File> m_file;
std::recursive_mutex m_mutex;
Expand Down

0 comments on commit 2ef8f99

Please sign in to comment.