title: Why is Android Worker finishing successfully and calling onStopped()
body: <p>My current Android app employs <code>androidx.work:work-runtime:2.2.0-rc01</code></p>
  
  <p>My Worker code resembles this:-</p>
  
  <pre><code>class SyncWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) {
  
      private var syncWorkerResult: Result = Result.success()
  
      override fun doWork(): Result {
  
          return syncWorkerResult
      }
  
      override fun onStopped() {
          Log.i(TAG, "onStopped() $isStopped")
  
          super.onStopped()
  
      }
  }
  </code></pre>
  
  <p>As far as I understand the docs for Worker I shouldnt be seeing the following logs:-</p>
  
  <pre><code>2019-08-21 14:25:55.183 22716-22750/com.my.app I/WM-WorkerWrapper: Worker result SUCCESS for Work [ id=70a5ff81-1b4b-4604-9d2e-a0b3d342a608, tags={ com.my.app.sync.SyncWorker, SYNC-IN-PROGRESS-TAG } ]
  2019-08-21 14:25:55.202 22716-22768/com.my.app I/SyncWorker: onStopped() true
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>As my worker is reporting result SUCCESS and success is defined as follows:-</p>
  
  <pre><code>Returns an instance of ListenableWorker.Result that can be used to indicate that the work completed successfully. Any work that depends on this can be executed as long as all of its other dependencies and constraints are met.
  </code></pre>
  
  <p>This is obviously a defect in the worker code as the above logs shows my worker has completed successfully and then gets stopped, what is there to "STOP" if its already completed successfully?</p>
  
  <p>I do not understand why <code>androidx.work.impl.WorkerWrapper</code> interrupt method is being called for my worker that has completed successfully</p>
  
  <pre><code>/**
   * @hide
   */
  @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
  public void interrupt(boolean cancelled) {
      mInterrupted = true;
      // Resolve WorkerWrapper's future so we do the right thing and setup a reschedule
      // if necessary. mInterrupted is always true here, we don't really care about the return
      // value.
      tryCheckForInterruptionAndResolve();
      if (mInnerFuture != null) {
          // Propagate the cancellations to the inner future.
          mInnerFuture.cancel(true);
      }
      // Worker can be null if run() hasn't been called yet.
      if (mWorker != null) {
          mWorker.stop();
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/nzSAQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nzSAQ.png" alt="enter image description here"></a></p>
  
  <p>Searching through the Android worker source code I have identified this method</p>
  
  <pre><code>/**
   * Stops a unit of work.
   *
   * @param id The work id to stop
   * @return {@code true} if the work was stopped successfully
   */
  public boolean stopWork(String id) {
      synchronized (mLock) {
          Logger.get().debug(TAG, String.format("Processor stopping %s", id));
          WorkerWrapper wrapper = mEnqueuedWorkMap.remove(id);
          if (wrapper != null) {
              wrapper.interrupt(false);
              Logger.get().debug(TAG, String.format("WorkerWrapper stopped for %s", id));
              return true;
          }
          Logger.get().debug(TAG, String.format("WorkerWrapper could not be found for %s", id));
          return false;
      }
  }
  </code></pre>
  
  <p>Which calls the <code>wrapper.interrupt(false);</code> method when it removes a worker ID present in the <code>mEnqueuedWorkMap</code> as shown by this debug variables image</p>
  
  <p><a href="https://i.stack.imgur.com/6gvtX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6gvtX.png" alt="enter image description here"></a></p>
  
  <p>Heres the WorkManager logs for when my Worker has its onStopped() method called</p>
  
  <pre><code>2019-08-23 13:02:32.754 21031-21031/com.my.app D/WM-PackageManagerHelper: androidx.work.impl.background.systemjob.SystemJobService enabled
  2019-08-23 13:02:32.754 21031-21031/com.my.app D/WM-Schedulers: Created SystemJobScheduler and enabled SystemJobService
  2019-08-23 13:02:32.763 21031-21085/com.my.app D/WM-ForceStopRunnable: Performing cleanup operations.
  2019-08-23 13:02:32.884 21031-21085/com.my.app D/WM-ForceStopRunnable: Application was force-stopped, rescheduling.
  2019-08-23 13:02:44.219 21031-21098/com.my.app D/WM-PackageManagerHelper: androidx.work.impl.background.systemalarm.RescheduleReceiver enabled
  2019-08-23 13:02:44.237 21031-21098/com.my.app D/WM-SystemJobScheduler: Scheduling work ID e6a31ec8-a155-4d15-8cf7-af505c70e323 Job ID 0
  2019-08-23 13:02:44.244 21031-21098/com.my.app D/WM-GreedyScheduler: Starting work for e6a31ec8-a155-4d15-8cf7-af505c70e323
  2019-08-23 13:02:44.268 21031-21085/com.my.app D/WM-PackageManagerHelper: androidx.work.impl.background.systemalarm.RescheduleReceiver enabled
  2019-08-23 13:02:44.302 21031-21085/com.my.app D/WM-SystemJobScheduler: Scheduling work ID 075fb9b3-e19b-463b-89f1-9e737e476d5b Job ID 1
  2019-08-23 13:02:44.331 21031-21101/com.my.app D/WM-Processor: Processor: processing e6a31ec8-a155-4d15-8cf7-af505c70e323
  2019-08-23 13:02:44.723 21031-21031/com.my.app D/WM-WorkerWrapper: Starting work for com.my.app.sync.SyncWorker
  2019-08-23 13:02:44.730 21031-21031/com.my.app D/WM-SystemJobService: onStartJob for e6a31ec8-a155-4d15-8cf7-af505c70e323
  2019-08-23 13:02:44.731 21031-21101/com.my.app D/WM-Processor: Work e6a31ec8-a155-4d15-8cf7-af505c70e323 is already enqueued for processing
  2019-08-23 13:02:44.795 21031-21098/com.my.app D/WM-WorkerWrapper: com.my.app.sync.SyncWorker returned a Success {mOutputData=androidx.work.Data@0} result.
  2019-08-23 13:02:44.797 21031-21098/com.my.app I/WM-WorkerWrapper: Worker result SUCCESS for Work [ id=e6a31ec8-a155-4d15-8cf7-af505c70e323, tags={ com.my.app.sync.SyncWorker, SYNC-IN-PROGRESS-TAG } ]
  2019-08-23 13:02:44.808 21031-21031/com.my.app D/WM-Processor: Processor e6a31ec8-a155-4d15-8cf7-af505c70e323 executed; reschedule = false
  2019-08-23 13:02:44.808 21031-21031/com.my.app D/WM-SystemJobService: e6a31ec8-a155-4d15-8cf7-af505c70e323 executed on JobScheduler
  2019-08-23 13:02:44.814 21031-21098/com.my.app D/WM-GreedyScheduler: Cancelling work ID e6a31ec8-a155-4d15-8cf7-af505c70e323
  2019-08-23 13:02:44.828 21031-21085/com.my.app D/WM-Processor: Processor stopping e6a31ec8-a155-4d15-8cf7-af505c70e323
  2019-08-23 13:02:44.829 21031-21085/com.my.app D/WM-Processor: WorkerWrapper could not be found for e6a31ec8-a155-4d15-8cf7-af505c70e323
  2019-08-23 13:02:44.829 21031-21085/com.my.app D/WM-StopWorkRunnable: StopWorkRunnable for e6a31ec8-a155-4d15-8cf7-af505c70e323; Processor.stopWork = false
  2019-08-23 13:02:44.856 21031-21098/com.my.app D/WM-PackageManagerHelper: androidx.work.impl.background.systemalarm.RescheduleReceiver enabled
  2019-08-23 13:02:44.874 21031-21098/com.my.app D/WM-SystemJobScheduler: Scheduling work ID ba72423c-5e4b-425c-aaab-a9a14efaf3f8 Job ID 2
  2019-08-23 13:02:44.880 21031-21098/com.my.app D/WM-GreedyScheduler: Starting work for ba72423c-5e4b-425c-aaab-a9a14efaf3f8
  2019-08-23 13:02:44.882 21031-21101/com.my.app D/WM-Processor: Processor: processing ba72423c-5e4b-425c-aaab-a9a14efaf3f8
  2019-08-23 13:02:44.897 21031-21031/com.my.app D/WM-SystemJobService: onStartJob for ba72423c-5e4b-425c-aaab-a9a14efaf3f8
  2019-08-23 13:02:44.900 21031-21031/com.my.app D/WM-WorkerWrapper: Starting work for com.my.app.sync.SyncWorker
  2019-08-23 13:02:44.908 21031-21101/com.my.app D/WM-Processor: Work ba72423c-5e4b-425c-aaab-a9a14efaf3f8 is already enqueued for processing
  2019-08-23 13:02:44.973 21031-21101/com.my.app D/WM-WorkerWrapper: com.my.app.sync.SyncWorker returned a Success {mOutputData=androidx.work.Data@0} result.
  2019-08-23 13:02:44.975 21031-21101/com.my.app I/WM-WorkerWrapper: Worker result SUCCESS for Work [ id=ba72423c-5e4b-425c-aaab-a9a14efaf3f8, tags={ com.my.app.sync.SyncWorker, SYNC-IN-PROGRESS-TAG } ]
  2019-08-23 13:02:44.989 21031-21101/com.my.app D/WM-GreedyScheduler: Cancelling work ID ba72423c-5e4b-425c-aaab-a9a14efaf3f8
  2019-08-23 13:02:44.996 21031-21085/com.my.app D/WM-Processor: Processor stopping ba72423c-5e4b-425c-aaab-a9a14efaf3f8
  2019-08-23 13:02:44.997 21031-21085/com.my.app D/WM-WorkerWrapper: Work interrupted for Work [ id=ba72423c-5e4b-425c-aaab-a9a14efaf3f8, tags={ com.my.app.sync.SyncWorker, SYNC-IN-PROGRESS-TAG } ]
  
  2019-08-23 13:02:44.999 21031-21085/com.my.app I/SyncWorker: onStopped() ba72423c-5e4b-425c-aaab-a9a14efaf3f8 Success {mOutputData=androidx.work.Data@0}
  
  2019-08-23 13:02:44.999 21031-21085/com.my.app D/WM-Processor: WorkerWrapper stopped for ba72423c-5e4b-425c-aaab-a9a14efaf3f8
  2019-08-23 13:02:44.999 21031-21085/com.my.app D/WM-StopWorkRunnable: StopWorkRunnable for ba72423c-5e4b-425c-aaab-a9a14efaf3f8; Processor.stopWork = true
  2019-08-23 13:02:45.045 21031-21031/com.my.app D/WM-Processor: Processor ba72423c-5e4b-425c-aaab-a9a14efaf3f8 executed; reschedule = false
  2019-08-23 13:02:45.046 21031-21031/com.my.app D/WM-SystemJobService: ba72423c-5e4b-425c-aaab-a9a14efaf3f8 executed on JobScheduler
  2019-08-23 13:02:45.047 21031-21031/com.my.app D/WM-SystemJobService: onStopJob for ba72423c-5e4b-425c-aaab-a9a14efaf3f8
  2019-08-23 13:02:45.049 21031-21098/com.my.app D/WM-Processor: Processor stopping ba72423c-5e4b-425c-aaab-a9a14efaf3f8
  2019-08-23 13:02:45.049 21031-21098/com.my.app D/WM-Processor: WorkerWrapper could not be found for ba72423c-5e4b-425c-aaab-a9a14efaf3f8
  2019-08-23 13:02:45.049 21031-21098/com.my.app D/WM-StopWorkRunnable: StopWorkRunnable for ba72423c-5e4b-425c-aaab-a9a14efaf3f8; Processor.stopWork = false
  </code></pre>
  
  <p>UPDATE</p>
  
  <p>This issue occurs when I start a second instance of my the worker immediately after the first instance completes OK. The second instance consistently behaves as shown above. When I add a <code>Thread.sleep(Xms)</code> within the <code>doWork()</code> method I have "some" control over when it occurs as by increasing Xms the issue takes long to appear.</p>
  
  <p>e.g. If I set up a "loop" where I start a new worker each time the pervious worker completes OK, I always see this issue where a subsequent worker instance will both complete <code>SUCCESS</code> and <code>onStopped()</code> is called. </p>
  
  <p>UPDATE II</p>
  
  <p>Heres a code snippet showing how I start the Worker</p>
  
  <pre><code>val refreshDatabaseWork: OneTimeWorkRequest = OneTimeWorkRequest.Builder(SyncWorker::class.java)
          .keepResultsForAtLeast(1L, TimeUnit.NANOSECONDS)
          .addTag(WORK_IN_PROGRESS_TAG).build()
  WorkManager.getInstance(application).beginUniqueWork(UNIQUE_WORK_NAME, ExistingWorkPolicy.KEEP, refreshDatabaseWork).enqueue()
  </code></pre>
  
  <p>I've opened an Issue <a href="https://issuetracker.google.com/issues/140055777" rel="nofollow noreferrer">https://issuetracker.google.com/issues/140055777</a> with small project that reproduces the issue</p>