Skip to content

Commit

Permalink
HPCC-26873 Use ISocket::shutdown when terminating read sockets
Browse files Browse the repository at this point in the history
Signed-off-by: Gavin Halliday <[email protected]>
  • Loading branch information
ghalliday committed Dec 17, 2021
1 parent ba99f88 commit 70bbcbb
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 91 deletions.
3 changes: 1 addition & 2 deletions common/thorhelper/persistent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ class CPersistentHandler : implements IPersistentHandler, implements ISocketSele
if(m_doNotReuseList.getValue(epstr.str()) != nullptr)
{
PERSILOG(PersistentLogLevel::PLogNormal, "PERSISTENT: socket %d's target endpoint %s is in DoNotReuseList, will not add it.", sock->OShandle(), epstr.str());
sock->shutdown();
sock->close();
shutdownAndCloseNoThrow(sock);
return;
}
}
Expand Down
3 changes: 1 addition & 2 deletions esp/platform/espplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ int CEspServiceThread::run()

DBGLOG("Closing Socket(%d)", m_socket->OShandle());

m_socket->shutdown();
m_socket->close();
shutdownAndCloseNoThrow(m_socket);

Release();
return 0;
Expand Down
12 changes: 4 additions & 8 deletions esp/test/httptest/httptest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,10 +886,8 @@ int COneServerHttpProxyThread::start()
if(httptest_tracelevel > 5)
fprintf(m_ofile, ">>sent the response back to %s:%d:\n", peername, port);

socket2->shutdown();
socket2->close();
m_client->shutdown();
m_client->close();
shutdownAndCloseNoThrow(socket2);
shutdownAndCloseNoThrow(m_client);
}
catch(IException *excpt)
{
Expand Down Expand Up @@ -1092,10 +1090,8 @@ int CHttpProxyThread::run()
//m_client->write(respbuf.str(), respbuf.length());
fflush(m_ofile);

m_remotesocket->shutdown();
m_remotesocket->close();
m_client->shutdown();
m_client->close();
shutdownAndCloseNoThrow(m_remotesocket);
shutdownAndCloseNoThrow(m_client);
}
}
catch(IException *excpt)
Expand Down
3 changes: 1 addition & 2 deletions esp/tools/soapplus/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1826,8 +1826,7 @@ int HttpClient::sendRequest(StringBuffer& req, IFileIO* request_output, IFileIO*

if(!isPersist || iter >= numReq - 1)
{
socket->shutdown();
socket->close();
shutdownAndCloseNoThrow(socket);
}

unsigned end1 = msTick();
Expand Down
19 changes: 1 addition & 18 deletions fs/dafsclient/rmtclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,7 @@ static ISocket *dummyReadWrite::timeoutreadsock = null; // used to trigger

void cleanupDaFsSocket(ISocket *sock)
{
if (!sock)
return;
try
{
sock->shutdown();
}
catch (IException *e)
{
e->Release();
}
try
{
sock->close();
}
catch (IException *e)
{
e->Release();
}
shutdownAndCloseNoThrow(sock);
}


Expand Down
19 changes: 1 addition & 18 deletions roxie/ccd/ccdprotocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,24 +274,7 @@ class ProtocolSocketListener : public ProtocolListener

virtual void cleanupSocket(ISocket *sock)
{
if (!sock)
return;
try
{
sock->shutdown();
}
catch (IException *e)
{
e->Release();
}
try
{
sock->close();
}
catch (IException *e)
{
e->Release();
}
shutdownAndCloseNoThrow(sock);
}

virtual int run()
Expand Down
2 changes: 1 addition & 1 deletion roxie/ccd/ccdqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2824,7 +2824,7 @@ class RoxieSocketQueueManager : public RoxieReceiverBase
if (running)
{
running = false;
multicastSocket->close();
shutdownAndCloseNoThrow(multicastSocket);
}
RoxieReceiverBase::stop();
}
Expand Down
11 changes: 4 additions & 7 deletions roxie/udplib/udptrr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class CReceiveManager : implements IReceiveManager, public CInterface
{
if (flowSocket)
{
flowSocket->close();
shutdownAndCloseNoThrow(flowSocket);
flowSocket->Release();
}
}
Expand Down Expand Up @@ -438,8 +438,7 @@ class CReceiveManager : implements IReceiveManager, public CInterface
~receive_receive_flow()
{
running = false;
if (flow_socket)
flow_socket->close();
shutdownAndCloseNoThrow(flow_socket);
join();
}

