{"record":{"id":"9eaa92eca854b424","repo":"dotnet/efcore","slug":"timeout-must-be-less-than-or-equal-to-int32-maxval","errorCode":null,"errorMessage":"Timeout must be less than or equal to Int32.MaxValue (2147483647) seconds. Provided timeout: {seconds} seconds.","messagePattern":"Timeout must be less than or equal to Int32\\.MaxValue \\(2147483647\\) seconds\\. Provided timeout: (.+?) seconds\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Extensions/RelationalDatabaseFacadeExtensions.cs","lineNumber":972,"sourceCode":"    /// </remarks>\n    /// <param name=\"databaseFacade\">The <see cref=\"DatabaseFacade\" /> for the context.</param>\n    /// <param name=\"timeout\">The timeout to use.</param>\n    public static void SetCommandTimeout(this DatabaseFacade databaseFacade, TimeSpan timeout)\n    {\n        if (timeout == Timeout.InfiniteTimeSpan)\n        {\n            databaseFacade.SetCommandTimeout(0);\n            return;\n        }\n\n        if (timeout < TimeSpan.Zero)\n        {\n            throw new ArgumentException(RelationalStrings.TimeoutTooSmall(timeout.TotalSeconds));\n        }\n\n        if (timeout.TotalSeconds > int.MaxValue)\n        {\n            throw new ArgumentException(RelationalStrings.TimeoutTooBig(timeout.TotalSeconds));\n        }\n\n        databaseFacade.SetCommandTimeout(Convert.ToInt32(timeout.TotalSeconds));\n    }\n\n    /// <summary>\n    ///     Returns the timeout (in seconds) set for commands executed with this <see cref=\"DbContext\" />.\n    /// </summary>\n    /// <remarks>\n    ///     <para>\n    ///         Note that the command timeout is distinct from the connection timeout, which is commonly\n    ///         set on the database connection string.\n    ///     </para>\n    ///     <para>\n    ///         See <see href=\"https://aka.ms/efcore-docs-connections\">Connections and connection strings</see> for more information and examples.\n    ///     </para>\n    /// </remarks>\n    /// <param name=\"databaseFacade\">The <see cref=\"DatabaseFacade\" /> for the context.</param>","sourceCodeStart":954,"sourceCodeEnd":990,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Extensions/RelationalDatabaseFacadeExtensions.cs#L954-L990","documentation":"Thrown by SetCommandTimeout(TimeSpan) when timeout.TotalSeconds exceeds int.MaxValue (2147483647). The ADO.NET CommandTimeout is an int (seconds), so larger spans cannot be represented.","triggerScenarios":"Calling db.Database.SetCommandTimeout(timeSpan) where timeSpan.TotalSeconds > int.MaxValue (RelationalDatabaseFacadeExtensions.cs:970-972). Typically from misconfiguration such as a TimeSpan parsed from an absurd number of days, or a unit confusion (ms vs s).","commonSituations":"Configuration given in milliseconds but treated as seconds; '999999' style sentinel values; TimeSpan.MaxValue; combining days/hours/mins into a huge span; copying a connection-timeout value into command-timeout.","solutions":["Use a reasonable timeout (e.g. 30-300 seconds) or Timeout.InfiniteTimeSpan for unbounded.","Clamp: TimeSpan.FromSeconds(Math.Min(int.MaxValue, Math.Max(0, seconds))).","Verify the unit of the configured value; convert ms->s when needed.","Prefer the int? overload SetCommandTimeout(int?) to avoid TimeSpan arithmetic overflow."],"exampleFix":"// before\ndb.Database.SetCommandTimeout(TimeSpan.FromMilliseconds(cfg.TimeoutMs)); // 2147484000ms treated as seconds -> throws\n\n// after\ndb.Database.SetCommandTimeout(TimeSpan.FromSeconds(cfg.TimeoutMs / 1000.0));\n// or simply\ndb.Database.SetCommandTimeout(cfg.TimeoutSeconds); // int overload","handlingStrategy":"validation","validationCode":"if (timeout.TotalSeconds > int.MaxValue)\n    throw new ArgumentOutOfRangeException(nameof(timeout));\ndb.Database.SetCommandTimeout(timeout);\n// or clamp:\ndb.Database.SetCommandTimeout(TimeSpan.FromSeconds(Math.Min(int.MaxValue, timeout.TotalSeconds)));","typeGuard":"static bool IsValidTimeout(TimeSpan t)\n    => t == Timeout.InfiniteTimeSpan || (t >= TimeSpan.Zero && t.TotalSeconds <= int.MaxValue);","tryCatchPattern":null,"preventionTips":["Prefer the int? SetCommandTimeout overload to avoid TimeSpan arithmetic overflow.","Double-check units (ms vs seconds) in configuration.","Clamp absurd configured values at startup."],"tags":["efcore","relational","command-timeout","validation","overflow"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}