Skip to content

Commit

Permalink
Use std::exception_ptr to transmit and store exceptions (#1768)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored Feb 8, 2024
1 parent 3fa22ce commit 4cd7951
Show file tree
Hide file tree
Showing 78 changed files with 950 additions and 922 deletions.
2 changes: 1 addition & 1 deletion cpp/include/Glacier2/SessionHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class GLACIER2_API SessionCallback
* @param session The corresponding session helper.
* @param ex The exception that caused the failure.
*/
virtual void connectFailed(const SessionHelperPtr& session, const Ice::Exception& ex) = 0;
virtual void connectFailed(const SessionHelperPtr& session, std::exception_ptr ex) = 0;
};
using SessionCallbackPtr = std::shared_ptr<SessionCallback>;

Expand Down
4 changes: 2 additions & 2 deletions cpp/include/Ice/Incoming.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ICE_API IncomingBase : private IceUtil::noncopyable
void setMarshaledResult(const Ice::MarshaledResult&);

void response(bool);
void exception(const std::exception&, bool);
void exception(std::exception_ptr, bool);
void exception(const std::string&, bool);

protected:
Expand All @@ -84,7 +84,7 @@ class ICE_API IncomingBase : private IceUtil::noncopyable

bool servantLocatorFinished(bool);

void handleException(const std::exception&, bool);
void handleException(std::exception_ptr, bool);
void handleException(const std::string&, bool);

