Skip to content

Commit 9d3e39c

Browse files
author
Kai Jewson
authored
Set noinherit on accepted sockets on Windows
1 parent 99f5089 commit 9d3e39c

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

src/socket.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,12 @@ impl Socket {
260260
)))]
261261
{
262262
let (socket, addr) = self.accept_raw()?;
263-
set_common_flags(socket).map(|socket| (socket, addr))
263+
let socket = set_common_flags(socket)?;
264+
// `set_common_flags` does not disable inheritance on Windows because `Socket::new`
265+
// unlike `accept` is able to create the socket with inheritance disabled.
266+
#[cfg(windows)]
267+
socket._set_no_inherit(true)?;
268+
Ok((socket, addr))
264269
}
265270
}
266271

tests/socket.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,26 +154,43 @@ fn set_nonblocking() {
154154
assert_nonblocking(&socket, false);
155155
}
156156

157-
#[test]
158-
fn default_flags() {
159-
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
157+
fn assert_common_flags(socket: &Socket, expected: bool) {
160158
#[cfg(unix)]
161-
assert_close_on_exec(&socket, true);
159+
assert_close_on_exec(socket, expected);
162160
#[cfg(target_vendor = "apple")]
163-
assert_flag_no_sigpipe(&socket, true);
161+
assert_flag_no_sigpipe(socket, expected);
164162
#[cfg(windows)]
165-
assert_flag_no_inherit(&socket, true);
163+
assert_flag_no_inherit(socket, expected);
166164
}
167165

168166
#[test]
169-
fn no_default_flags() {
170-
let socket = Socket::new_raw(Domain::IPV4, Type::STREAM, None).unwrap();
171-
#[cfg(unix)]
172-
assert_close_on_exec(&socket, false);
173-
#[cfg(target_vendor = "apple")]
174-
assert_flag_no_sigpipe(&socket, false);
175-
#[cfg(windows)]
176-
assert_flag_no_inherit(&socket, false);
167+
fn common_flags() {
168+
let listener = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
169+
assert_common_flags(&listener, true);
170+
171+
listener.bind(&any_ipv4()).unwrap();
172+
listener.listen(1).unwrap();
173+
174+
let client = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
175+
client.connect(&listener.local_addr().unwrap()).unwrap();
176+
177+
let accepted = listener.accept().unwrap().0;
178+
assert_common_flags(&accepted, true);
179+
}
180+
181+
#[test]
182+
fn no_common_flags() {
183+
let listener = Socket::new_raw(Domain::IPV4, Type::STREAM, None).unwrap();
184+
assert_common_flags(&listener, false);
185+
186+
listener.bind(&any_ipv4()).unwrap();
187+
listener.listen(1).unwrap();
188+
189+
let client = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
190+
client.connect(&listener.local_addr().unwrap()).unwrap();
191+
192+
let accepted = listener.accept_raw().unwrap().0;
193+
assert_common_flags(&accepted, false);
177194
}
178195

179196
#[cfg(all(

0 commit comments

Comments
 (0)