From b9eb22b7cdb7c52b87848c486214012aabf8cf96 Mon Sep 17 00:00:00 2001 From: Lukas Larsson Date: Thu, 19 Dec 2013 14:10:28 +0100 Subject: erts: Make sure fds 0,1 and 2 are open If they are not open a really nasty race where the io pipe in erl_poll got fd 0 and the fd_driver setting fd 0 to blocking could occur. This caused the emulator to hang during shutdown. --- erts/emulator/sys/unix/sys.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/erts/emulator/sys/unix/sys.c b/erts/emulator/sys/unix/sys.c index a7ea4b2490..77d7154313 100644 --- a/erts/emulator/sys/unix/sys.c +++ b/erts/emulator/sys/unix/sys.c @@ -547,6 +547,25 @@ erts_sys_pre_init(void) #endif #endif /* USE_THREADS */ erts_smp_atomic_init_nob(&sys_misc_mem_sz, 0); + + { + /* + * Unfortunately we depend on fd 0,1,2 in the old shell code. + * So if for some reason we do not have those open when we start + * we have to open them here. Not doing this can cause the emulator + * to deadlock when reaping the fd_driver ports :( + */ + int fd; + /* Make sure fd 0 is open */ + if ((fd = open("/dev/null", O_RDONLY)) != 0) + close(fd); + /* Make sure fds 1 and 2 are open */ + while (fd < 3) { + fd = open("/dev/null", O_WRONLY); + } + close(fd); + } + } void -- cgit v1.2.3