Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

SQL Server Agent jobs fail with error 258: TCP Provider timeout and login timeout expired

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

The SQL Server Agent service shows as running, yet scheduled jobs do not run. In the SQL Server and Agent logs there are network and authentication timeouts and failed sign-ins. Microsoft's page for this fault gives this example of what is added to the logs:

SQLServer Error: 258, TCP Provider: Timeout error [258]
ODBC Error: 0, Login timeout expired [SQLSTATE HYT00]
SQLServer Error: 258, Unable to complete login process due to delay in prelogin response [SQLSTATE 08001]
Logon to server '<ServerName>' failed (ConnLogJobHistory)

Read the first two sections and run the read-only checks before you restart anything. The last step Microsoft gives, restarting Agent, interrupts running jobs, so it comes last.

What the error means

Microsoft describes the symptom this way: the SQL Server Agent service runs, but scheduled jobs don't run, and the SQL Server and Agent logs show network and authentication timeouts in addition to failed sign-ins. The page lists three underlying problems, and the order of its steps follows them. It lists the checks in a fixed order: service state, jobs and schedules, blocking on the msdb tables, worker, CPU and memory health, and only then a restart of Agent.

Read the example lines for what they plainly say. "TCP Provider: Timeout error [258]" and "Login timeout expired" say a connection attempt ran out of time. "Unable to complete login process due to delay in prelogin response" says a reply from the server was late in the sign-in. The last line says the logon to the server failed. Microsoft's page does not say which of the three causes produces which line, so use the checks below to tell them apart.

Likely causes

Cause What it is How to confirm
Blocking on msdb tables Blocking on the msdb system tables Agent uses "prevents job metadata read and write operations" Blocked tasks on the Agent tables, below
Hung Agent threads or other process-level problems Important Agent threads are not responding Agent not running or not progressing; no blocking found
Worker thread exhaustion in SQL Server No workers are available, so Agent cannot connect or process schedules Worker counts in the health check, below

The page names dbo.sysjobs, dbo.sysjobschedulers and dbo.jobsteps as example tables.

Prove the cause before changing anything

All of these read. The queries are Microsoft's.

  1. Check that the Agent service is running. The page gives these PowerShell commands, one form for default instances and one for named instances:
Get-Service -Name "SQLSERVERAGENT"
Get-Service -Name "SQLSERVERAGENT$<InstanceName>"

Note that, for the restart step, the same page writes the named-instance service as SQLAgent$<InstanceName>. The two forms differ, so find out which name your server uses before any step that names the service.

  1. List the enabled jobs and their schedules from msdb, and look for jobs that are enabled but failed:
USE msdb;
GO
-- List enabled jobs
SELECT name, enabled, description
FROM msdb.dbo.sysjobs
WHERE enabled = 1;
GO
-- List schedules and next run information
SELECT
    s.name AS ScheduleName,
    j.name AS JobName,
    s.enabled AS ScheduleEnabled,
    s.active_start_date,
    s.active_end_time
FROM msdb.dbo.sysjobs j
JOIN msdb.dbo.sysjobschedules js ON j.job_id = js.job_id
JOIN msdb.dbo.sysschedules s ON js.schedule_id = s.schedule_id
WHERE j.enabled = 1;
GO
  1. Look for blocking on the Agent tables:
USE msdb;
GO
SELECT
    session_id,
    blocking_session_id,
    wait_type,
    wait_duration_ms,
    resource_description
FROM sys.dm_os_waiting_tasks
WHERE resource_description LIKE '%sysjobs%'
   OR resource_description LIKE '%sysjobschedulers%'
   OR resource_description LIKE '%jobsteps%';
GO
  1. To find the query behind a blocking session, run Microsoft's longer query, which joins the waiting tasks to the requests and the SQL text for the same three table names. Do this if step 3 returns rows:
SELECT
    wt.session_id,
    wt.blocking_session_id,
    wt.wait_type,
    wt.wait_duration_ms,
    wt.resource_description,
    er.status,
    er.command,
    er.cpu_time,
    er.total_elapsed_time,
    txt.text AS sql_text
