Improve failed task handling.

See full commit messages for details.

@Osmose r?