Skip to content

Commit 0d4753c

Browse files
committed
Make SO_NOSIGPIPE best-effort in select backend
On develop, 5 of 6 SO_NOSIGPIPE call sites in the select backend ignored setsockopt failures (best-effort); only the accept path was strict, which was itself inconsistent with the rest of the backend. The refactor propagated the strict behavior into the shared set_fd_options helper, making all socket-creation paths fail on SO_NOSIGPIPE errors — a behavior change from develop. Switch both the accept_policy and set_fd_options to best-effort to match develop's predominant behavior. Write paths still use MSG_NOSIGNAL where available, so SO_NOSIGPIPE is a safety net, not a hard requirement.
1 parent 36e1fa0 commit 0d4753c

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

include/boost/corosio/native/detail/select/select_traits.hpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,12 @@ struct select_traits
140140
}
141141

142142
#ifdef SO_NOSIGPIPE
143+
// Best-effort: SO_NOSIGPIPE is a safety net; write paths
144+
// also use MSG_NOSIGNAL where available. Failure here
145+
// should not prevent the accepted connection from being used.
143146
int one = 1;
144-
if (::setsockopt(
145-
new_fd, SOL_SOCKET, SO_NOSIGPIPE,
146-
&one, sizeof(one)) == -1)
147-
{
148-
int err = errno;
149-
::close(new_fd);
150-
errno = err;
151-
return -1;
152-
}
147+
(void)::setsockopt(
148+
new_fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one));
153149
#endif
154150

155151
return new_fd;
@@ -178,15 +174,14 @@ struct select_traits
178174
return make_err(EINVAL);
179175

180176
#ifdef SO_NOSIGPIPE
181-
// SO_NOSIGPIPE is the primary defense against SIGPIPE on
182-
// platforms that don't support MSG_NOSIGNAL. A silent failure
183-
// here would leave the fd vulnerable to crashing the process
184-
// on a closed-peer write, so propagate the error.
177+
// Best-effort: SO_NOSIGPIPE is a safety net; write paths
178+
// also use MSG_NOSIGNAL where available. Match develop's
179+
// predominant behavior of ignoring failures here rather
180+
// than failing socket creation.
185181
{
186182
int one = 1;
187-
if (::setsockopt(
188-
fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)) != 0)
189-
return make_err(errno);
183+
(void)::setsockopt(
184+
fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one));
190185
}
191186
#endif
192187

0 commit comments

Comments
 (0)