Skip to content

Commit 65fb637

Browse files
author
yuchang.xu
committed
Bugfix: fix panic caused by fd limit reached during new ClientConnection
Fixed issue where reaching the file descriptor limit (EMFILE) during ClientConnection creation could lead to unexpected process termination. Signed-off-by: yuchang.xu <yuchang.xu@linux.alibaba.com>
1 parent 7ca128d commit 65fb637

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/sync/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ impl Client {
5555
#[cfg(unix)]
5656
/// Initialize a new [`Client`] from raw file descriptor.
5757
pub fn new(fd: RawFd) -> Result<Client> {
58-
let conn = ClientConnection::new(fd);
58+
let conn =
59+
ClientConnection::new(fd).map_err(err_to_others_err!(e, "new ClientConnection"))?;
5960

6061
Self::new_client(conn)
6162
}

src/sync/sys/unix/net.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,14 @@ pub struct ClientConnection {
228228
}
229229

230230
impl ClientConnection {
231-
pub fn client_connect(sockaddr: &str)-> Result<ClientConnection> {
231+
pub fn client_connect(sockaddr: &str) -> Result<ClientConnection> {
232232
let fd = unsafe { client_connect(sockaddr)? };
233-
Ok(ClientConnection::new(fd))
233+
ClientConnection::new(fd)
234234
}
235235

236-
pub(crate) fn new(fd: RawFd) -> ClientConnection {
236+
pub(crate) fn new(fd: RawFd) -> Result<ClientConnection> {
237237
let (recver_fd, close_fd) =
238-
socketpair(AddressFamily::Unix, SockType::Stream, None, SOCK_CLOEXEC).unwrap();
238+
socketpair(AddressFamily::Unix, SockType::Stream, None, SOCK_CLOEXEC)?;
239239

240240
// MacOS doesn't support descriptor creation with SOCK_CLOEXEC automically,
241241
// so there is a chance of leak if fork + exec happens in between of these calls.
@@ -245,11 +245,10 @@ impl ClientConnection {
245245
set_fd_close_exec(close_fd).unwrap();
246246
}
247247

248-
249-
ClientConnection {
250-
fd,
251-
socket_pair: (recver_fd, close_fd)
252-
}
248+
Ok(ClientConnection {
249+
fd,
250+
socket_pair: (recver_fd, close_fd),
251+
})
253252
}
254253

255254
pub fn ready(&self) -> std::result::Result<Option<()>, io::Error> {

0 commit comments

Comments
 (0)