Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions api/src/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "serializer.hpp"

#include <csnode/configholder.hpp>
#include <lib/system/polluntil.hpp>

#include <solver/solvercore.hpp>
#include <solver/smartcontracts.hpp>
Expand Down Expand Up @@ -169,7 +170,8 @@ void cs::Executor::getExecutorBuildVersion(executor::ExecutorBuildVersionResult&
// could still arrive on this connection: replace it now, not on the next call
recreateOriginExecutor();

_return.status.code = 1;
// the executor did not answer: told apart from an answer, as execute() does
_return.status.code = cs::error::ThriftException;
_return.status.message = x.what();

notifyError();
Expand Down Expand Up @@ -208,7 +210,9 @@ void cs::Executor::stop() {

std::this_thread::sleep_for(std::chrono::milliseconds(500));

if (manager_.isExecutorProcessRunning()) {
// every contract-executor JVM of the machine is this node's (official behaviour), unless the
// operator runs several nodes on it (executor_multi_instance): then the others are theirs to keep
if (!cs::ConfigHolder::instance().config()->getApiSettings().executorMultiInstance && manager_.isExecutorProcessRunning()) {
manager_.stopExecutorProcess();
}
}
Expand Down Expand Up @@ -759,24 +763,47 @@ void cs::Executor::onExecutorProcessError(const cs::ProcessException& exception)
cswarning() << "Executor process error occured " << exception.what() << ", code " << exception.code();
}

// Runs on the thread that launched the executor (the node's start, or the watcher when it relaunches
// one), synchronously: moving it to a thread of its own would open the shared transport from two
// threads. The executor gets executorCheckVersionDelay to answer and is asked every second: a JVM up
// in 2 s lets the node go on after 2 s, dozens of JVMs starting at once on one machine still get the
// whole delay, and the last attempt is made at or after the deadline, so this is never stricter than
// the former single attempt after the delay. A failure ends as before: the executor is terminated.
void cs::Executor::checkExecutorVersion() {
executor::ExecutorBuildVersionResult _return;
bool result = false;

std::this_thread::sleep_for(std::chrono::milliseconds(cs::ConfigHolder::instance().config()->getApiSettings().executorCheckVersionDelay));
const auto start = std::chrono::steady_clock::now();
const auto delay = std::chrono::milliseconds(cs::ConfigHolder::instance().config()->getApiSettings().executorCheckVersionDelay);
cs::pollUntil(delay, std::chrono::milliseconds(1000),
[&] {
_return = executor::ExecutorBuildVersionResult{};
if (!connect()) {
return false;
}
getExecutorBuildVersion(_return);
// any answer is final, an error one too (e.g. an unknown method); a transport
// error means the executor is not listening yet
return _return.status.code != cs::error::ThriftException;
},
[&] { return requestStop_.load(); });

if (requestStop_) {
return;
}

connect();
getExecutorBuildVersion(_return);

const auto waited = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count();
if (_return.status.code == 0 && !isConnected()) {
_return.status.code = cs::error::ThriftException;
_return.status.message = "no connection to the executor";
}
if (_return.status.code != 0) {
cserror() << "Start contract executor error code " << int(_return.status.code) << ": " << _return.status.message;
cserror() << "Start contract executor error code " << int(_return.status.code) << ": " << _return.status.message
<< (_return.status.code == cs::error::ThriftException ? " (no answer within " : " (after ") << waited << " ms)";
}
else {
cslog() << "Start contract executor: " << _return.status.message << ". Executor build number " << _return.commitNumber;
cslog() << "Start contract executor: " << _return.status.message << ". Executor build number " << _return.commitNumber
<< ", answered after " << waited << " ms";
}

result = _return.commitNumber < commitMin_ || (_return.commitNumber > commitMax_ && commitMax_ != -1);
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ lines under the new release.
| #67 | csdb, ci, docs: conversion follow-ups from the review of #65 (contract-records message, keep a copy of `db/` before converting from berkeleydb, sturdier db-convert scenario) |
| #70 | csnode: bootstrap tables of 9 to 25 confidants are accepted (the official node silently drops them, so a network of 9 or more initial confidants never starts); the same choice as the official node up to 8 |
| #71 | csnode: a bootstrap round table is taken only for a round next to the node's own, and, unless signed with the blockchain key, only once the node's own round has stalled; the block-removal request it set (never effective) is gone |
| #73 | api: the executor version check asks every second up to `executor_check_version_delay` instead of once after it (the node starts as soon as its executor answers); with `executor_multi_instance` a stopping node no longer kills the other nodes' executors |

## community.9 — 2026-10-22

Expand Down
34 changes: 34 additions & 0 deletions lib/include/lib/system/polluntil.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef POLLUNTIL_HPP
#define POLLUNTIL_HPP

#include <algorithm>
#include <chrono>
#include <thread>

namespace cs {

// Calls probe() now and then every step until it returns true, the time is up or stop() returns true.
// The last call is made at or after the deadline, so a probe that succeeds only at the very end still
// counts. Returns the result of the last call (false if stop() ended it before any success).
template <typename Probe, typename Stop>
bool pollUntil(std::chrono::milliseconds total, std::chrono::milliseconds step, Probe probe, Stop stop) {
const auto deadline = std::chrono::steady_clock::now() + total;
for (;;) {
if (stop()) {
return false;
}
if (probe()) {
return true;
}
const auto now = std::chrono::steady_clock::now();
if (now >= deadline) {
return false;
}
// rounded up: rounded down, the last millisecond would spin with zero-length sleeps
std::this_thread::sleep_for(std::min(step, std::chrono::ceil<std::chrono::milliseconds>(deadline - now)));
}
}

} // namespace cs

#endif // POLLUNTIL_HPP
45 changes: 45 additions & 0 deletions tests/csnode/polluntil_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "gtest/gtest.h"

#include <chrono>

#include <lib/system/polluntil.hpp>

using namespace std::chrono_literals;
using cs::pollUntil;

namespace {
auto never = [] { return false; };
} // namespace

// the executor answers on the third attempt: no need to wait for the whole delay
TEST(PollUntil, StopsAtTheFirstSuccess) {
int calls = 0;
const auto start = std::chrono::steady_clock::now();
EXPECT_TRUE(pollUntil(5000ms, 20ms, [&] { return ++calls == 3; }, never));
EXPECT_EQ(calls, 3);
EXPECT_LT(std::chrono::steady_clock::now() - start, 1000ms);
}

// never answers: tried until the deadline, the last attempt at or after it
TEST(PollUntil, LastAttemptAtTheDeadline) {
int calls = 0;
std::chrono::steady_clock::time_point last;
const auto start = std::chrono::steady_clock::now();
EXPECT_FALSE(pollUntil(250ms, 100ms, [&] { ++calls; last = std::chrono::steady_clock::now(); return false; }, never));
EXPECT_GE(calls, 3);
EXPECT_LE(calls, 6);
EXPECT_GE(last - start, 250ms);
}

TEST(PollUntil, AStopEndsItEarly) {
int calls = 0;
EXPECT_FALSE(pollUntil(5000ms, 10ms, [&] { ++calls; return false; }, [&] { return calls >= 2; }));
EXPECT_EQ(calls, 2);
}

TEST(PollUntil, NoDelayMeansOneAttempt) {
int calls = 0;
EXPECT_FALSE(pollUntil(0ms, 100ms, [&] { ++calls; return false; }, never));
EXPECT_EQ(calls, 1);
EXPECT_TRUE(pollUntil(0ms, 100ms, [] { return true; }, never));
}
Loading