Skip to content

Write-Host statements, file redirections in a finally block intermittently fail quietly on Windows when Ctrl+C is used to terminate a Read-Host call #19987

@mklement0

Description

@mklement0

Prerequisites

Steps to reproduce

The problem isn't easy to reproduce, and I see it on Windows only - it may be specific to aborting a Read-Host call.
It may take several tries, suggesting some non-deterministic timing is at play, relating to when the finally block executes in relation to stopping the script.

Related: #19988

# Save to a file and run on *Windows*:
# Repeatedly run the following and press Ctrl-C when prompted.
# Then make sure that:
#   (a) 'Aborted.' prints
#   and (b) that file temp:/$PID.txt exists.
Remove-Item -ErrorAction Ignore temp:/$PID.txt
try {
  Read-Host 'Press Ctrl-C now'
}
finally {
  Write-Host "Aborted."
  Get-Date > temp:/$PID.txt
}

Expected behavior

Aborted. should print.
Running Get-Content temp:/$PID.txt afterwards should print the timestamp when Ctrl-C was pressed.

Actual behavior

Intermittently, Aborted. doesn't print.
More often, file temp:/$PID.txt isn't created.

The failure is always quiet.

Error details

No response

Environment data

PowerShell 7.4.0-preview.4 on Windows 11 22H2 (ARM64)

Visuals

No response

Metadata

Metadata

Assignees

Labels

KeepOpenThe bot will ignore these and not auto-closeNeeds-InvestigationThe behavior reported in the issue is unexpected and needs further investigation.WG-Enginecore PowerShell engine, interpreter, and runtime

Type

No type

Projects

Status

Issue Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions