Skip to content

Write-PodeErrorLog` Called Before Log Subsystem Initialization in Console Setup #1514

@mdaneri

Description

@mdaneri

Description:
In the current Pode implementation, the following block of code attempts to invoke Write-PodeErrorLog inside a catch block if an exception occurs:

    try {
        if (! (Test-PodeIsISEHost)) {
            if (!$ctx.Server.Console.Quiet) {
                [System.Console]::CursorVisible = $false

                if ($ctx.Server.Console.ShowDivider) {
                    [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
                }
            }
            if (Test-PodeIsConsoleHost) {
                [Console]::TreatControlCAsInput = $true
            }
        }
    }
    catch {
        $_ | Write-PodeErrorLog
        $ctx.Server.Console.DisableTermination = $true
        $ctx.Server.Console.DisableConsoleInput = $true
        $ctx.Server.Console.Quiet = $true
        $ctx.Server.Console.ShowDivider = $false
    }

However, Write-PodeErrorLog is being called before the logging subsystem is fully initialized. This creates compatibility issues with the new logging features introduced in PR [#1387](#1387).

Expected Behavior

  • The console initialization logic should handle errors gracefully without invoking Write-PodeErrorLog prematurely.
  • The context should be configured for non-console behavior in case of failure, without relying on logging at this stage.

Proposed Fix

A simple fix is to check if the session is running in a console and is not marked as a daemon before proceeding. Additionally, we should remove Write-PodeErrorLog from this section:

    # Check if the current session is running in a console-like environment and it's not marked as Daemon
    if (Test-PodeHasConsole -and ! $Daemon) {
        try {
            if (! (Test-PodeIsISEHost)) {
                if (!$ctx.Server.Console.Quiet) {
                    [System.Console]::CursorVisible = $false

                    if ($ctx.Server.Console.ShowDivider) {
                        [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
                    }
                }
                if (Test-PodeIsConsoleHost) {
                    [Console]::TreatControlCAsInput = $true
                }
            }
        }
        catch {
            $ctx.Server.Console.DisableTermination = $true
            $ctx.Server.Console.DisableConsoleInput = $true
            $ctx.Server.Console.Quiet = $true
            $ctx.Server.Console.ShowDivider = $false
        }
    }

Steps to Reproduce

  1. Start Pode in a separated pwsh in background
  2. An exception is trigegred in the try block.
  3. Observe that Write-PodeErrorLog is invoked before the logging subsystem is initialized.

Additional Notes

Removing Write-PodeErrorLog from this specific section should prevent any early logging issues while ensuring the console context is handled correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions