From 7fa0bfe45fa5ec60a805b47a044a78fdfc77bade Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Mon, 15 Dec 2025 15:56:38 +0100 Subject: [PATCH 1/2] Return 'null' when trying to read from closed pipe. --- src/pipe.toit | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pipe.toit b/src/pipe.toit index d682d50..1be4604 100644 --- a/src/pipe.toit +++ b/src/pipe.toit @@ -98,7 +98,7 @@ class OpenPipe extends OpenPipe_: class OpenPipe_ implements Stream: resource_ := ? - state_ := ? + state_/monitor.ResourceState_? := ? pid := null child-process-name_ /string? input_ /int := UNKNOWN-DIRECTION_ @@ -153,6 +153,7 @@ class OpenPipe_ implements Stream: read_ -> ByteArray?: while true: state_.wait-for-state READ-EVENT_ | CLOSE-EVENT_ + if not state_: return null result := read-from-pipe_ resource_ if result != -1: if result == null: From 8b5d1395d5c5aa3027c5a8f73f97e03aab0f60c5 Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Mon, 15 Dec 2025 16:06:56 +0100 Subject: [PATCH 2/2] Add test. --- src/pipe.toit | 1 + tests/pipe3_exe.toit | 9 +++++++++ tests/pipe3_test.toit | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 tests/pipe3_exe.toit create mode 100644 tests/pipe3_test.toit diff --git a/src/pipe.toit b/src/pipe.toit index 1be4604..ad6fe9b 100644 --- a/src/pipe.toit +++ b/src/pipe.toit @@ -152,6 +152,7 @@ class OpenPipe_ implements Stream: read_ -> ByteArray?: while true: + if not state_: return null state_.wait-for-state READ-EVENT_ | CLOSE-EVENT_ if not state_: return null result := read-from-pipe_ resource_ diff --git a/tests/pipe3_exe.toit b/tests/pipe3_exe.toit new file mode 100644 index 0000000..28a721b --- /dev/null +++ b/tests/pipe3_exe.toit @@ -0,0 +1,9 @@ +// Copyright (C) 2025 Toit contributors. +// Use of this source code is governed by a Zero-Clause BSD license that can +// be found in the tests/TESTS_LICENSE file. + +import host.pipe + +main: + // Just wait for a message. + pipe.stdin.in.read diff --git a/tests/pipe3_test.toit b/tests/pipe3_test.toit new file mode 100644 index 0000000..3eb7fff --- /dev/null +++ b/tests/pipe3_test.toit @@ -0,0 +1,32 @@ +// Copyright (C) 2025 Toit contributors. +// Use of this source code is governed by a Zero-Clause BSD license that can +// be found in the tests/TESTS_LICENSE file. + +import expect show * + +import host.pipe +import system + +main args: + toit-exe := args[0] + + my-path := system.program-path + end := my-path.index-of --last "test.toit" + if end == -1: throw "UNEXPECTED" + input := "$my-path[..end]input.toit" + + process := pipe.fork + --create-stdout + --create-stdin + toit-exe + [toit-exe, input] + + to-process := process.stdin + from-process := process.stdout + + from-process.close + + expect-null from-process.in.read + to-process.out.write "done" + + process.wait