Overview
Features
Download
Documentation
Community
Add-Ons & Services

Catching NoThreadAvailableException in ActiveStarter?

Please post support and help requests here.

Catching NoThreadAvailableException in ActiveStarter?

Postby bakercp » 11 Feb 2013, 04:42

Hi there,

The default ActiveStarter static start method does not catch NoThreadAvailableExceptions from the threadpool.

See:
https://github.com/pocoproject/poco/blo ... rter.h#L59

Ideally, I would like to catch that error and then call the error method on the associated ActiveResult (passing in the NoThreadAvailableExceptions exception). Intra-thread Exceptions are caught and appropriately set from WITHIN the run method of ActiveRunnable, allowing the ActiveResult to fail by calling its error(*) method:

https://github.com/pocoproject/poco/blo ... able.h#L80

But I'm looking for a way to call error(*) on an ActiveResult BEFORE its run() method is called (i.e. due to a NoThreadAvailableException or perhaps another arbitrary exception).

https://github.com/pocoproject/poco/blo ... hod.h#L114

I can think of several work arounds, including checking the threadpool's thread availability before calling the ActiveMethod (risky in a multi-threaded environment), or overriding the ActiveMethod::operator() method, or even writing a custom StarterType that casts the ActiveRunnableBase::Ptr to the derived ActiveRunnable type and then accesses it's ActiveResult, but that all seems a little bit sloppy, when the rest of the Active framework seems so tidy.

It seems like the tidiest way would be for the StarterClass to throw an exception that the the ActiveMethod could catch and pass to the ActiveResult -- something like:

Code: Select all
   
ActiveResultType operator () (const ArgType& arg)
      /// Invokes the ActiveMethod.
   {
      ActiveResultType result(new ActiveResultHolder<ResultType>());
      ActiveRunnableBase::Ptr pRunnable(new ActiveRunnableType(_pOwner, _method, arg, result));
      try {
                    StarterType::start(_pOwner, pRunnable);
                } catch(Exception& exc) {
                     result.error(exc);
                      return result;
                } catch(std::exception& e) {
                      result.error(e.what());
                      return result;
                } catch( ... ) {
                      result.error("unknown exception");
                      return result;
                }
      return result;
   }



Anyway, advice for how I might catch exceptions from WITHIN the StarterType static method class and passing them to the ActiveResult are appreciated.

Thanks!
Christopher
bakercp
 
Posts: 4
Joined: 17 Jan 2013, 20:57

Re: Catching NoThreadAvailableException in ActiveStarter?

Postby bakercp » 11 Feb 2013, 05:15

By the way, here is my current best solution (without a change to the core). It basically overrides the ActiveMethod operator(). There is no version for the NoArg ActiveMethod:

Code: Select all
template <class ResultType, class ArgType, class OwnerType, class StarterType = ActiveStarter<OwnerType> >
class ActiveMethodWithExceptionHandling : public ActiveMethod<ResultType,ArgType,OwnerType,StarterType> {
public:
    typedef ResultType (OwnerType::*Callback)(const ArgType&);
   typedef ActiveResult<ResultType> ActiveResultType;
   typedef ActiveRunnable<ResultType, ArgType, OwnerType> ActiveRunnableType;
   
    ActiveMethodWithExceptionHandling(OwnerType* pOwner, Callback method) :
    ActiveMethod<ResultType,ArgType,OwnerType,StarterType>(pOwner,method)
    { }

    ActiveResultType operator () (const ArgType& arg)
    /// Invokes the ActiveMethod.
   {
        try {
            return ActiveMethod<ResultType,ArgType,OwnerType,StarterType>::operator()(arg);
        } catch(Exception& exc) {
            ActiveResultType result(new ActiveResultHolder<ResultType>());
            result.error(exc);
            return result;
        } catch(std::exception& e) {
            ActiveResultType result(new ActiveResultHolder<ResultType>());
            result.error(e.what());
            return result;
        } catch( ... ) {
            ActiveResultType result(new ActiveResultHolder<ResultType>());
            result.error("unknown exception");
            return result;
        }
   }
};


It isn't particularly pretty, and it may be memory leaky, but I haven't refined it much yet. Comments and/or corrections are appreciated.
bakercp
 
Posts: 4
Joined: 17 Jan 2013, 20:57


Return to Support

Who is online

Users browsing this forum: No registered users and 6 guests

cron