serhiy-storchaka · GitHub

@serhiy-storchaka

Bug report

os.posix_fadvise() and os.posix_fallocate() raise OSError with a meaningless error code on DragonFly BSD:

>>> os.posix_fadvise(-42, 0, 0, os.POSIX_FADV_WILLNEED)
OSError: [Errno -1] Unknown error: -1
>>> os.posix_fallocate(-42, 0, 10)
OSError: [Errno -1] Unknown error: -1

It should be EBADF.

POSIX specifies that these functions return the error number and do not set errno, and this is what the manual page on DragonFly BSD says too:

If successful, posix_fallocate() returns zero. It returns an error on failure, without setting errno.

But the implementation returns -1 and sets errno:

posix_fadvise(-42)   ret=-1 errno=9 (Bad file descriptor)
posix_fallocate(-42) ret=-1 errno=9 (Bad file descriptor)

We use the return value since gh-73528, where the opposite problem was reported: on musl the error was taken from errno, which was not set. Using -1 as an error number produces the above, and it also breaks retrying on EINTR, because the returned value is compared with EINTR.

Both conventions can be supported by using errno when the function returns -1.

Linked PRs

Read the original on github.com ↗