From 80d532abd755f6fcc9e73d7b24adf1d40272b070 Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Wed, 28 Feb 2024 17:04:34 +0100 Subject: [PATCH] avoid forgetting to quit in `os_ops` tools if log fails (#5986) `stderr.write` may fail, e.g., if no tty is connected, which may happen in some CI configurations. Discard such failures and continue quitting instead of raising the error. --- tests/consensus_spec/os_ops.nim | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/tests/consensus_spec/os_ops.nim b/tests/consensus_spec/os_ops.nim index e722102f5..ce0b13dfe 100644 --- a/tests/consensus_spec/os_ops.nim +++ b/tests/consensus_spec/os_ops.nim @@ -1,3 +1,12 @@ +# beacon_chain +# Copyright (c) 2023-2024 Status Research & Development GmbH +# Licensed and distributed under either of +# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). +# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). +# at your option. This file may not be copied, modified, or distributed except according to those terms. + +{.push raises: [].} + import std/os import stew/io2 @@ -13,8 +22,11 @@ proc readFile*(filename: string): string = let res = io2.readAllChars(filename) if res.isErr(): writeStackTrace() - stderr.write "Could not load data from file \"", filename, "\"\n" - stderr.write "(" & $int(res.error()) & ") " & ioErrorMsg(res.error()), "\n" + try: + stderr.write "Could not load data from file \"", filename, "\"\n" + stderr.write "(" & $int(res.error()) & ") ", ioErrorMsg(res.error()), "\n" + except IOError: + discard quit 1 res.get() @@ -25,7 +37,10 @@ proc readFileBytes*(path: string): seq[byte] = let res = io2.readAllBytes(path) if res.isErr(): writeStackTrace() - stderr.write "Could not load data from file \"", path, "\"\n" - stderr.write "(" & $int(res.error()) & ") " & ioErrorMsg(res.error()), "\n" + try: + stderr.write "Could not load data from file \"", path, "\"\n" + stderr.write "(" & $int(res.error()) & ") ", ioErrorMsg(res.error()), "\n" + except IOError: + discard quit 1 res.get()