Skip to content
Snippets Groups Projects
Commit 6843de89 authored by q3k's avatar q3k
Browse files

mpy: prevent garbage collection from closing stdin

Somwhere in Micropython there's a `<io.TextIOWrapper 0>` from
vfs_posix_file that's getting garbage collected and which in turn
triggers the object's __del__ which then in turn attempt to close FD 0.

Let's not allow that to happen by adding some ugly hardcoded deny list
for FD 0,1,2 in the close function.

Ideally, we would figure out why that object exists, why it gets garbage
collect and what really should happen here. But this is good enough.
parent cc809d99
No related branches found
No related tags found
No related merge requests found
Pipeline #6035 passed
......@@ -187,7 +187,18 @@ STATIC mp_uint_t vfs_posix_file_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_
case MP_STREAM_CLOSE:
if (o->fd >= 0) {
MP_THREAD_GIL_EXIT();
close(o->fd);
switch (o->fd) {
case 0:
case 1:
case 2:
// Don't let anything close stdin/stdout/stderr.
// BUG: we shouldn't even get to this point of anything
// attempting to close these. But that's for someone else to
// debug.
break;
default:
close(o->fd);
}
MP_THREAD_GIL_ENTER();
}
o->fd = -1;
......@@ -249,4 +260,4 @@ MP_DEFINE_CONST_OBJ_TYPE(
locals_dict, &vfs_posix_rawfile_locals_dict
);
#endif // MICROPY_VFS_POSIX
\ No newline at end of file
#endif // MICROPY_VFS_POSIX
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment