Skip to content

Error 1067 during windows service stop #81274

Closed

Description

@jgasiorowski

Description

Following ms docs if I use windows Services ui to stop the service I will be getting Error 1067: The process terminated unexpectedly
image

It happens because Task.Delay is throwing TaskCanceledException which gets catched and Environment.Exit is terminating the process before service can stop gracefuly.

I think I can catch this exception explicitly and do something else to fix the problem but I've notice that documentation while I was investigating an issue after migrating my app to net6.

Are there any resources which can help me diagnose an issue where this error appears randomly on stopping the service? I have only 4 IHostedService instances in it and I went through code of these projectcs and I couldn't find evidence than any of these are causing that behaviour.

I've been removing all these hosted services and this issue was still appearing during stop even with just Serilog library used.
It would be very helpful if you could provide any knowledge on what scenarios this can happen

Reproduction Steps

Just follow ms docs pages

Expected behavior

Docs should not suggest to build BackgroundService that way

Actual behavior

Stoping the service is throwing an error to ui which is confusing and if recovery strategy is applied - service might get restarted which is not expected if a user stopped it intentionally

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jan 27, 2023
  2. jgasiorowski commented on Jan 27, 2023

    @jgasiorowski
    Author

    Just an update:
    I wanted to try if error 1067 occurs on clean net6 worker service. All I did is I've created a project from template

    dotnet new worker -n TestWorkerFromTemplate --use-program-main true -f net6.0
    

    added package

    dotnet add TestWorkerFromTemplate package Microsoft.Extensions.Hosting.WindowsServices -v 6.0.1
    

    Added UseWindowsService() call on IHostBuilder in Program.cs

    installed windows service with sc.exe create command
    and runned a powershell script to start, stop and check status of that service like:

    $found = $false;
    $count = 0;
    while ($found -eq $false){
        Start-Service AATestWorker
        Start-Sleep -Seconds 1
        Stop-Service AATestWorker
        Start-Sleep -Seconds 1
        $status = & sc.exe query AATestWorker
    
        foreach ($t in $status){
            if ($t -match 'WIN32_EXIT_CODE' -And $t -match '1067'){
                
                echo $status
                $found = $true;
                break;
            }
        };
        $count++;
        echo "Finished attempt #$count"
    }

    I know it might look crazy but I would expect this to be reliable especially that on net core 3.1 we did not have that error ever.
    I've runned this test 3 times. I was waiting for the first time for about 20 minutes to get that error. On second and third attempt I received result quicker (25 and 27 iterations) :

    Finished attempt #18
    Finished attempt #19
    Finished attempt #20
    Finished attempt #21
    Finished attempt #22
    Finished attempt #23
    Finished attempt #24
    
    SERVICE_NAME: AATestWorker
            TYPE               : 10  WIN32_OWN_PROCESS
            STATE              : 1  STOPPED
            WIN32_EXIT_CODE    : 1067  (0x42b)
            SERVICE_EXIT_CODE  : 0  (0x0)
            CHECKPOINT         : 0x0
            WAIT_HINT          : 0x0
    Finished attempt #25
    

    With empty worker it happens less often - but when using our normal service which is using more libraries like Serilog, MassTransit which are using resources mostly from Azure it happens much more frequently

    View from EventViewer
    image

  3. ericstj commented on Feb 14, 2023

    @ericstj
    Member

    I believe this is the same race condition we found in #62579.

  4. jgasiorowski commented on Feb 15, 2023

    @jgasiorowski
    Author

    Thanks @ericstj I think you are right. But I don't know really what is the status of the referenced issue? It is still open but you already found the issue. It is also assigned to Future milestone. Does it mean that there is no plan to fix it for .net 7 or even 8?

  5. ericstj commented on Feb 16, 2023

    @ericstj
    Member

    It's a bit of a fundamental design challenge with Windows Services + Hosting that's always been present in some form. It needs folks who own hosting to debate if there is a way to make the models work together.

    As a workaround you can try adding a delay to your main routine after the call to Run() to see if that can help in the short term until the Hosting owners can determine a fix.

  6. jgasiorowski commented on Feb 16, 2023

    @jgasiorowski
    Author

    Thanks @ericstj for suggestion. We are also planning to modify our script used for stoping the service to disable it as well - that way we will make sure that service will not start unexpectedly

  7. ericstj commented on Feb 16, 2023

    @ericstj
    Member

    If you have a chance to try the delay and report back here, it would be a useful data point to the team. It would confirm we're seeing the same issue.

  8. added this to the Future milestone on Feb 17, 2023
  9. ghost removed
    untriagedNew issue has not been triaged by the area owner
    on Feb 17, 2023
  10. added
    needs-author-actionAn issue or pull request that requires more info or actions from the author.
    on Feb 17, 2023
  11. ghost closed this as completedon Mar 18, 2023
  12. ghost locked as resolved and limited conversation to collaborators on Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions