@@ -421,27 +421,28 @@ internal class FirebaseDataConnectImpl(
421421 }
422422 }
423423
424- // Register the new "close job", unless there is a "close job" already in progress or one that
425- // completed successfully.
426- val updatedCloseJob =
427- closeJob.updateAndGet { oldCloseJob ->
428- if (oldCloseJob.ref != = null && ! oldCloseJob.ref.isCancelled) {
429- oldCloseJob
424+ // Register the new "close job". Do not overwrite a close job that is already in progress (to
425+ // avoid having more than one close job in progress at a time) or a close job that completed
426+ // successfully (since there is nothing to do if a previous close job was successful).
427+ val updatedCloseJobRef =
428+ closeJob.updateAndGet { oldCloseJobRef: NullableReference <Deferred <Unit >> ->
429+ if (oldCloseJobRef.ref != = null && ! oldCloseJobRef.ref.isCancelled) {
430+ oldCloseJobRef
430431 } else {
431432 NullableReference (newCloseJob)
432433 }
433434 }
434435
435- // If the updated "close job" was the one that we created, then start it!
436- if (updatedCloseJob.ref == = newCloseJob) {
437- newCloseJob.start()
438- }
436+ // Start the updated "close job" (if it was already started then start() is a no-op).
437+ val updatedCloseJob =
438+ checkNotNull(updatedCloseJobRef.ref) {
439+ " internal error: closeJob.updateAndGet() returned a NullableReference whose 'ref' " +
440+ " property was null; however it should NOT have been null (error code y5fk4ntdnd)"
441+ }
442+ updatedCloseJob.start()
439443
440- // Return the job "close job" that is active or already completed so that the caller can await
441- // its result.
442- return checkNotNull(updatedCloseJob.ref) {
443- " updatedCloseJob.ref should not have been null (error code y5fk4ntdnd)"
444- }
444+ // Return the "close job", which _may_ already be completed, so the caller can await it.
445+ return updatedCloseJob
445446 }
446447
447448 // The generated SDK relies on equals() and hashCode() using object identity.
0 commit comments