Move dispatch_group_leave to writeSource cancel handler

This ensures that the dispatch_group is not deallocated until it is empty