8000 bugfix: Make `CCheckQueue` RAII-styled (attempt 2) by hebasto · Pull Request #26762 · bitcoin/bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

bugfix: Make CCheckQueue RAII-styled (attempt 2) #26762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 30, 2023

Conversation

hebasto
Copy link
Member
@hebasto hebasto commented Dec 28, 2022

This PR:

The previous attempt was in #18731.

@DrahtBot
Copy link
Contributor

DrahtBot commented Dec 28, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK TheCharlatan, martinus, achow101
Approach ACK w0xlt
Stale ACK ajtowns

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #28960 (kernel: Remove dependency on CScheduler by TheCharlatan)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Contributor
@w0xlt w0xlt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach ACK

Copy link
Contributor
@ajtowns ajtowns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

@@ -187,24 +175,16 @@ class CCheckQueue
}
}

//! Stop all of the worker threads.
void StopWorkerThreads() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
~CCheckQueue()
{
WITH_LOCK(m_mutex, m_request_stop = true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang turns off thread safety analysis in constructors and destructors, so I think if locking is needed it would be better to keep a separate (private?) function, that the destructor calls when necessary to maximise compile time checking.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Updated.

@@ -244,7 +244,6 @@ void Shutdown(NodeContext& node)
// CScheduler/checkqueue, scheduler and load block thread.
if (node.scheduler) node.scheduler->stop();
if (node.chainman && node.chainman->m_load_block.joinable()) node.chainman->m_load_block.join();
StopScriptCheckWorkerThreads();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cf #23234 (comment)

I don't think this change makes anything worse, but Shutdown() is pretty fragile.

@@ -134,22 +134,10 @@ class CCheckQueue
Mutex m_control_mutex;

//! Create a new check queue
explicit CCheckQueue(unsigned int nBatchSizeIn)
: nBatchSize(nBatchSizeIn)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I would probably have broken this up into multiple commits:

  1. refactor/behaviour change: move the global scriptcheckqueue into a class (and move its params out of init.cpp)
  2. possible behaviour change: move all the StopWorkerThreads() calls to wherever the queue goes out of scope (~ChainStateManager?)
  3. refactor: add a constructor that accepts worker_threads_num and just calls StartWorkerThreads()
  4. refactor: replace all the calls to StartWorkerThreads with the new constructor
  5. refactor: call StopWorkerThreads() from the destructor only

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Broken into a few commits in way to assure each commit passes tests.

@@ -1492,6 +1492,7 @@ Chainstate::Chainstate(
ChainstateManager& chainman,
std::optional<uint256> from_snapshot_blockhash)
: m_mempool(mempool),
m_script_check_queue{std::make_unique<CCheckQueue<CScriptCheck>>(/*batch_size=*/128, chainman.m_options.worker_threads_num)},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be doubling the number of script check workers if the assumeutxo snapshot chainstate is allocated; shouldn't m_script_check_queue be part of ChainstateManager instead, so it remains a single global queue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Updated.

@@ -143,6 +143,9 @@ class CCheckQueue
}
}