Ice::Current _current;
Expand Down
2 changes: 2 additions & 0 deletions cpp/include/Ice/ObserverHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace IceInternal
{

std::string getExceptionId(std::exception_ptr);

template<typename T = Ice::Instrumentation::Observer> class ObserverHelperT
{
public:
Expand Down
41 changes: 17 additions & 24 deletions cpp/include/Ice/OutgoingAsync.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class ICE_API OutgoingAsyncCompletionCallback
protected:

virtual bool handleSent(bool, bool) = 0;
virtual bool handleException(const Ice::Exception&) = 0;
virtual bool handleException(std::exception_ptr) = 0;
virtual bool handleResponse(bool) = 0;

virtual void handleInvokeSent(bool, OutgoingAsyncBase*) const = 0;
virtual void handleInvokeException(const Ice::Exception&, OutgoingAsyncBase*) const = 0;
virtual void handleInvokeException(std::exception_ptr, OutgoingAsyncBase*) const = 0;
virtual void handleInvokeResponse(bool, OutgoingAsyncBase*) const = 0;
};

Expand All @@ -53,7 +53,7 @@ class ICE_API OutgoingAsyncBase : public virtual OutgoingAsyncCompletionCallback
public:

virtual bool sent();
virtual bool exception(const Ice::Exception&);
virtual bool exception(std::exception_ptr);
virtual bool response();

void invokeSentAsync();
Expand Down Expand Up @@ -94,10 +94,10 @@ class ICE_API OutgoingAsyncBase : public virtual OutgoingAsyncCompletionCallback
OutgoingAsyncBase(const InstancePtr&);

bool sentImpl(bool);
bool exceptionImpl(const Ice::Exception&);
bool exceptionImpl(std::exception_ptr);
bool responseImpl(bool, bool);

void cancel(const Ice::LocalException&);
void cancel(std::exception_ptr);
void checkCanceled();

void warning(const std::exception&) const;
Expand All @@ -121,8 +121,8 @@ class ICE_API OutgoingAsyncBase : public virtual OutgoingAsyncCompletionCallback
std::mutex _m;
using Lock = std::lock_guard<std::mutex>;

std::unique_ptr<Ice::Exception> _ex;
std::unique_ptr<Ice::LocalException> _cancellationException;
std::exception_ptr _ex;
std::exception_ptr _cancellationException;

InvocationObserver _observer;
ObserverHelperT<Ice::Instrumentation::ChildInvocationObserver> _childObserver;
Expand Down Expand Up @@ -150,12 +150,12 @@ class ICE_API ProxyOutgoingAsyncBase : public OutgoingAsyncBase,
virtual AsyncStatus invokeRemote(const Ice::ConnectionIPtr&, bool, bool) = 0;
virtual AsyncStatus invokeCollocated(CollocatedRequestHandler*) = 0;

virtual bool exception(const Ice::Exception&);
virtual bool exception(std::exception_ptr);
virtual void cancelable(const CancellationHandlerPtr&);

void retryException(const Ice::Exception&);
void retryException();
void retry();
void abort(const Ice::Exception&);
void abort(std::exception_ptr);

std::shared_ptr<ProxyOutgoingAsyncBase> shared_from_this()
{
Expand All @@ -169,7 +169,7 @@ class ICE_API ProxyOutgoingAsyncBase : public OutgoingAsyncBase,

void invokeImpl(bool);
bool sentImpl(bool);
bool exceptionImpl(const Ice::Exception&);
bool exceptionImpl(std::exception_ptr);
bool responseImpl(bool, bool);

virtual void runTimerTask();
Expand Down Expand Up @@ -201,7 +201,7 @@ class ICE_API OutgoingAsync : public ProxyOutgoingAsyncBase
virtual AsyncStatus invokeRemote(const Ice::ConnectionIPtr&, bool, bool);
virtual AsyncStatus invokeCollocated(CollocatedRequestHandler*);

void abort(const Ice::Exception&);
void abort(std::exception_ptr);
void invoke(const std::string&);
void invoke(const std::string&, Ice::OperationMode, Ice::FormatType, const Ice::Context&,
std::function<void(Ice::OutputStream*)>);
Expand Down Expand Up @@ -256,11 +256,11 @@ class ICE_API LambdaInvoke : public virtual OutgoingAsyncCompletionCallback
protected:

virtual bool handleSent(bool, bool) override;
virtual bool handleException(const Ice::Exception&) override;
virtual bool handleException(std::exception_ptr) override;
virtual bool handleResponse(bool) override;

virtual void handleInvokeSent(bool, OutgoingAsyncBase*) const override;
virtual void handleInvokeException(const Ice::Exception&, OutgoingAsyncBase*) const override;
virtual void handleInvokeException(std::exception_ptr, OutgoingAsyncBase*) const override;
virtual void handleInvokeResponse(bool, OutgoingAsyncBase*) const override;

std::function<void(::std::exception_ptr)> _exception;
Expand Down Expand Up @@ -291,16 +291,9 @@ class PromiseInvoke : public virtual OutgoingAsyncCompletionCallback
return false;
}

virtual bool handleException(const Ice::Exception& ex) override
virtual bool handleException(std::exception_ptr ex) override
{
try
{
ex.ice_throw();
}
catch(const Ice::Exception&)
{
_promise.set_exception(std::current_exception());
}
_promise.set_exception(ex);
return false;
}

Expand All @@ -315,7 +308,7 @@ class PromiseInvoke : public virtual OutgoingAsyncCompletionCallback
assert(false);
}

virtual void handleInvokeException(const Ice::Exception&, OutgoingAsyncBase*) const override
virtual void handleInvokeException(std::exception_ptr, OutgoingAsyncBase*) const override
{
assert(false);
}
Expand Down
6 changes: 3 additions & 3 deletions cpp/include/Ice/Proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ class InvokeOutgoingAsyncT : public OutgoingAsync
}
OutgoingAsync::invoke(operation);
}
catch(const Ice::Exception& ex)
catch (const std::exception&)
{
abort(ex);
abort(std::current_exception());
}
}

Expand Down Expand Up @@ -1174,7 +1174,7 @@ class ICE_API ObjectPrx : public Proxy<ObjectPrx>, public ::std::enable_shared_f

const ::IceInternal::ReferencePtr& _getReference() const { return _reference; }

