Skip to content

Commit 11bc21d

Browse files
committed
unix/main: Ignore SIGPIPE signal, instead make EPIPE arrive.
Do not raise SIGPIPE, instead return EPIPE. Otherwise, e.g. writing to peer-closed socket will lead to sudden termination of MicroPython process. SIGPIPE is particularly nasty, because unix shell doesn't print anything for it, so the above looks like completely sudden and silent termination for unknown reason. Ignoring SIGPIPE is also what CPython does. Note that this may lead to problems using MicroPython scripts as pipe filters, but again, that's what CPython does. So, scripts which want to follow unix shell pipe semantics (where SIGPIPE means "pipe was requested to terminate, it's not an error"), should catch EPIPE themselves.
1 parent 5db55e6 commit 11bc21d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

unix/main.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <sys/stat.h>
3636
#include <sys/types.h>
3737
#include <errno.h>
38+
#include <signal.h>
3839

3940
#include "py/mpstate.h"
4041
#include "py/nlr.h"
@@ -414,6 +415,20 @@ int main(int argc, char **argv) {
414415
}
415416

416417
MP_NOINLINE int main_(int argc, char **argv) {
418+
#ifdef SIGPIPE
419+
// Do not raise SIGPIPE, instead return EPIPE. Otherwise, e.g. writing
420+
// to peer-closed socket will lead to sudden termination of MicroPython
421+
// process. SIGPIPE is particularly nasty, because unix shell doesn't
422+
// print anything for it, so the above looks like completely sudden and
423+
// silent termination for unknown reason. Ignoring SIGPIPE is also what
424+
// CPython does. Note that this may lead to problems using MicroPython
425+
// scripts as pipe filters, but again, that's what CPython does. So,
426+
// scripts which want to follow unix shell pipe semantics (where SIGPIPE
427+
// means "pipe was requested to terminate, it's not an error"), should
428+
// catch EPIPE themselves.
429+
signal(SIGPIPE, SIG_IGN);
430+
#endif
431+
417432
mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4));
418433

419434
pre_process_options(argc, argv);

0 commit comments

Comments
 (0)