I was able to fix the issue by using dispatch queue to make sure it's on the main thread and executed without delay
deinit {
func cleanup() {
MainActor.assumeIsolated {
// call to MainActor code
}
}
if Thread.isMainThread {
cleanup()
} else {
DispatchQueue.main.async {
cleanup()
}
}
}