Commit 08be168d authored by Pieter Hintjens's avatar Pieter Hintjens

Merge pull request #514 from pijyoi/fixcs

release critical section on failure to create signaler fdpair
parents 7094a706 8c71ac47
...@@ -286,7 +286,7 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_) ...@@ -286,7 +286,7 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
(char *)&tcp_nodelay, sizeof (tcp_nodelay)); (char *)&tcp_nodelay, sizeof (tcp_nodelay));
wsa_assert (rc != SOCKET_ERROR); wsa_assert (rc != SOCKET_ERROR);
// Bind listening socket to any free local port. // Bind listening socket to signaler port.
struct sockaddr_in addr; struct sockaddr_in addr;
memset (&addr, 0, sizeof (addr)); memset (&addr, 0, sizeof (addr));
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
...@@ -314,15 +314,19 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_) ...@@ -314,15 +314,19 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
// Connect writer to the listener. // Connect writer to the listener.
rc = connect (*w_, (struct sockaddr*) &addr, sizeof (addr)); rc = connect (*w_, (struct sockaddr*) &addr, sizeof (addr));
wsa_assert (rc != SOCKET_ERROR);
// Accept connection from writer.
*r_ = accept (listener, NULL, NULL);
wsa_assert (*r_ != INVALID_SOCKET);
// On Windows, preventing sockets to be inherited by child processes. // Save errno if connection fails
brc = SetHandleInformation ((HANDLE) *r_, HANDLE_FLAG_INHERIT, 0); int conn_errno = 0;
win_assert (brc); if (rc == SOCKET_ERROR) {
conn_errno = WSAGetLastError ();
} else {
// Accept connection from writer.
*r_ = accept (listener, NULL, NULL);
if (*r_ == INVALID_SOCKET) {
conn_errno = WSAGetLastError ();
}
}
// We don't need the listening socket anymore. Close it. // We don't need the listening socket anymore. Close it.
rc = closesocket (listener); rc = closesocket (listener);
...@@ -332,11 +336,33 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_) ...@@ -332,11 +336,33 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
brc = SetEvent (sync); brc = SetEvent (sync);
win_assert (brc != 0); win_assert (brc != 0);
// Release the kernel object // Release the kernel object
brc = CloseHandle (sync); brc = CloseHandle (sync);
win_assert (brc != 0); win_assert (brc != 0);
return 0; if (*r_ != INVALID_SOCKET) {
// On Windows, preventing sockets to be inherited by child processes.
brc = SetHandleInformation ((HANDLE) *r_, HANDLE_FLAG_INHERIT, 0);
win_assert (brc);
return 0;
} else {
// Cleanup writer if connection failed
rc = closesocket (*w_);
wsa_assert (rc != SOCKET_ERROR);
*w_ = INVALID_SOCKET;
// Set errno from saved value
errno = wsa_error_to_errno (conn_errno);
// Ideally, we would return errno to the caller signaler_t()
// Unfortunately, it uses errno_assert() which gives "Unknown error"
// We might as well assert here and print the actual error message
wsa_assert_no (conn_errno);
return -1;
}
#elif defined ZMQ_HAVE_OPENVMS #elif defined ZMQ_HAVE_OPENVMS
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment