Score:2

SQL Server 2019, SQL Server Agent service doesn't auto-load - error 776, token supplied to function is invalid

tt flag

I noticed after a recent update to Windows 2019, our lone server running Active Directory, that several related SQL Server services were no longer starting.

After researching each of those errors, I was able to get them running, leaving SQL Server Agent as the only one left to troubleshoot. I can start it manually, it runs for 10 seconds, then shuts down.

After researching, things I've tried:

  1. SERVICES > SQL SERVER AGENT > LOG ON, I changed this to Local System Account and ticked Allow Service To Interact With Desktop
  2. SERVICES > SQL SERVER AGENT > GENERAL, I changed Startup Type to Automatic (Delayed Start)
  3. SERVICES > SQL SERVER AGENT > Recovery > First Failure, I set to restart the service
  4. SERVICES > SQL SERVER AGENT > Recovery > Second Failure, I set to restart the service

Contents of SQLAGENT.OUT

2023-06-15 09:25:53 - ? [508] Logging SQL Server Agent messages in file 'E:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Log\SQLAGENT.OUT'.
2023-06-15 09:25:53 - ? [000] Event Global\sqlserverRecComplete opened
2023-06-15 09:25:53 - ? [100] Microsoft SQLServerAgent version 15.0.2101.7 (X64 unicode retail build) : Process ID 8028
2023-06-15 09:25:53 - ? [495] The SQL Server Agent startup service account is XXX\SA200$.
2023-06-15 09:25:53 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:25:54 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:25:55 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:25:56 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:25:57 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:25:58 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:25:59 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:26:00 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:26:01 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:26:02 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:26:03 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:26:04 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:26:05 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:26:06 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:26:07 - ! [150] SQL Server does not accept the connection (error: 776). Waiting for Sql Server to allow connections. Operation attempted was: Verify Connection On Start.
2023-06-15 09:26:07 - ! [000] Unable to connect to server '(local)'; SQLServerAgent cannot start
2023-06-15 09:26:07 - ! [298] SQLServer Error: 776, SQL Server Network Interfaces: The token supplied to the function is invalid [SQLSTATE HY000] 
2023-06-15 09:26:07 - ! [298] SQLServer Error: 776, Cannot generate SSPI context [SQLSTATE HY000] 
2023-06-15 09:26:07 - ! [382] Logon to server '(local)' failed (DisableAgentXPs)
2023-06-15 09:26:07 - ? [098] SQLServerAgent terminated (normally)

Any help would be greatly appreciated

Additional Info

In terms of what may have influenced this behaviour, we had a catastrophic failure of the internet telephone line at the premises the day before. The quickest solution was to swap out the existing domestic router with a new one, which uses a sim card / 4G connection. The new router was given same IP address as previous one and its DHCP server was set up with same scope.

This then led me down a new rabbit hole, explaining how an SSPI > SPN error may have come about

article on Cannot Generate SSPI context

result of SQLCHECK...

Suggested SPN                       Exists  Status                                                                                   
--------------------------------    ------  ---------------------------------------------
MSSQLSvc/SA200.ad.redacted.net:1433     False   SPN is on the wrong account: Administrator, CN=Administrator,CN=Users,DC=ad,DC=redacted,DC=net
MSSQLSvc/SA200:1433                 False   SPN does not exist.                                                                      
MSSQLSvc/SA200.ad.redacted.net          False   SPN is on the wrong account: Administrator, CN=Administrator,CN=Users,DC=ad,DC=redacted,DC=net
MSSQLSvc/SA200                      False   SPN does not exist. 

I suspect this SPN issue could be down to me initially using the wrong solution to fix the SQLSERVER service - I altered the log on as to local service which I think may cause the above SPN issue. All I needed do was select delayed start. I'll try and set the SQLSERVER service back to log on as NT AUTHORITY/Network Service tonight and re-run SQLCHECK to see if it solves the SPN issue

UPDATE 22nd July 2023

Added 2 missing SPN's using SETSPN -s ...

SETSPN -s MSSQLSvc/SA200 (computername)

SETSPN -s MSSQLSvc/SA200:1433 (computername)

the 2 other SPN's I cant seem to remove and add back in...

MSSQLSvc/SA200.ad.redacted.net

MSSQLSvc/SA200.ad.redacted.net:1433

I can remove them with SETSPN -d but when I try to add either back in, it tells me duplicates detected

Alberto Morillo avatar
in flag
Have tried to see if Shared Memory and Named Pipes protocols are enabled? Please enable them and restart SQL Server service using SQL Server Configuration Manager.
ZX Spectrum 48K avatar
tt flag
enabled - restarted but problem persists
Score:1
tt flag

In the SQL Server administrator we noticed the service for SQLServer Agent was logging in with the local user account - we changed this to the correct NT Service\SQLServerAgent and problem was corrected

Regards the SPN errors detailed by SQLCheck, we corrected those by using SETSPN -d to first remove each one, being careful to use the correct account name (or else they won't be removed). We then created new SPN's for each with the -s option. Re-running SQLCheck showed no errors

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.