dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.45k stars 4.76k forks source link

UseWindowsService then run in windows service will crash. #65696

Closed MiyamuraMiyako closed 2 years ago

MiyamuraMiyako commented 2 years ago

Description

UseWindowsService() then run in windows service will crash.

Reproduction Steps

Just .UseWindowsService() and run in windows serivice will crash.

Process has exited, so the requested information is not available.
   at System.Diagnostics.Process.get_ProcessName()
   at Microsoft.Extensions.Hosting.WindowsServices.WindowsServiceHelpers.IsWindowsService()
   at Microsoft.Extensions.Hosting.WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder hostBuilder, Action`1 configure)
   at Microsoft.Extensions.Hosting.WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder hostBuilder)
   at Program.<Main>$(String[] args) in D:\path\to\my\code\Program.cs:line 54

Expected behavior

Everything Run ok.

Actual behavior

Service crash.

Regression?

No response

Known Workarounds

No response

Configuration

.NET Info:

C:\Users\zgxlt>dotnet --info
.NET SDK (反映任何 global.json):
 Version:   7.0.100-preview.1.22110.4
 Commit:    129d2465c8

运行时环境:
 OS Name:     Windows
 OS Version:  10.0.22000
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.100-preview.1.22110.4\

Host (useful for support):
  Version: 7.0.0-preview.1.22076.8
  Commit:  405337939c

.NET SDKs installed:
  5.0.404 [C:\Program Files\dotnet\sdk]
  5.0.405 [C:\Program Files\dotnet\sdk]
  6.0.102 [C:\Program Files\dotnet\sdk]
  6.0.200-preview.22055.15 [C:\Program Files\dotnet\sdk]
  7.0.100-preview.1.22110.4 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.0-preview.1.22109.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.0-preview.1.22076.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.0-preview.1.22077.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

package info: <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="7.0.0-preview.1.22076.8" />

Other information

No response

ghost commented 2 years ago

Tagging subscribers to this area: @dotnet/area-extensions-hosting See info in area-owners.md if you want to be subscribed.

Issue Details
### Description `UseWindowsService()` then run in windows service will crash. ### Reproduction Steps Just `.UseWindowsService()` and run in windows serivice will crash. ``` Process has exited, so the requested information is not available. at System.Diagnostics.Process.get_ProcessName() at Microsoft.Extensions.Hosting.WindowsServices.WindowsServiceHelpers.IsWindowsService() at Microsoft.Extensions.Hosting.WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder hostBuilder, Action`1 configure) at Microsoft.Extensions.Hosting.WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder hostBuilder) at Program.
$(String[] args) in D:\path\to\my\code\Program.cs:line 54 ``` ### Expected behavior Everything Run ok. ### Actual behavior Service crash. ### Regression? _No response_ ### Known Workarounds _No response_ ### Configuration .NET Info: ``` C:\Users\zgxlt>dotnet --info .NET SDK (反映任何 global.json): Version: 7.0.100-preview.1.22110.4 Commit: 129d2465c8 运行时环境: OS Name: Windows OS Version: 10.0.22000 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\7.0.100-preview.1.22110.4\ Host (useful for support): Version: 7.0.0-preview.1.22076.8 Commit: 405337939c .NET SDKs installed: 5.0.404 [C:\Program Files\dotnet\sdk] 5.0.405 [C:\Program Files\dotnet\sdk] 6.0.102 [C:\Program Files\dotnet\sdk] 6.0.200-preview.22055.15 [C:\Program Files\dotnet\sdk] 7.0.100-preview.1.22110.4 [C:\Program Files\dotnet\sdk] .NET runtimes installed: Microsoft.AspNetCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.0-preview.1.22109.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.0-preview.1.22076.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.0-preview.1.22077.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] ``` package info: `` ### Other information _No response_
Author: MiyamuraMiyako
Assignees: -
Labels: `untriaged`, `area-Extensions-Hosting`
Milestone: -
MiyamuraMiyako commented 2 years ago

WinDbg infomation below:

0:000> !analyze -v
*******************************************************************************
*                                                                             *
*                        Exception Analysis                                   *
*                                                                             *
*******************************************************************************

