Skip to content

Unbuffered I/O breaks some valid usages #7836

@nertpinx

Description

@nertpinx

fish version: 3.2.0
uname: Linux cjohnson 5.11.6-gentoo-x86_64 #1 SMP Thu Mar 18 10:26:07 CET 2021 x86_64 Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz GenuineIntel GNU/Linux
TERM: xterm-kitty

Builtin echo function (and probably other built-ins as well since 616d301) are using unbuffered writes to stdout even when the output is redirected to a file. This causes issue when writing to kernel interface files where each write is consumed separately.

Example of the behaviour:

# strace -P /sys/class/power_supply/BAT0/charge_control_end_threshold -e write --signal !SIGCHLD fish -c 'builtin echo 100 >/sys/class/power_supply/BAT0/charge_control_end_threshold'
strace: Requested path "/sys/class/power_supply/BAT0/charge_control_end_threshold" resolved into "/sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:19/PNP0C09:00/PNP0C0A:00/power_supply/BAT0/charge_control_end_threshold"
write(4, "1", 1)                        = 1
write(4, "0", 1)                        = -1 EINVAL (Invalid argument)
write: Invalid argument
+++ exited with 0 +++
# cat /sys/class/power_supply/BAT0/charge_control_end_threshold
1

Yes, the other issue is that the command does not fail and ends with 0. Let me know if I need to file another issue for that. In order to make this work (and charge my battery to a percentage higher than one digit numbers) it is enough to use /bin/echo, bash, echo 100 | cat >/... or basically any other solution. The issue is not that this would not be possible to do otherwise, but it is completely unclear for the user to see what is happening.

The change to unbuffered I/O was done because of #3748. Even though I could argue that it makes the shell behave unexpectedly and that issue could've been fixed differently, I think it would be enough if the writes were buffered when being redirected as that could, probably, deal with most issue possibly arising from this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething that's not working as intended

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions