From 68b1bc2042f8e5dcdfbe12c5b0dace0b174f6911 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 18 Mar 2020 17:43:00 +1100 Subject: [PATCH] extmod/vfs_posix_file: Lock GIL when writing and allow stdio flush. Also support MP_STREAM_GET_FILENO ioctl. The stdio flush change was done previously for the unix port in 3e0b46b9af8b7a6934b7ac68671ef8f55be2b848. These changes make this POSIX file implementation equivalent to the unix file implementation. --- extmod/vfs_posix_file.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/extmod/vfs_posix_file.c b/extmod/vfs_posix_file.c index b49251f035..8ac6f975d0 100644 --- a/extmod/vfs_posix_file.c +++ b/extmod/vfs_posix_file.c @@ -157,7 +157,9 @@ STATIC mp_uint_t vfs_posix_file_write(mp_obj_t o_in, const void *buf, mp_uint_t return size; } #endif + MP_THREAD_GIL_EXIT(); mp_int_t r = write(o->fd, buf, size); + MP_THREAD_GIL_ENTER(); while (r == -1 && errno == EINTR) { if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) { mp_obj_t obj = MP_STATE_VM(mp_pending_exception); @@ -184,6 +186,13 @@ STATIC mp_uint_t vfs_posix_file_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_ int ret = fsync(o->fd); MP_THREAD_GIL_ENTER(); if (ret == -1) { + if (errno == EINVAL + && (o->fd == STDIN_FILENO || o->fd == STDOUT_FILENO || o->fd == STDERR_FILENO)) { + // fsync(stdin/stdout/stderr) may fail with EINVAL, but don't propagate that + // error out. Because data is not buffered by us, and stdin/out/err.flush() + // should just be a no-op. + return 0; + } *errcode = errno; return MP_STREAM_ERROR; } @@ -208,6 +217,8 @@ STATIC mp_uint_t vfs_posix_file_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_ o->fd = -1; #endif return 0; + case MP_STREAM_GET_FILENO: + return o->fd; default: *errcode = EINVAL; return MP_STREAM_ERROR;