ensure cleanup runs before callback invoked, to prevent one cause of double callbacks

ensure that cleanup runs before the callback is invoked, to prevent for example a thrown exception in the callback preventing the cleanup from happening.