FROM sys.dm_os_waiting_tasks wt
LEFT JOIN sys.dm_exec_requests er
       ON wt.session_id = er.session_id
CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) AS txt
WHERE wt.resource_description LIKE '%sysjobs%'
   OR wt.resource_description LIKE '%sysjobschedulers%'
   OR wt.resource_description LIKE '%jobsteps%';

Read the sql_text and blocking_session_id columns. They tell you what the blocking session is running and which session it is.

  1. Run the page's health check query. It reports worker threads, CPU pressure, memory state and page life expectancy from sys.dm_os_schedulers, sys.dm_os_sys_memory and sys.dm_os_performance_counters:
/* ============================================================
   HEALTH CHECK (Worker, CPU, Memory)
   ============================================================ */
SELECT
    Section,
    Metric,
    Value,
    ExtraInfo
FROM (
    /* ===============================
       WORKER THREAD STATUS
       =============================== */
    SELECT
        CAST('WORKER THREAD STATUS' AS VARCHAR(MAX)) AS Section,
        CAST(CONCAT('Scheduler ', scheduler_id) AS VARCHAR(MAX)) AS Metric,
        CAST(CONCAT('Workers: ', active_workers_count, '/', current_workers_count) AS VARCHAR(MAX)) AS Value,
        CAST(CONCAT('WorkQueue=', work_queue_count, ', Idle=', is_idle) AS VARCHAR(MAX)) AS ExtraInfo
    FROM sys.dm_os_schedulers
    WHERE scheduler_id < 255
    UNION ALL
    /* ===============================
       CPU PRESSURE
       =============================== */
    SELECT
        CAST('CPU PRESSURE' AS VARCHAR(MAX)) AS Section,
        CAST(CONCAT('Scheduler ', scheduler_id) AS VARCHAR(MAX)) AS Metric,
        CAST(CONCAT('RunnableTasks=', runnable_tasks_count) AS VARCHAR(MAX)) AS Value,
        CAST(CONCAT('PendingIO=', pending_disk_io_count) AS VARCHAR(MAX)) AS ExtraInfo
    FROM sys.dm_os_schedulers
    WHERE scheduler_id < 255
    UNION ALL
    /* ===============================
       MEMORY STATUS (System)
       =============================== */
    SELECT
        CAST('MEMORY STATUS' AS VARCHAR(MAX)) AS Section,
        CAST('SystemMemoryState' AS VARCHAR(MAX)) AS Metric,
        CAST(system_memory_state_desc AS VARCHAR(MAX)) AS Value,
        CAST(CONCAT('TotalMB=', total_physical_memory_kb/1024,
                    ', AvailableMB=', available_physical_memory_kb/1024) AS VARCHAR(MAX)) AS ExtraInfo
    FROM sys.dm_os_sys_memory
    UNION ALL
    /* ===============================
       PAGE LIFE EXPECTANCY
       =============================== */
    SELECT
        CAST('PAGE LIFE EXPECTANCY' AS VARCHAR(MAX)) AS Section,
        CAST('PLE' AS VARCHAR(MAX)) AS Metric,
        CAST(cntr_value AS VARCHAR(MAX)) AS Value,
        CAST(NULL AS VARCHAR(MAX)) AS ExtraInfo
    FROM sys.dm_os_performance_counters
    WHERE counter_name = 'Page life expectancy'
      AND object_name LIKE '%Buffer Manager%'
) AS x
ORDER BY Section, Metric;

Microsoft lists these signs:

  • Worker thread pressure: workers exhausted, for example "Workers: 512/512", or a WorkQueue greater than zero, which means tasks are waiting and the system is overloaded.
  • CPU pressure: RunnableTasks greater than zero. Microsoft says this can also mean a scheduler is being monopolised by a thread.
  • Memory pressure: memory state LOW, a low AvailableMB, or a page life expectancy value under 300.

Example (illustrative): on a hosted server sql02, jobs stop at 02:00 and the logs show error 258 each night. Step 3 returns a row where session 61 blocks a task waiting on sysjobs. That matches the first cause on Microsoft's page.

Fix

Follow Microsoft's order, and stop when jobs run again.

Service not running

