Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mdaneri opened this issue Mar 4, 2025 · 0 comments
Labels

Comments

@mdaneri
Copy link
Contributor

mdaneri commented Mar 4, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Backlog
Development

No branches or pull requests

1 participant