From 2170b20fd3a3ce7dd8ae0976e1060cef519c6aa9 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL HE/HIM) (from Dev Box)" Date: Thu, 2 Oct 2025 17:56:43 -0700 Subject: [PATCH 1/2] Fix crash when pipe is closed by shell --- dsc/src/util.rs | 8 ++++++-- dsc/tests/dsc.exit_code.tests.ps1 | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/dsc/src/util.rs b/dsc/src/util.rs index f2504b8a0..00938ecc8 100644 --- a/dsc/src/util.rs +++ b/dsc/src/util.rs @@ -47,7 +47,7 @@ use schemars::{Schema, schema_for}; use serde::Deserialize; use std::collections::HashMap; use std::env; -use std::io::{IsTerminal, Read}; +use std::io::{IsTerminal, Read, stdout, Write}; use std::path::Path; use std::process::exit; use syntect::{ @@ -293,7 +293,11 @@ pub fn write_object(json: &str, format: Option<&OutputFormat>, include_separator } } else { - println!("{output}"); + let mut lock = stdout().lock(); + if writeln!(lock, "{output}").is_err() { + // likely caused by a broken pipe (e.g. 'head' command closed early) + exit(EXIT_SUCCESS); + } } } diff --git a/dsc/tests/dsc.exit_code.tests.ps1 b/dsc/tests/dsc.exit_code.tests.ps1 index 16c27905b..10f5a676a 100644 --- a/dsc/tests/dsc.exit_code.tests.ps1 +++ b/dsc/tests/dsc.exit_code.tests.ps1 @@ -15,4 +15,9 @@ Describe 'exit code tests' { $result.actualState.exitCode | Should -Be 0 $LASTEXITCODE | Should -Be 0 } + It 'Exiting early due to broken pipe is a success' { + $out = dsc resource list | Select-Object -First 1 | ConvertFrom-Json + $out.Count | Should -Be 1 + $LASTEXITCODE | Should -Be 0 + } } From d589dea97e8299a9400198c02d69035613483e9c Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 2 Oct 2025 18:09:59 -0700 Subject: [PATCH 2/2] Update dsc/src/util.rs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- dsc/src/util.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dsc/src/util.rs b/dsc/src/util.rs index 00938ecc8..cc2c4d064 100644 --- a/dsc/src/util.rs +++ b/dsc/src/util.rs @@ -293,8 +293,8 @@ pub fn write_object(json: &str, format: Option<&OutputFormat>, include_separator } } else { - let mut lock = stdout().lock(); - if writeln!(lock, "{output}").is_err() { + let mut stdout_lock = stdout().lock(); + if writeln!(stdout_lock, "{output}").is_err() { // likely caused by a broken pipe (e.g. 'head' command closed early) exit(EXIT_SUCCESS); }