CCheckQueue(const CCheckQueue&) = delete;
CCheckQueue& operator=(const CCheckQueue&) = delete;
Copy link
Contributor
@ajtowns ajtowns Jan 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should apply rule-of-5 and default the move operations, no?
(EDIT: also, move the explanation of why these are deleted from the commit message into the file itself?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Done.

@hebasto
Copy link
Member Author
hebasto commented Jan 3, 2023

Updated 2d248d6 -> 5a19c39 (pr26762.01 -> pr26762.02, diff):

@hebasto
Copy link
Member Author
hebasto commented Jan 16, 2023

Rebased 5a19c39 -> f020924 (pr26762.02 -> pr26762.03) due to the conflict with #26251.

@ajtowns
Copy link
Contributor
ajtowns commented Jan 27, 2023

utACK f020924

Seems reasonable to me. Not 100% confident over delaying stopping the threads until the destructor; but also about equally unsure about how it works now...

: m_script_check_queue{/*nBatchSizeIn=*/128},
m_options{Flatten(std::move(options))}
{
m_script_check_queue.StartWorkerThreads(m_options.worker_threads_num);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously this was conditional on worker_threads_num > 0 (since StartScriptCheckWorkerThreads would not be called unless script_threads >= 1). I think this is okay, since in that case the function only sets some variables that will never be used and skips over a loop.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code will gone in the "refactor: Make CCheckQueue constructor start worker threads" commit.

@hebasto
Copy link
Member Author
hebasto commented Jan 30, 2023

Updated f020924 -> 5a7932f (pr26762.03 -> pr26762.04, diff):

  • addressed @ajtowns's comment
  • add a default initializer for kernel::ChainstateManagerOpts::worker_threads_num

Not 100% confident over delaying stopping the threads until the destructor; but also about equally unsure about how it works now...

Actually, it is how the #25448 has been fixed now.

@ajtowns
Copy link
Contributor
ajtowns commented Jan 31, 2023

add a default initializer for kernel::ChainstateManagerOpts::worker_threads_num

Yikes. Work on adding cppcoreguidelines-pro-type-member-init` to clang-tidy maybe?

reACK 5a7932f

@DrahtBot DrahtBot mentioned this pull request Feb 4, 2023
Copy link
Contributor
@TheCharlatan TheCharlatan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

@@ -34,6 +36,16 @@ std::optional<bilingual_str> ApplyArgsManOptions(const ArgsManager& args, Chains

if (auto value{args.GetIntArg("-maxtipage")}) opts.max_tip_age = std::chrono::seconds{*value};

int par_value = args.GetIntArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not consistent with how the other options are handled. This overrides the value of the passed in option even if no argument was passed in. I would handle this the same way as max_tip_age and also move the MAX_SCRIPTCHECK_THREADS to chainstatemanager_opts.h.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not consistent with how the other options are handled. This overrides the value of the passed in option even if no argument was passed in. I would handle this the same way as max_tip_age...

I'm not sure about this change considering this PR scope. The semantic of the "-par" and "-maxtipage" options are quite different.

... and also move the MAX_SCRIPTCHECK_THREADS to chainstatemanager_opts.h.

Thanks! Updated.

@hebasto
Copy link
Member Author
hebasto commented Mar 21, 2023

Yikes. Work on adding cppcoreguidelines-pro-type-member-init` to clang-tidy maybe?

Well, it fires too many false warnings for our code base (for example, for CTxOut::CTxOut() constructor).

@hebasto
Copy link
Member Author
hebasto commented Oct 3, 2023

Rebased 2235765 -> 5b3ea5f (pr26762.14 -> pr26762.15) due to the conflict with #27596.

Copy link
@cacrowley cacrowley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor
@TheCharlatan TheCharlatan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 5b3ea5f

@@ -17,9 +17,9 @@

#include <common/system.h>
#include <interfaces/node.h>
#include <node/chainstatemanager_args.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: fix include order.

@DrahtBot DrahtBot requested review from martinus and w0xlt and removed request for martinus and w0xlt November 29, 2023 16:35
@martinus
Copy link
Contributor

ACK 5b3ea5f

@DrahtBot DrahtBot requested review from w0xlt and removed request for martinus and w0xlt November 29, 2023 18:01
@achow101
Copy link
Member

ACK 5b3ea5f

@DrahtBot DrahtBot requested review from w0xlt and removed request for w0xlt November 30, 2023 19:17
@achow101 achow101 merged commit 498994b into bitcoin:master Nov 30, 2023
@@ -362,7 +353,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
/** Test that CCheckQueueControl is threadsafe */
BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks)
{
auto queue = std::make_unique<Standard_Queue>(QUEUE_BATCH_SIZE);
auto queue = std::make_unique<Standard_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a change in behavior for the test in 9cf89f7?

@@ -17,6 +17,7 @@
#include <mapport.h>
#include <net.h>
#include <netbase.h>
#include <node/chainstatemanager_args.h>
#include <txdb.h> // for -dbcache defaults
#include <util/string.h>
#include <validation.h> // For DEFAULT_SCRIPTCHECK_THREADS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5b3ea5f: Comment is now wrong, no?

Copy link
Member
@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@bitcoin bitcoin locked and limited conversation to collaborators Dec 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

~CCheckQueue assertion failure during unclean exits
10 participants
0