{"record":{"id":"7bfaed1f7d1181ce","repo":"panjf2000/ants","slug":"can-not-set-up-a-negative-capacity-under-prealloc","errorCode":null,"errorMessage":"can not set up a negative capacity under PreAlloc mode","messagePattern":"can not set up a negative capacity under PreAlloc mode","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ants.go","lineNumber":76,"sourceCode":"\t// CLOSED represents that the pool is closed.\n\tCLOSED\n)\n\nvar (\n\t// ErrLackPoolFunc will be returned when invokers don't provide function for pool.\n\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.","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/panjf2000/ants/blob/107e37678122b00e1b365636ce4f80623ba41579/ants.go#L58-L94","documentation":"ErrInvalidPreAllocSize is returned when PreAlloc mode is requested with capacity -1. Under PreAlloc, ants pre-allocates the worker slice at construction, so the size must be a concrete non-negative value; infinite capacity (-1) cannot be pre-allocated (ants.go:235).","triggerScenarios":"ants.NewPool(-1, ants.WithPreAlloc(true)) or the WithFunc/Generic equivalents passing size -1 together with WithPreAlloc(true).","commonSituations":"Reusing a shared 'size = -1 means unlimited' idiom from non-prealloc code and adding PreAlloc for performance; size derived from config defaulting to -1.","solutions":["Provide a finite positive capacity when using WithPreAlloc(true).","If you truly need an unbounded pool, drop WithPreAlloc (PreAlloc only helps with a known capacity).","Clamp config-derived sizes: if size <= 0 and PreAlloc is enabled, choose a default capacity."],"exampleFix":"// before\npool, err := ants.NewPool(-1, ants.WithPreAlloc(true))\n// after\npool, err := ants.NewPool(10000, ants.WithPreAlloc(true))","handlingStrategy":"validation","validationCode":"if size < 0 && preAlloc {\n    return fmt.Errorf(\"PreAlloc requires a finite capacity, got %d\", size)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use -1 (infinite) only without WithPreAlloc.","Clamp config sizes to a positive default when PreAlloc is enabled.","Prefer explicit finite capacities for predictable memory use."],"tags":["go","validation","configuration","prealloc"],"backgroundTag":"invalid-argument-value","analyzedSha":"107e37678122b00e1b365636ce4f80623ba41579","analyzedAt":"2026-09-06T13:28:28.492Z","contentChangedAt":"2026-09-06T13:28:28.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}