How to maintain servers in SIGPIPE

Asked 2 years ago, Updated 2 years ago, 79 views

We created a server that receives commands through TCP or local UNIX sockets and sends the results. Occasionally, the client leaves before receiving the result and SIGPIPE occurs, each time the server dies.

How can I keep the server alive? Do I have to check if I can write it on the socket? Or should I register SIGPIPE with catch and a handler that doesn't do anything?

c io broken-pipe sigpipe signal

2022-09-22 15:47

1 Answers

Usually, in this case, we ignore (not even catch) SIGPIPE and handle the error immediately within the code.

signal(SIGPIPE, SIG_IGN); If you add this code, SIGPIPE will not occur even if something is written to a socket/pipe that cannot be written.

If you are using the send() function, you can also give the MSG_NOSIGNAL option. However, not all operating systems support MSG_NOSIGNAL, so please check and write it in advance,

Finally, there is a way to set SO_SIGNOPIPE flag in setsockopt(). Use this method when you want to ignore SIGPIPE only in a specific socket. This is also not supported by all operating systems, so please check it in advance and write it


2022-09-22 15:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.