netdata/netdata · error

Number of threads must be a positive integer.

Error message

Number of threads must be a positive integer.

What it means

Validation error from busy_threads (src/collectors/apps.plugin/busy_threads.c:47): the single argument parsed with atoi() must be a positive integer. When num_threads <= 0 (non-numeric input that atoi() maps to 0, zero, or a negative number), the tool prints this message and exits with EXIT_FAILURE before creating any threads.

Source

Thrown at src/collectors/apps.plugin/busy_threads.c:47

    keep_running = 0;
}

void *busy_loop(void *arg) {
    while (keep_running) {
        // Busy loop to keep CPU at 100%
    }
    return NULL;
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <number of threads>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    int num_threads = atoi(argv[1]);
    if (num_threads <= 0) {
        fprintf(stderr, "Number of threads must be a positive integer.\n");
        exit(EXIT_FAILURE);
    }

    // Register the signal handler to gracefully exit on Ctrl-C
    signal(SIGINT, handle_signal);

    pthread_t *threads = malloc(sizeof(pthread_t) * num_threads);
    if (threads == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    // Create threads
    for (int i = 0; i < num_threads; i++) {
        if (pthread_create(&threads[i], NULL, busy_loop, NULL) != 0) {
            perror("pthread_create");
            free(threads);
            exit(EXIT_FAILURE);

View on GitHub (pinned to 4864de85e2)

Solutions

  1. Pass a positive integer: './busy_threads 4'.
  2. In scripts, default and validate first: 'THREADS=${THREADS:-2}; case $THREADS in (*[!0-9]*|'') echo bad;; esac'.
  3. Keep the value within int range to avoid atoi overflow wrap-around.

Example fix

# before
$ ./busy_threads "$THREADS"   # THREADS unset -> '' -> 0
# after
$ THREADS=${THREADS:-2}; ./busy_threads "$THREADS"
Defensive patterns

Strategy: validation

Validate before calling

# bash: reject empty/non-numeric/non-positive values
n=${1:-}; [[ $n =~ ^[1-9][0-9]*$ ]] || { echo "thread count must be a positive integer" >&2; exit 1; }
./busy_threads "$n"

Type guard

/* C: strict positive-int check unlike the tool's atoi */
static bool positive_int(const char *s, long *out) {
    char *end; long v = strtol(s, &end, 10);
    if (end == s || *end != '\0' || v <= 0 || v > INT_MAX) return false;
    *out = v; return true;
}

Prevention

When it happens

Trigger: Running './busy_threads abc' (atoi returns 0), './busy_threads 0', './busy_threads -2', or a value that overflows int so atoi wraps to a non-positive result.

Common situations: Shell scripts passing an empty or unset variable ('$THREADS' when THREADS is empty becomes argument '0' or missing); copy/paste with a stray character; automated harness supplying a non-numeric parameter.

Related errors


AI-assisted analysis of netdata/netdata@4864de85e2 (2026-08-15). Data as JSON: /api/errors/cdf9fd3103a48650. Report an issue: GitHub.