fix: ensure task stealing respects the tasks queueing semantic
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 24 Aug 2023 21:27:18 +0000 (23:27 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 24 Aug 2023 21:27:18 +0000 (23:27 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts

index a6ffcbbb3959caa7ac2480656a25798fa5223856..062df07791af8666fd94282eac229aef466c7a1b 100644 (file)
@@ -1253,18 +1253,14 @@ export abstract class AbstractPool<
           ...(sourceWorkerNode.popTask() as Task<Data>),
           workerId: destinationWorkerNode.info.id as number
         }
-        // Enqueue task for continuous task stealing
-        this.enqueueTask(destinationWorkerNodeKey, task)
-        // Avoid starvation
         if (
-          this.tasksQueueSize(destinationWorkerNodeKey) > 0 &&
+          this.tasksQueueSize(destinationWorkerNodeKey) === 0 &&
           destinationWorkerNode.usage.tasks.executing <
             (this.opts.tasksQueueOptions?.concurrency as number)
         ) {
-          this.executeTask(
-            destinationWorkerNodeKey,
-            this.dequeueTask(destinationWorkerNodeKey) as Task<Data>
-          )
+          this.executeTask(destinationWorkerNodeKey, task)
+        } else {
+          this.enqueueTask(destinationWorkerNodeKey, task)
         }
         break
       }