Skip to content

wren-cli should fflush(0) before returning with a successful exit code #56

@guenther-brunthaler

Description

@guenther-brunthaler

I would like to use wren-cli for writing real scripts that work reliably, not just toying around for learning the languages and trying out things.

Unfortunately, wren-cli is not reliable yet because it fails to detect write errors in certain situations. And reliable programs must catch at least input/output errors!

In order to demonstrate the problem, compare

$ echo 'System.print("hello")' > test.wren
$ wren0 test.wren > /dev/full

versus

$ echo puts hello | tclsh > /dev/full
error writing "stdout": no space left on device

wren should also display an error message in this case, and could even surpass tclsh by also returning EXIT_FAILURE in this case, allowing a script invoking wren-cli to detect the failure and react accordingly.

The reason why wren-cli currently misses this error is because it obviously does not call fflush(0) and check its success before exiting.

The problem are the C standard I/O functions which normally work buffered. Which means a printf() might be successful, even tough it did not write anything yet. It only wrote to the internal buffer.

When main() returns, the C runtime automatically calls fflush(0) internally, which tries to write out the contents all yet-unwritten buffers. This fails, but at this time main() has already exited and cannot detect the error any more. It is implementation-defined what the C runtime does in this situation. Under Linux obviously it ignores the error and returns as if everything was fine.

But if wren called fflush() itself before exit() or returning from main(), it had a chance to catch the write error and to report it or throw an exception.

I would very much appreciate if wren-cli would do this, because then it would be possible to write scripts that catch all output errors - and not just most.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions