From 5e90d67bdee2966fe3257458c2bfeb549533b5f5 Mon Sep 17 00:00:00 2001 From: David Smith Date: Fri, 24 Feb 2023 11:27:35 +0100 Subject: [PATCH] [XrdCl] Check for closed connection between handling of read and write events --- src/XrdCl/XrdClAsyncSocketHandler.cc | 17 +++++++++++++++++ src/XrdCl/XrdClAsyncSocketHandler.hh | 2 ++ 2 files changed, 19 insertions(+) diff --git a/src/XrdCl/XrdClAsyncSocketHandler.cc b/src/XrdCl/XrdClAsyncSocketHandler.cc index c416cebb367..a028bb1cd4e 100644 --- a/src/XrdCl/XrdClAsyncSocketHandler.cc +++ b/src/XrdCl/XrdClAsyncSocketHandler.cc @@ -46,6 +46,7 @@ namespace XrdCl pStreamName( ToStreamName( url, subStreamNum ) ), pSocket( new Socket() ), pHandShakeDone( false ), + pCloseCount( 0 ), pConnectionStarted( 0 ), pConnectionTimeout( 0 ), pHSWaitStarted( 0 ), @@ -190,6 +191,7 @@ namespace XrdCl pPoller->RemoveSocket( pSocket ); pSocket->Close(); + pCloseCount++; return XRootDStatus(); } @@ -213,6 +215,12 @@ namespace XrdCl //-------------------------------------------------------------------------- type = pSocket->MapEvent( type ); + //-------------------------------------------------------------------------- + // In case we'll be handling both read and write events be prepared to + // detect a close between the handling of each. + //-------------------------------------------------------------------------- + const uint32_t closeCount = pCloseCount; + //-------------------------------------------------------------------------- // Read event //-------------------------------------------------------------------------- @@ -241,6 +249,15 @@ namespace XrdCl OnTimeoutWhileHandshaking(); } + //-------------------------------------------------------------------------- + // We check to see if any of the read event handlers above may have caused + // us to be closed. If so, we may have been re-opened, added to another + // poller and now be concurrently handling requests for another connection. + // Discontinue processing if a close is detected. + //-------------------------------------------------------------------------- + if( closeCount != pCloseCount ) + return; + //-------------------------------------------------------------------------- // Write event //-------------------------------------------------------------------------- diff --git a/src/XrdCl/XrdClAsyncSocketHandler.hh b/src/XrdCl/XrdClAsyncSocketHandler.hh index f696e754af9..91c31f46cf3 100644 --- a/src/XrdCl/XrdClAsyncSocketHandler.hh +++ b/src/XrdCl/XrdClAsyncSocketHandler.hh @@ -30,6 +30,7 @@ #include "XrdCl/XrdClAsyncHSReader.hh" #include "XrdCl/XrdClAsyncMsgWriter.hh" #include "XrdCl/XrdClAsyncHSWriter.hh" +#include "XrdSys/XrdSysRAtomic.hh" namespace XrdCl { @@ -264,6 +265,7 @@ namespace XrdCl std::unique_ptr pHandShakeData; bool pHandShakeDone; uint16_t pTimeoutResolution; + RAtomic_uint32_t pCloseCount; time_t pConnectionStarted; time_t pConnectionTimeout; time_t pLastActivity;