int _handleException(const ::Ice::Exception&, const ::IceInternal::RequestHandlerPtr&, ::Ice::OperationMode,
int _handleException(std::exception_ptr, const ::IceInternal::RequestHandlerPtr&, ::Ice::OperationMode,
bool, int&);

void _checkTwowayOnly(const ::std::string&) const;
Expand Down
21 changes: 10 additions & 11 deletions cpp/src/Glacier2Lib/SessionHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,25 +476,24 @@ class ConnectFailed : public Ice::DispatcherCall
public:

ConnectFailed(const Glacier2::SessionCallbackPtr& callback, const Glacier2::SessionHelperPtr& session,
const Ice::Exception& ex) :
std::exception_ptr ex) :
_callback(callback),
_session(session)
{
_ex = ex.ice_clone();
_ex = ex;
}

virtual void
run()
{
const Ice::Exception* ex(_ex.get());
_callback->connectFailed(_session, *ex);
_callback->connectFailed(_session, _ex);
}

private:

const Glacier2::SessionCallbackPtr _callback;
const Glacier2::SessionHelperPtr _session;
unique_ptr<Ice::Exception> _ex;
std::exception_ptr _ex;
};

class CreatedCommunicator : public Ice::DispatcherCall
Expand Down Expand Up @@ -544,13 +543,13 @@ class ConnectThread : public IceUtil::Thread
communicator = Ice::initialize(_session->_initData);
_session->_communicator = communicator;
}
catch(const Ice::LocalException& ex)
catch(const Ice::LocalException&)
{
{
lock_guard lock(_session->_mutex);
_session->_destroy = true;
}
_session->dispatchCallback(new ConnectFailed(_callback, _session, ex), nullptr);
_session->dispatchCallback(new ConnectFailed(_callback, _session, current_exception()), nullptr);
return;
}

Expand All @@ -564,9 +563,9 @@ class ConnectThread : public IceUtil::Thread
finder = Ice::uncheckedCast<Ice::RouterFinderPrx>(communicator->stringToProxy(_finder));
communicator->setDefaultRouter(finder->getRouter());
}
catch(const Ice::CommunicatorDestroyedException& ex)
catch(const Ice::CommunicatorDestroyedException&)
{
_session->dispatchCallback(new ConnectFailed(_callback, _session, ex), 0);
_session->dispatchCallback(new ConnectFailed(_callback, _session, current_exception()), 0);
return;
}
catch(const Ice::Exception&)
Expand All @@ -586,7 +585,7 @@ class ConnectThread : public IceUtil::Thread
Glacier2::SessionPrxPtr session = _factory->connect(routerPrx);
_session->connected(routerPrx, session);
}
catch(const Ice::Exception& ex)
catch(const Ice::Exception&)
{
try
{
Expand All @@ -596,7 +595,7 @@ class ConnectThread : public IceUtil::Thread
{
}

_session->dispatchCallback(new ConnectFailed(_callback, _session, ex), 0);
_session->dispatchCallback(new ConnectFailed(_callback, _session, current_exception()), 0);
}
_session = 0;
}
Expand Down
6 changes: 3 additions & 3 deletions cpp/src/Ice/BatchRequestQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ BatchRequestQueue::prepareBatchRequest(OutputStream* os)
unique_lock lock(_mutex);
if(_exception)
{
_exception->ice_throw();
rethrow_exception(_exception);
}
_conditionVariable.wait(lock, [this] { return !_batchStreamInUse; });

Expand Down Expand Up @@ -203,10 +203,10 @@ BatchRequestQueue::swap(OutputStream* os, bool& compress)
}

void
BatchRequestQueue::destroy(const Ice::LocalException& ex)
BatchRequestQueue::destroy(std::exception_ptr ex)
{
lock_guard lock(_mutex);
_exception = ex.ice_clone();
_exception = ex;
}

bool
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/Ice/BatchRequestQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BatchRequestQueue : public IceUtil::Shared

int swap(Ice::OutputStream*, bool&);

void destroy(const Ice::LocalException&);
void destroy(std::exception_ptr);
bool isEmpty();

void enqueueBatchRequest(const Ice::ObjectPrxPtr&);
Expand All @@ -44,7 +44,7 @@ class BatchRequestQueue : public IceUtil::Shared
bool _batchCompress;
int _batchRequestNum;
size_t _batchMarker;
std::unique_ptr<Ice::LocalException> _exception;
std::exception_ptr _exception;
size_t _maxSize;

