Fix issue with cancelling tasks and intermitten NPE.