modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.26k stars 2.59k forks source link

[BUG] Opening a unix fifo in "write" mode raises an exception #3208

Open diocletiann opened 4 months ago

diocletiann commented 4 months ago

Bug description

I'm not sure why this is failing, mentioned it on Discord and was asked to open an issue:

$ mojo run src/main.mojo
Unhandled exception caught during execution: unable to remove existing file /tmp/mojo.fifo
mojo: error: execution exited with a non-zero result: 1

This is the pipe:

$ ll /tmp/mojo.fifo
prwxrwxrwx  1 user  wheel     0B Jul  9 14:12 /tmp/mojo.fifo|

Steps to reproduce

$ mkfifo -m 777 /tmp/mojo.fifo
fn main() raises:
    var handle = open("/tmp/mojo.fifo", "w")

System information

MacOS 14.3.1
mojo 2024.7.905 (e860c551)
modular 0.8.0 (39a426b5)
ematejska commented 4 months ago

Looks like we're attempting to clear the the file before writing. This reproduces if the file is in a location that's owned by root but doesn't reproduce if the file is in a location owned by the user.

diocletiann commented 4 months ago

Looks like we're attempting to clear the the file before writing. This reproduces if the file is in a location that's owned by root but doesn't reproduce if the file is in a location owned by the user.

Well even in a location owned by the user, I tried open() with a text file and it deleted the file before it could even be read.

ematejska commented 4 months ago

Does that happen when you open it for reading with "r". I would expect that to happen when it's open for writing because the file is cleared out before writing happens….I believe that's how it works in Python as well.

diocletiann commented 4 months ago

"r" opens the file without data loss, sounds like an "rw" option is needed.