std::mutex _mutex;
Expand Down
16 changes: 8 additions & 8 deletions cpp/src/Ice/CollocatedRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ CollocatedRequestHandler::sendAsyncRequest(const ProxyOutgoingAsyncBasePtr& outA
}

void
CollocatedRequestHandler::asyncRequestCanceled(const OutgoingAsyncBasePtr& outAsync, const LocalException& ex)
CollocatedRequestHandler::asyncRequestCanceled(const OutgoingAsyncBasePtr& outAsync, exception_ptr ex)
{
lock_guard<mutex> lock(_mutex);

Expand Down Expand Up @@ -257,15 +257,15 @@ CollocatedRequestHandler::sendNoResponse()
}

bool
CollocatedRequestHandler::systemException(Int requestId, const SystemException& ex, bool amd)
CollocatedRequestHandler::systemException(Int requestId, exception_ptr ex, bool amd)
{
handleException(requestId, ex, amd);
_adapter->decDirectCount();
return true;
}

void
CollocatedRequestHandler::invokeException(Int requestId, const LocalException& ex, int /*invokeNum*/, bool amd)
CollocatedRequestHandler::invokeException(Int requestId, exception_ptr ex, int /*invokeNum*/, bool amd)
{
handleException(requestId, ex, amd);
_adapter->decDirectCount();
Expand Down Expand Up @@ -346,9 +346,9 @@ CollocatedRequestHandler::invokeAll(OutputStream* os, Int requestId, Int batchRe
{
_adapter->incDirectCount();
}
catch(const ObjectAdapterDeactivatedException& ex)
catch(const ObjectAdapterDeactivatedException&)
{
handleException(requestId, ex, false);
handleException(requestId, current_exception(), false);
break;
}

Expand All @@ -357,16 +357,16 @@ CollocatedRequestHandler::invokeAll(OutputStream* os, Int requestId, Int batchRe
--invokeNum;
}
}
catch(const LocalException& ex)
catch(const LocalException&)
{
invokeException(requestId, ex, invokeNum, false); // Fatal invocation exception
invokeException(requestId, current_exception(), invokeNum, false); // Fatal invocation exception
}

_adapter->decDirectCount();
}

void
CollocatedRequestHandler::handleException(int requestId, const Exception& ex, bool amd)
CollocatedRequestHandler::handleException(int requestId, std::exception_ptr ex, bool amd)
{
if(requestId == 0)
{
Expand Down
8 changes: 4 additions & 4 deletions cpp/src/Ice/CollocatedRequestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class CollocatedRequestHandler : public RequestHandler, public ResponseHandler

virtual AsyncStatus sendAsyncRequest(const ProxyOutgoingAsyncBasePtr&);

virtual void asyncRequestCanceled(const OutgoingAsyncBasePtr&, const Ice::LocalException&);
virtual void asyncRequestCanceled(const OutgoingAsyncBasePtr&, std::exception_ptr);

virtual void sendResponse(Ice::Int, Ice::OutputStream*, Ice::Byte, bool);
virtual void sendNoResponse();
virtual bool systemException(Ice::Int, const Ice::SystemException&, bool);
virtual void invokeException(Ice::Int, const Ice::LocalException&, int, bool);
virtual bool systemException(Ice::Int, std::exception_ptr, bool);
virtual void invokeException(Ice::Int, std::exception_ptr, int, bool);

const ReferencePtr& getReference() const { return _reference; } // Inlined for performances.

Expand All @@ -64,7 +64,7 @@ class CollocatedRequestHandler : public RequestHandler, public ResponseHandler

private:

void handleException(Ice::Int, const Ice::Exception&, bool);
void handleException(Ice::Int, std::exception_ptr, bool);

const std::shared_ptr<Ice::ObjectAdapterI> _adapter;
const bool _dispatcher;
Expand Down
Loading

0 comments on commit 4cd7951

Please sign in to comment.