Skip to content

Commit f272f86

Browse files
authored
Merge pull request #1154 from SteveL-MSFT/broken-pipe
Fix crash when pipe is closed by shell
2 parents 548fa97 + d589dea commit f272f86

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

dsc/src/util.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use schemars::{Schema, schema_for};
4747
use serde::Deserialize;
4848
use std::collections::HashMap;
4949
use std::env;
50-
use std::io::{IsTerminal, Read};
50+
use std::io::{IsTerminal, Read, stdout, Write};
5151
use std::path::Path;
5252
use std::process::exit;
5353
use syntect::{
@@ -293,7 +293,11 @@ pub fn write_object(json: &str, format: Option<&OutputFormat>, include_separator
293293
}
294294
}
295295
else {
296-
println!("{output}");
296+
let mut stdout_lock = stdout().lock();
297+
if writeln!(stdout_lock, "{output}").is_err() {
298+
// likely caused by a broken pipe (e.g. 'head' command closed early)
299+
exit(EXIT_SUCCESS);
300+
}
297301
}
298302
}
299303

dsc/tests/dsc.exit_code.tests.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ Describe 'exit code tests' {
1515
$result.actualState.exitCode | Should -Be 0
1616
$LASTEXITCODE | Should -Be 0
1717
}
18+
It 'Exiting early due to broken pipe is a success' {
19+
$out = dsc resource list | Select-Object -First 1 | ConvertFrom-Json
20+
$out.Count | Should -Be 1
21+
$LASTEXITCODE | Should -Be 0
22+
}
1823
}

0 commit comments

Comments
 (0)