If step 1 shows Agent is stopped, Microsoft's page says to start it. Record the service state first, with the step 1 command, and keep the output:

Get-Service -Name "SQLSERVERAGENT"

Risk: The next command starts the SQL Server Agent service, so any enabled jobs with schedules can begin to run.

Start-Service -Name "SQLSERVERAGENT"

For a named instance the page gives Start-Service -Name "SQLSERVERAGENT$<InstanceName>".

To undo it: stop the SQL Server Agent service again with your usual service management tool. Microsoft's page gives no stop command, and stopping Agent means scheduled jobs stop running, so only do it if you have a reason to keep Agent off.

Blocking on the Agent tables

If step 3 shows a blocking session, Microsoft's page says to resolve or terminate it, using Kill <Blocking_Session_ID>. First identify who owns the session and what it is doing. The query in step 4 shows the SQL text. Save those details before ending anything: run the step 4 query once more and keep its output, which records the blocking session, what it was running and how long it had been waiting.

SELECT
    wt.session_id,
    wt.blocking_session_id,
    wt.wait_type,
    wt.wait_duration_ms,
    wt.resource_description,
    er.status,
    er.command,
    er.cpu_time,
    er.total_elapsed_time,
    txt.text AS sql_text
FROM sys.dm_os_waiting_tasks wt
LEFT JOIN sys.dm_exec_requests er
       ON wt.session_id = er.session_id
CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) AS txt
WHERE wt.resource_description LIKE '%sysjobs%'
   OR wt.resource_description LIKE '%sysjobschedulers%'
   OR wt.resource_description LIKE '%jobsteps%';

Risk: The next statement ends one session. Microsoft's KILL page says the statement can take some time to complete, particularly when the session has a long transaction to roll back, and that system processes and processes running an extended stored procedure can't be ended.

Before you run it, replace the placeholder <session_id> with the blocking session ID from step 3 that you have identified as the cause. Do not run the statement with the placeholder left in.

KILL <session_id>;

Microsoft's KILL page says that repeating a KILL for the same session ID is risky, because the session ID can be reassigned to a new task before the second statement runs. To read the rollback progress instead, replace the placeholder with the same session ID and use the page's WITH STATUSONLY form:

KILL <session_id> WITH STATUSONLY;

To undo it: a killed session cannot be brought back. Whoever owned it must reconnect and repeat the work that was rolled back.

Worker, CPU or memory pressure

Microsoft's page says: "If you identified any worker, CPU, or memory issues in the previous step, reduce your current workload to resolve the issues." The page gives no commands for this. It depends on which workload you can pause or move.

Restart Agent

If you found no problem in the checks, or the jobs still do not run, Microsoft's last step is to restart SQL Server Agent, with the note that "Restarting the SQL Server Agent interrupts any currently running jobs."

Before you do, look at which jobs are running now in Job Activity Monitor and write them down.

Disruptive step: The next command restarts the SQL Server Agent service. Any job that is running at that moment is interrupted.

Restart-Service -Name "SQLSERVERAGENT"

For a named instance the page gives Restart-Service -Name "SQLAgent$<InstanceName>".

To undo it: a restart cannot be reversed. If Agent does not come back, start it with the Start-Service command above. Run again any job that was interrupted and that you noted before the restart.

Confirm the fix

  • Microsoft's page says to check, after the restart, that jobs now run, using Job Activity Monitor.
  • Re-run the step 3 query. It returns no rows for the Agent tables.
  • Check the SQL Server and Agent logs. New error 258 lines do not appear.

What to watch afterwards

  • Whether the failure returns at the same time each day. A scheduled workload that exhausts workers or blocks msdb will repeat.
  • Worker counts and RunnableTasks during the busy period, using the health check query.
  • Any long transaction that touches msdb tables.

Risks and limits

These steps have not been run or lab-tested. Microsoft's page gives no version range, and it does not say how to tell a hung Agent thread from the other causes apart from the steps above. Its second blocking filter names sysjobschedulers, while its schedule query joins sysjobschedules, so a result with no rows does not rule out worker exhaustion or a hang. The page gives Kill as a way to end blocking, with no guidance on whether the blocking session's work can be repeated, so that judgement stays with you.

Sources