Skip to content

Commit

Permalink
Merge branch 'HTTPRebindSegFault' of https://github.com/Daviey/xmrig
Browse files Browse the repository at this point in the history
…into Daviey-HTTPRebindSegFault
  • Loading branch information
xmrig committed Feb 12, 2024
2 parents 8afd4d5 + daa6328 commit 2c9c40d
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/base/api/Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@


#include <thread>
#include <iostream>


namespace xmrig {
Expand Down Expand Up @@ -80,7 +81,8 @@ static rapidjson::Value getResources(rapidjson::Document &doc)

xmrig::Api::Api(Base *base) :
m_base(base),
m_timestamp(Chrono::currentMSecsSinceEpoch())
m_timestamp(Chrono::currentMSecsSinceEpoch()),
m_httpd(nullptr)
{
base->addListener(this);

Expand All @@ -91,7 +93,11 @@ xmrig::Api::Api(Base *base) :
xmrig::Api::~Api()
{
# ifdef XMRIG_FEATURE_HTTP
delete m_httpd;
if (m_httpd) {
m_httpd->stop();
delete m_httpd;
m_httpd = nullptr; // Ensure the pointer is set to nullptr after deletion
}
# endif
}

Expand All @@ -109,30 +115,40 @@ void xmrig::Api::start()
genWorkerId(m_base->config()->apiWorkerId());

# ifdef XMRIG_FEATURE_HTTP
m_httpd = new Httpd(m_base);
m_httpd->start();
if (!m_httpd) {
m_httpd = new Httpd(m_base);
if (!m_httpd->start()) {
std::cerr << "HTTP server failed to start." << std::endl;
delete m_httpd; // Properly handle failure to start
m_httpd = nullptr;
}
}
# endif
}


void xmrig::Api::stop()
{
# ifdef XMRIG_FEATURE_HTTP
m_httpd->stop();
if (m_httpd) {
m_httpd->stop();
}
# endif
}


void xmrig::Api::tick()
{
# ifdef XMRIG_FEATURE_HTTP
if (m_httpd->isBound() || !m_base->config()->http().isEnabled()) {
if (!m_httpd || !m_base->config()->http().isEnabled() || m_httpd->isBound()) {
return;
}

if (++m_ticks % 10 == 0) {
m_ticks = 0;
m_httpd->start();
if (m_httpd) {
m_httpd->start();
}
}
# endif
}
Expand Down

0 comments on commit 2c9c40d

Please sign in to comment.