There are certain situations - particularly when performing external I/O - that a long running task may fail to complete. When such a situation occurs, the callback or errback methods on the associated deferred event object will never be called and the callback chain would not then be executed.
To provide a clean way of dealing with long running tasks which fail to complete it is possible to make use of deferred event timeouts. These are supported by the setTimeout and cancelTimeout methods on the Deferred interface. An example of setting a deferred callback timeout is shown in Listing 5.16. The full version of this example is included in the deferred examples package as DeferredTimeoutExample.
In the event that the timeout expired before a call to the callback or errback method is made, an exception of type DeferredTimedOutException will be passed back via the onErrback method of the first deferrable callback handler in the chain. The callback handlers may then process the timeout condition as if it were a conventional error condition. A subtle consequence of triggering a timeout condition in this manner is that a subsequent call to the callback or errback methods will be silently discarded, since the callbacks have already been triggered.
An alternative mechanism for setting timeouts on threadable tasks is described in Section 6.6. In addition to the behaviour described here, the alternative method will also automatically cancel the underlying thread execution.