{"record":{"id":"9dcf9376ac15941d","repo":"HangfireIO/Hangfire","slug":"maxconcurrency","errorCode":null,"errorMessage":"maxConcurrency","messagePattern":"maxConcurrency","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Hangfire.Core/Processing/BackgroundDispatcherAsync.cs","lineNumber":47,"sourceCode":"        private readonly ILog _logger = LogProvider.GetLogger(typeof(BackgroundDispatcherAsync));\n        private readonly CountdownEvent _stopped;\n\n        private readonly IBackgroundExecution _execution;\n        private readonly Func<Guid, object, Task> _action;\n        private readonly object _state;\n\n        private readonly TaskScheduler _taskScheduler;\n        private readonly bool _ownsScheduler;\n\n        public BackgroundDispatcherAsync(\n            [NotNull] IBackgroundExecution execution,\n            [NotNull] Func<Guid, object, Task> action,\n            [CanBeNull] object state,\n            [NotNull] TaskScheduler taskScheduler,\n            int maxConcurrency,\n            bool ownsScheduler)\n        {\n            if (maxConcurrency <= 0) throw new ArgumentOutOfRangeException(nameof(maxConcurrency));\n\n            _execution = execution ?? throw new ArgumentNullException(nameof(execution));\n            _action = action ?? throw new ArgumentNullException(nameof(action));\n            _state = state;\n            _taskScheduler = taskScheduler ?? throw new ArgumentNullException(nameof(taskScheduler));\n            _ownsScheduler = ownsScheduler;\n\n#if !NETSTANDARD1_3\n            AppDomainUnloadMonitor.EnsureInitialized();\n#endif\n\n            _stopped = new CountdownEvent(maxConcurrency);\n\n            for (var i = 0; i < maxConcurrency; i++)\n            {\n                Task.Factory.StartNew(\n                    DispatchLoop,\n                    CancellationToken.None,","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/HangfireIO/Hangfire/blob/c236dd0f930f831ec151e436e138ddc429a02a72/src/Hangfire.Core/Processing/BackgroundDispatcherAsync.cs#L29-L65","documentation":"Thrown by BackgroundDispatcherAsync's constructor (internal) when maxConcurrency is less than or equal to zero. maxConcurrency drives a CountdownEvent and a for-loop that spawns that many dispatch tasks, so a non-positive value is meaningless and would break the shutdown counter.","triggerScenarios":"Constructing BackgroundDispatcherAsync with maxConcurrency = 0 or negative. Reached through Hangfire server setup that computes concurrency from a misconfigured source (e.g., a zero-valued WorkerCount or a negative derived value).","commonSituations":"Server options set WorkerCount to 0; a configuration provider returns an unset/default int that resolves to 0; environment variable parsed as negative; dynamic scaling logic underflows to zero.","solutions":["Ensure the concurrency value passed in is >= 1; clamp with `Math.Max(1, value)` at the configuration boundary.","Validate server options (e.g., BackgroundJobServerOptions.WorkerCount) before starting the server.","Check the source feeding maxConcurrency (config file, env var) for missing or empty values."],"exampleFix":"// before\nvar dispatcher = new BackgroundDispatcherAsync(execution, action, state, scheduler, maxConcurrency: 0, false);\n\n// after\nvar concurrency = Math.Max(1, configuredWorkerCount);\nvar dispatcher = new BackgroundDispatcherAsync(execution, action, state, scheduler, concurrency, false);","handlingStrategy":"validation","validationCode":"int concurrency = Math.Max(1, configuredConcurrency);\n// then pass concurrency to the dispatcher","typeGuard":null,"tryCatchPattern":"try { /* construct dispatcher with concurrency */ }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"maxConcurrency\")\n{ concurrency = 1; /* retry construction */ }","preventionTips":["Clamp all concurrency config to a minimum of 1 at the configuration boundary.","Treat a zero/negative WorkerCount as a configuration error, not a runtime one.","Log the raw configured value before clamping to aid diagnosis."],"tags":["hangfire","threading","argument-validation","configuration","background-processing"],"backgroundTag":null,"analyzedSha":"c236dd0f930f831ec151e436e138ddc429a02a72","analyzedAt":"2026-08-13T20:27:11.027Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}