DEBUG_FLR_EXCEPTION_CODE(80131509) and the ".exr -1" ExceptionCode(e0434352) don't match
MethodDesc:   00007ffc7f72bb50
Method Name:          Program.<Main>$(System.String[])
Class:                00007ffc7f7508e0
MethodTable:          00007ffc7f72bb78
mdToken:              0000000006000017
Module:               00007ffc7f729168
IsJitted:             yes
Current CodeAddr:     00007ffc7f6695c0
Version History:
  ILCodeVersion:      0000000000000000
  ReJIT ID:           0
  IL Addr:            000001edef6723d4
     CodeAddr:           00007ffc7f6695c0  (MinOptJitted)
     NativeCodeVersion:  0000000000000000
Failed to request MethodData, not in JIT code range
Failed to request MethodData, not in JIT code range
Failed to request MethodData, not in JIT code range
Failed to request MethodData, not in JIT code range

KEY_VALUES_STRING: 1

    Key  : Analysis.CPU.mSec
    Value: 2359

    Key  : Analysis.DebugAnalysisManager
    Value: Create

    Key  : Analysis.Elapsed.mSec
    Value: 4174

    Key  : Analysis.Init.CPU.mSec
    Value: 327

    Key  : Analysis.Init.Elapsed.mSec
    Value: 344402

    Key  : Analysis.Memory.CommitPeak.Mb
    Value: 148

    Key  : CLR.Engine
    Value: CORECLR

    Key  : CLR.Version
    Value: 7.0.22.7608

    Key  : Timeline.OS.Boot.DeltaSec
    Value: 7610954

    Key  : Timeline.Process.Start.DeltaSec
    Value: 2

    Key  : WER.OS.Branch
    Value: rs5_release

    Key  : WER.OS.Timestamp
    Value: 2018-09-14T14:34:00Z

    Key  : WER.OS.Version
    Value: 10.0.17763.1

    Key  : WER.Process.Version
    Value: 1.0.0.0

FILE_IN_CAB:  SayedKioskHub.exe.16044.dmp

NTGLOBALFLAG:  0

PROCESS_BAM_CURRENT_THROTTLED: 0

PROCESS_BAM_PREVIOUS_THROTTLED: 0

APPLICATION_VERIFIER_FLAGS:  0

CONTEXT:  (.ecxr)
rax=00007ffc7f6fe6c0 rbx=0000005dfb37e1e0 rcx=0000000000000001
rdx=0000000000000000 rsi=0000000000000001 rdi=0000005dfb37b2d8
rip=00007ffd11929149 rsp=0000005dfb37b160 rbp=0000005dfb37e950
 r8=0000000000000005  r9=000001f27b3498d0 r10=00007ffc00000006
