Skip to content

WorkerPool allows zero workers but next_worker() panics on an empty pool #423

Description

@xc01

Describe the bug

WorkerPool::new(..., 0) is currently allowed and returns Ok(...):

/// This can happen if the pool was created with 0 workers
/// Which is not particularly useful, but is allowed

However, calling next_worker() on that pool panics because it indexes workers[0] even when the pool is empty:

let worker = &self.workers[self.next_worker];

This means a configuration value of 0 workers is accepted during initialization, but later turns into a runtime panic when work is dispatched.

In a server setting, this can move a configuration error from startup time into request/task handling.

To Reproduce (PoC)

use rustyscript::worker::{DefaultWorker, DefaultWorkerOptions, WorkerPool};

#[test]
#[should_panic]
fn worker_pool_next_worker_panics_when_pool_is_empty() {
    let mut pool = WorkerPool::<DefaultWorker>::new(
        DefaultWorkerOptions::default(),
        0,
    )
    .expect("creating a zero-worker pool is currently allowed");

    let _ = pool.next_worker();
}

Expected behavior

Either:

  • WorkerPool::new(..., 0) should return an error, or
  • next_worker() should gracefully handle an empty pool instead of panicking.

Desktop

OS: Ubuntu 22.04.1
Version: 0.12.3

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions