Merge "Clean up resources before detaching the task from hierarchy"