r11=0000005dfb37a498 r12=0000000000004000 r13=000001edef4a2370
r14=0000000000000005 r15=00000000e0434352
iopl=0         nv up ei pl nz na pe nc
cs=0033  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000202
KERNELBASE!RaiseException+0x69:
00007ffd`11929149 0f1f440000      nop     dword ptr [rax+rax]
Resetting default scope

EXCEPTION_RECORD:  (.exr -1)
ExceptionAddress: 00007ffd11929149 (KERNELBASE!RaiseException+0x0000000000000069)
   ExceptionCode: e0434352 (CLR exception)
  ExceptionFlags: 00000001
NumberParameters: 5
   Parameter[0]: ffffffff80131509
   Parameter[1]: 0000000000000000
   Parameter[2]: 0000000000000000
   Parameter[3]: 0000000000000000
   Parameter[4]: 00007ffcdf0d0000

PROCESS_NAME:  SayedKioskHub.exe

EXCEPTION_CODE_STR:  80131509

FAULTING_THREAD:  ffffffff

STACK_TEXT:  
0000005d`fb37b480 00007ffc`7f66b49e SayedKioskHub!Program.<Main>$+0x1ede

STACK_COMMAND:  ** Pseudo Context ** ManagedPseudo ** Value: ffffffff ** ; kb

SYMBOL_NAME:  SayedKioskHub!Program.<Main>$+1ede

MODULE_NAME: SayedKioskHub

IMAGE_NAME:  SayedKioskHub.dll

FAILURE_BUCKET_ID:  CLR_EXCEPTION_80131509_SayedKioskHub.dll!Program._Main_$

OS_VERSION:  10.0.17763.1

BUILDLAB_STR:  rs5_release

OSPLATFORM_TYPE:  x64

OSNAME:  Windows 10

IMAGE_VERSION:  1.0.0.0

FAILURE_ID_HASH:  {24c420db-c0ba-269a-01a0-06a6cb9aa596}

Followup:     MachineOwner
---------

0:000> !sos.pe
Exception object: 000001ef0f830d88
Exception type:   System.InvalidOperationException
Message:          Process has exited, so the requested information is not available.
InnerException:   <none>
StackTrace (generated):
    SP               IP               Function
    0000005DFB37E070 00007FFCE050D4AC System_Diagnostics_Process!System.Diagnostics.Process.get_ProcessName()+0xbc
    0000005DFB37E0B0 00007FFC7F9FFB2A Microsoft_Extensions_Hosting_WindowsServices!Microsoft.Extensions.Hosting.WindowsServices.WindowsServiceHelpers.IsWindowsService()+0x6a
    0000005DFB37E100 00007FFC7F9FF46A Microsoft_Extensions_Hosting_WindowsServices!Microsoft.Extensions.Hosting.WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(Microsoft.Extensions.Hosting.IHostBuilder, System.Action`1<Microsoft.Extensions.Hosting.WindowsServiceLifetimeOptions>)+0x6a
    0000005DFB37E180 00007FFC7F9FF33D Microsoft_Extensions_Hosting_WindowsServices!Microsoft.Extensions.Hosting.WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(Microsoft.Extensions.Hosting.IHostBuilder)+0xed
    0000005DFB37E1E0 00007FFC7F669801 SayedKioskHub!Program.<Main>$(System.String[])+0x241

StackTraceString: <none>
HResult: 80131509
There are nested exceptions on this thread. Run with -nested for details
am11 commented 2 years ago

Regression?

No response

Can you confirm if it is working with .NET 6? If it only fails with 7 preview 1, then the exception in function System_Diagnostics_Process!System.Diagnostics.Process.get_ProcessName()+0xbc could be related to this recent change: https://github.com/dotnet/runtime/commit/a2d61d96102902a1ebb0bb1c6996a6fd89e123c2.

cc @adamsitnik

MiyamuraMiyako commented 2 years ago

@am11 Yes, same code working in .NET6, I just change TargetFramework to net7.0 and upgrade some packages.

am11 commented 2 years ago

@MiyamuraMiyako, thanks. I wasn't able to reproduce the issue on Windows 10 with latest (daily builds) package:

    <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="7.0.0-preview.2*" />
:: dotnet 7 daily build downloaded from: https://github.com/dotnet/installer#table
:: main (7.0.x Runtime) column

> dotnet --info
.NET SDK (reflecting any global.json):
 Version:   7.0.100-preview.3.22122.1
 Commit:    5567d09a75

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19044
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Users\am11\.dotnet7\sdk\7.0.100-preview.3.22122.1\

Host (useful for support):
  Version: 7.0.0-preview.2.22116.9
  Commit:  793185a239

.NET SDKs installed:
  5.0.404 [C:\Program Files\dotnet\sdk]
  6.0.101 [C:\Program Files\dotnet\sdk]
  6.0.102 [C:\Program Files\dotnet\sdk]
  7.0.100-preview.3.22122.1 [C:\Users\am11\.dotnet7\sdk]

...
MiyamuraMiyako commented 2 years ago

@am11 Is this mean I need wait .NET7 preview2 publish?

am11 commented 2 years ago

@MiyamuraMiyako, can you confirm it with daily build? Extract the daily build zip archive https://aka.ms/dotnet/7.0.1xx/daily/dotnet-sdk-win-x64.zip in a directory, for example, %UserProfile%\.dotnet7 and then run your application with it: %UserProfile%\.dotnet7\dotnet run.

If it is fixed, then you can continue to use this daily build until the next preview is published.

Otherwise, please share an SSCCE that demonstrates the issue. Thank you!