{"record":{"id":"791030876b1a6d90","repo":"panjf2000/ants","slug":"invalid-pool-index","errorCode":null,"errorMessage":"invalid pool index","messagePattern":"invalid pool index","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ants.go","lineNumber":82,"sourceCode":"\tErrLackPoolFunc = errors.New(\"must provide function for pool\")\n\n\t// ErrInvalidPoolExpiry will be returned when setting a negative number as the periodic duration to purge goroutines.\n\tErrInvalidPoolExpiry = errors.New(\"invalid expiry for pool\")\n\n\t// ErrPoolClosed will be returned when submitting task to a closed pool.\n\tErrPoolClosed = errors.New(\"this pool has been closed\")\n\n\t// ErrPoolOverload will be returned when the pool is full and no workers available.\n\tErrPoolOverload = errors.New(\"too many goroutines blocked on submit or Nonblocking is set\")\n\n\t// ErrInvalidPreAllocSize will be returned when trying to set up a negative capacity under PreAlloc mode.\n\tErrInvalidPreAllocSize = errors.New(\"can not set up a negative capacity under PreAlloc mode\")\n\n\t// ErrTimeout will be returned after the operations timed out.\n\tErrTimeout = errors.New(\"operation timed out\")\n\n\t// ErrInvalidPoolIndex will be returned when trying to retrieve a pool with an invalid index.\n\tErrInvalidPoolIndex = errors.New(\"invalid pool index\")\n\n\t// ErrInvalidLoadBalancingStrategy will be returned when trying to create a MultiPool with an invalid load-balancing strategy.\n\tErrInvalidLoadBalancingStrategy = errors.New(\"invalid load-balancing strategy\")\n\n\t// ErrInvalidMultiPoolSize  will be returned when trying to create a MultiPool with an invalid size.\n\tErrInvalidMultiPoolSize = errors.New(\"invalid size for multiple pool\")\n\n\t// workerChanCap determines whether the channel of a worker should be a buffered channel\n\t// to get the best performance. Inspired by fasthttp at\n\t// https://github.com/valyala/fasthttp/blob/master/workerpool.go#L139\n\tworkerChanCap = func() int {\n\t\t// Use blocking channel if GOMAXPROCS=1.\n\t\t// This switches context from sender to receiver immediately,\n\t\t// which results in higher performance (under go1.5 at least).\n\t\tif runtime.GOMAXPROCS(0) == 1 {\n\t\t\treturn 0\n\t\t}\n","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/panjf2000/ants/blob/107e37678122b00e1b365636ce4f80623ba41579/ants.go#L64-L100","documentation":"ErrInvalidPoolIndex is returned by MultiPool index-based accessors such as WaitingByIndex and RunningByIndex when the given index is outside the range [0, len(pools)). It is an argument-validation error protecting against out-of-bounds pool lookups on a MultiPool.","triggerScenarios":"mp.WaitingByIndex(-1) or mp.WaitingByIndex(11) when the MultiPool was created with fewer than 12 sub-pools; similarly RunningByIndex with an out-of-range index.","commonSituations":"Computing an index from a hash/shard key without modding by the pool count; hard-coded indices that drift from the configured pool size; iterating with wrong bounds.","solutions":["Validate the index before calling: ensure 0 <= idx < the size passed to NewMultiPool*.","Reduce sharding keys with modulo the actual pool count.","If you only need totals, use mp.Waiting()/mp.Running() instead of per-index lookups."],"exampleFix":"// before\nw, err := mp.WaitingByIndex(shardID)\n// after\nidx := int(shardID) % poolSize\nif idx < 0 {\n    idx += poolSize\n}\nw, err := mp.WaitingByIndex(idx)","handlingStrategy":"validation","validationCode":"func validIndex(idx, poolSize int) bool {\n    return idx >= 0 && idx < poolSize\n}\nif !validIndex(i, poolCount) {\n    return fmt.Errorf(\"pool index %d out of range [0,%d)\", i, poolCount)\n}","typeGuard":null,"tryCatchPattern":"if _, err := mp.WaitingByIndex(i); errors.Is(err, ants.ErrInvalidPoolIndex) {\n    i = 0 // or return a config error\n}","preventionTips":["Keep the MultiPool size in a variable and compute indices as idx %% size.","Never hard-code sub-pool indices; iterate over [0, size).","Use mp.Waiting()/mp.Running() aggregates when per-index detail is unnecessary."],"tags":["go","index-out-of-range","validation","multipool"],"backgroundTag":"index-out-of-range","analyzedSha":"107e37678122b00e1b365636ce4f80623ba41579","analyzedAt":"2026-09-06T13:28:28.492Z","contentChangedAt":"2026-09-06T13:28:28.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}