Expand Down Expand Up @@ -646,10 +645,8 @@ class CReceiveManager : implements IReceiveManager, public CInterface
DBGLOG("Total data packets seen = %u OOO(%u) Requests(%u) Permits(%u)", dataPacketsReceived.load(), packetsOOO.load(), flowRequestsReceived.load(), flowRequestsSent.load());

running = false;
if (receive_socket)
receive_socket->close();
if (selfFlowSocket)
selfFlowSocket->close();
shutdownAndCloseNoThrow(receive_socket);
shutdownAndCloseNoThrow(selfFlowSocket);
join();
::Release(receive_socket);
::Release(selfFlowSocket);
Expand Down
3 changes: 1 addition & 2 deletions roxie/udplib/udptrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,7 @@ class CSendManager : implements ISendManager, public CInterface
~send_receive_flow()
{
running = false;
if (flow_socket)
flow_socket->shutdownNoThrow();
shutdownAndCloseNoThrow(flow_socket);
join();
}

Expand Down
2 changes: 1 addition & 1 deletion system/hrpc/hrpcsock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ class HRPCsockettransport: implements IHRPCtransport, public CInterface
CriticalBlock critblock(critsect);
if (datasock ) {
try {
datasock->shutdown();
datasock->shutdownNoThrow();
datasock->close();
}
catch (IJSOCK_Exception *e)
Expand Down
26 changes: 17 additions & 9 deletions system/jlib/jsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5545,15 +5545,7 @@ CSingletonSocketConnection::CSingletonSocketConnection(SocketEndpoint &_ep)

CSingletonSocketConnection::~CSingletonSocketConnection()
{
try {
if (sock)
sock->close();
}
catch (IException *e) {
if (e->errorCode()!=JSOCKERR_graceful_close)
EXCLOG(e,"CSingletonSocketConnection close");
e->Release();
}
shutdownAndCloseNoThrow(sock);
}

void CSingletonSocketConnection::set_keep_alive(bool keepalive)
Expand Down Expand Up @@ -7106,5 +7098,21 @@ IAllowListHandler *createAllowListHandler(AllowListPopulateFunction populateFunc
return new CAllowListHandler(populateFunc, roleFormatFunc);
}

extern jlib_decl void shutdownAndCloseNoThrow(ISocket * optSocket)
{
if (!optSocket)
return;

optSocket->shutdownNoThrow();
try
{
optSocket->close();
}
catch (IException * e)
{
e->Release();
}
}

static_assert(sizeof(IpAddress) == 16, "check size of IpAddress");
static_assert(sizeof(SocketEndpoint) == 20, "check size of SocketEndpoint");
2 changes: 2 additions & 0 deletions system/jlib/jsocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,5 +730,7 @@ class jlib_decl CSingletonSocketConnection: implements IConversation, public CIn
virtual void cancel();
};

extern jlib_decl void shutdownAndCloseNoThrow(ISocket * optSocket); // Safely shutdown and close a socket without throwing an exception.

#endif

2 changes: 1 addition & 1 deletion system/mp/mpcomm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2081,7 +2081,7 @@ void CMPConnectThread::checkSelfDestruct(void *p,size32_t sz)
PROGLOG("MP Self destruct invoked");
try {
if (listensock) {
listensock->close();
shutdownAndCloseNoThrow(listensock);
listensock->Release();
listensock=NULL;
}
Expand Down
10 changes: 1 addition & 9 deletions system/security/securesocket/securesocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2059,15 +2059,7 @@ class CSingletonSecureSocketConnection: public CSingletonSocketConnection

virtual ~CSingletonSecureSocketConnection()
{
try {
if (sock)
sock->close();
}
catch (IException *e) {
if (e->errorCode()!=JSOCKERR_graceful_close)
EXCLOG(e,"CSingletonSocketConnection close");
e->Release();
}
shutdownAndCloseNoThrow(sock);
}

bool connect(unsigned timeoutms) override
Expand Down
12 changes: 1 addition & 11 deletions thorlcr/msort/tsorts1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,7 @@ protected: friend class CSortMerge;
}
e->Release();
}
try {
server->close();
}
catch (IJSOCK_Exception *e)
{
if (e->errorCode()!=JSOCKERR_cancel_accept) {
PrintExceptionLog(e,"CSortTransferServerThread closing");
// Ignore for now
}
e->Release();
}
shutdownAndCloseNoThrow(server);
subjoin();
DBGLOG("CSortTransferServerThread finished");
return 0;
Expand Down

0 comments on commit 70bbcbb

Please sign in to comment.