< Summary - Kestrun — Combined Coverage

Information
Class: Public.Server.Stop-KrServer
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Server/Stop-KrServer.ps1
Tag: Kestrun/Kestrun@5f1d2b981c9d7292c11fd448428c6ab6c811c5de
Line coverage
0%
Covered lines: 0
Uncovered lines: 24
Coverable lines: 24
Total lines: 83
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 11/19/2025 - 17:40:50 Line coverage: 0% (0/21) Total lines: 78 Tag: Kestrun/Kestrun@fcf33342333cef0516fe0d0912a86709874fd02603/26/2026 - 03:54:59 Line coverage: 0% (0/24) Total lines: 83 Tag: Kestrun/Kestrun@844b5179fb0492dc6b1182bae3ff65fa7365521d

Coverage delta

Coverage delta 1 -1

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Server/Stop-KrServer.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Starts the Kestrun server and listens for incoming requests.
 4    .DESCRIPTION
 5        This function starts the Kestrun server, allowing it to accept incoming HTTP requests.
 6    .PARAMETER Server
 7        The Kestrun server instance to start. This parameter is mandatory.
 8    .PARAMETER NoWait
 9        If specified, the function will not wait for the server to start and will return immediately.
 10    .PARAMETER Quiet
 11        If specified, suppresses output messages during the startup process.
 12    .EXAMPLE
 13        Start-KrServer -Server $server
 14        Starts the specified Kestrun server instance and listens for incoming requests.
 15    .NOTES
 16        This function is designed to be used after the server has been configured and routes have been added.
 17        It will block the console until the server is stopped or Ctrl+C is pressed.
 18#>
 19function Stop-KrServer {
 20    [KestrunRuntimeApi('Everywhere')]
 21    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
 22    [CmdletBinding()]
 23    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '')]
 24    param(
 25        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 26        [Kestrun.Hosting.KestrunHost]$Server,
 27        [Parameter()]
 28        [switch]$NoWait,
 29        [Parameter()]
 30        [switch]$Quiet
 31    )
 32    begin {
 033        $effectiveQuiet = $Quiet.IsPresent
 034        if (-not $PSBoundParameters.ContainsKey('Quiet')) {
 035            $effectiveQuiet = [bool]$ExecutionContext.SessionState.PSVariable.GetValue('__krRunnerQuiet', $false)
 36        }
 37
 38        # Ensure the server instance is resolved
 039        $Server = Resolve-KestrunServer -Server $Server
 040        $writeConsole = $false
 41        try {
 042            $null = [Console]::KeyAvailable
 043            $writeConsole = -not $effectiveQuiet
 44        } catch {
 045            Write-KrLog -Level Information -Message 'No console available; running in non-interactive mode.'
 046            $writeConsole = $false
 47        }
 48    }
 49    process {
 050        if ($null -ne $Context -and $null -ne $Context.Response) {
 51            # If called within a route, send a response before stopping
 052            Write-KrTextResponse -InputObject 'Server is stopping...' -StatusCode 202
 053            Start-Sleep -Seconds 1
 054            $Server.StopAsync() | Out-Null
 55            return
 56        }
 57        # Stop the Kestrel server
 058        $Server.StopAsync() | Out-Null
 59        # Ensure the server is stopped on exit
 060        if ($writeConsole) {
 061            Write-Host 'Stopping Kestrun server...' -NoNewline
 62        }
 63
 64        # If NoWait is specified, return immediately
 065        if ($NoWait.IsPresent) {
 66            return
 67        }
 68
 069        while ($Server.IsRunning) {
 070            Start-Sleep -Seconds 1
 071            if ($writeConsole) {
 072                Write-Host '#' -NoNewline
 73            }
 74        }
 75
 076        [Kestrun.KestrunHostManager]::Destroy($Server.ApplicationName)
 77
 078        if ($writeConsole) {
 079            Write-Host 'Kestrun server stopped.'
 80        }
 81    }
 82}
 83

Methods/Properties

Stop-KrServer()