Skip to content

Commit 6f4ed5f

Browse files
committed
xmrig#478 Fixed totally broken reconnect.
1 parent f852996 commit 6f4ed5f

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

cmake/flags.cmake

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
66
set(CMAKE_BUILD_TYPE Release)
77
endif()
88

9+
if (CMAKE_BUILD_TYPE STREQUAL "Release")
10+
add_definitions(/DNDEBUG)
11+
endif()
12+
913
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
1014

1115
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-strict-aliasing")

src/App_unix.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
55
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
66
* Copyright 2016 Jay D Dee <[email protected]>
7-
* Copyright 2016-2017 XMRig <support@xmrig.com>
8-
*
7+
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
8+
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <[email protected]>
99
*
1010
* This program is free software: you can redistribute it and/or modify
1111
* it under the terms of the GNU General Public License as published by
@@ -36,6 +36,8 @@
3636

3737
void App::background()
3838
{
39+
signal(SIGPIPE, SIG_IGN);
40+
3941
if (m_options->affinity() != -1L) {
4042
Cpu::setAffinity(-1, m_options->affinity());
4143
}

src/net/Client.cpp

+35-14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
*/
2323

24+
#include <assert.h>
2425
#include <inttypes.h>
2526
#include <iterator>
2627
#include <stdio.h>
@@ -204,16 +205,30 @@ bool Client::close()
204205

205206
setState(ClosingState);
206207

207-
uv_read_stop(reinterpret_cast<uv_stream_t*>(m_socket));
208+
uv_stream_t *stream = reinterpret_cast<uv_stream_t*>(m_socket);
208209

209-
uv_shutdown(new uv_shutdown_t, reinterpret_cast<uv_stream_t*>(m_socket), [](uv_shutdown_t* req, int status) {
210+
if (uv_is_readable(stream) == 1) {
211+
uv_read_stop(stream);
212+
}
210213

211-
if (uv_is_closing(reinterpret_cast<uv_handle_t*>(req->handle)) == 0) {
212-
uv_close(reinterpret_cast<uv_handle_t*>(req->handle), Client::onClose);
213-
}
214+
if (uv_is_writable(stream) == 1) {
215+
const int rc = uv_shutdown(new uv_shutdown_t, stream, [](uv_shutdown_t* req, int status) {
216+
if (uv_is_closing(reinterpret_cast<uv_handle_t*>(req->handle)) == 0) {
217+
uv_close(reinterpret_cast<uv_handle_t*>(req->handle), Client::onClose);
218+
}
214219

215-
delete req;
216-
});
220+
delete req;
221+
});
222+
223+
assert(rc == 0);
224+
225+
if (rc != 0) {
226+
onClose();
227+
}
228+
}
229+
else {
230+
uv_close(reinterpret_cast<uv_handle_t*>(m_socket), Client::onClose);
231+
}
217232

218233
return true;
219234
}
@@ -442,6 +457,18 @@ void Client::login()
442457
}
443458

444459

460+
void Client::onClose()
461+
{
462+
delete m_socket;
463+
464+
m_stream = nullptr;
465+
m_socket = nullptr;
466+
setState(UnconnectedState);
467+
468+
reconnect();
469+
}
470+
471+
445472
void Client::parse(char *line, size_t len)
446473
{
447474
startTimeout();
@@ -655,13 +682,7 @@ void Client::onClose(uv_handle_t *handle)
655682
return;
656683
}
657684

658-
delete client->m_socket;
659-
660-
client->m_stream = nullptr;
661-
client->m_socket = nullptr;
662-
client->setState(UnconnectedState);
663-
664-
client->reconnect();
685+
client->onClose();
665686
}
666687

667688

src/net/Client.h

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class Client
8787
void connect(const std::vector<addrinfo*> &ipv4, const std::vector<addrinfo*> &ipv6);
8888
void connect(sockaddr *addr);
8989
void login();
90+
void onClose();
9091
void parse(char *line, size_t len);
9192
void parseExtensions(const rapidjson::Value &value);
9293
void parseNotification(const char *method, const rapidjson::Value &params, const rapidjson::Value &error);

0 commit comments

Comments
 (0)