-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
fix(engine): cleanup leaked context #13749
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works
7ebcaa8
to
227dc8b
Compare
CodSpeed Performance ReportMerging #13749 will degrade performances by 14.87%Comparing Summary
Benchmarks breakdown
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these changes makes sense overall, but I assume the lifetime issue is more tricky because we use a borrowed tx for the HashedPostStateCursor etc.
which wouldn't be 'static that the roottask requires.
although we have ownership of the consistentview, so this problem seems to arise from us using tx_ref, if I understood this correctly we could perhaps consume the provider_ro and its tx (into_tx instead of tx_ref)?
crates/engine/tree/src/tree/mod.rs
Outdated
let provider_ro = consistent_view.provider_ro()?; | ||
let nodes_sorted = state_root_config.nodes_sorted.clone(); | ||
let state_sorted = state_root_config.state_sorted.clone(); | ||
let prefix_sets = state_root_config.prefix_sets.clone(); | ||
|
||
let trie_cursor = Arc::new(InMemoryTrieCursorFactory::new( | ||
DatabaseTrieCursorFactory::new(provider_ro.tx_ref()), | ||
nodes_sorted, | ||
)); | ||
let state_cursor = Arc::new(HashedPostStateCursorFactory::new( | ||
DatabaseHashedCursorFactory::new(provider_ro.tx_ref()), | ||
state_sorted, | ||
)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the problematic section because this borrows and is not 'static
2276 | let provider_ro = consistent_view.provider_ro()?;
| ----------- bindingprovider_ro
declared here
...
2286 | DatabaseHashedCursorFactory::new(provider_ro.tx_ref()),
| ^^^^^^^^^^^---------
| |
| borrowed value does not live long enough
| argument requires thatprovider_ro
is borrowed for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes exactly, we previously had the same issue with nodes_sorted
and state_sorted
, was fixed by turning reference parameters into Arc
s here a5103d5, currently in progress passing ownership of the provider factory as you mentioned
crates/engine/tree/src/tree/root.rs
Outdated
BPF: BlindedProviderFactory + Send + Sync + 'static, | ||
BPF::AccountNodeProvider: BlindedProvider + Send + Sync + 'static, | ||
BPF::StorageNodeProvider: BlindedProvider + Send + Sync + 'static, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which would clash with these here I assume
5f4e69d
to
a23771d
Compare
a14c037
to
b531bf7
Compare
af808c2
to
4131ec8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, what's the main reason why we need Arc now in a bunch of places. this appears a bit invasive now
is this because of
reth/crates/trie/db/src/trie_cursor.rs
Line 19 in 8efe441
pub struct DatabaseTrieCursorFactory<'a, TX>(&'a TX); |
reth/crates/trie/db/src/hashed_cursor.rs
Line 12 in 8efe441
pub struct DatabaseHashedCursorFactory<'a, TX>(&'a TX); |
?
but we need an owned instance?
because we previously could use both factories with a single reference and we're forced to own so we can clone+spawn, this then does not work for this pr.
what if we instead of having two factory types use only one and implement both traits for that,
another idea would be to not require &Tx
and Tx: DbTx
and instead use a new helper trait that can handle both &Tx and owned provider?
@@ -162,7 +162,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> { | |||
|
|||
td += sealed_block.difficulty(); | |||
let mut executor = executor_provider.batch_executor(StateProviderDatabase::new( | |||
LatestStateProviderRef::new(&provider_rw), | |||
LatestStateProviderRef::new(provider_rw.clone()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, why does this Ref
type now requre an owned instance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, LatestStateProviderRef
and LatestStateProvider
can be merged with the current approach, will address this depending on how we decide to go forward
624d05e
to
8d68b54
Compare
yes, that's exactly the case. moreover, several places use the reth/crates/trie/db/src/proof.rs Lines 37 to 40 in 83b5619
given that we are forced to own, having the ability to create the factories and related types from an owned instance is pervasive and affects a lot of places.
thx a lot for the suggestions! will give them a try, i think the problem with the first one is that in any case we need an owned instance, and the changes would cascade as we have now, will let you know how it goes |
8d68b54
to
c2f6d17
Compare
Finally went for this second option, i already had in place a |
a4c1aaf
to
14ec3ef
Compare
a bit simpler now without the wrapper, implementing the helper trait directly on &Tx |
14ec3ef
to
a3dc40c
Compare
a3dc40c
to
cd63408
Compare
After the introduction on
std::thread::scope
for creatingStateRootTask
, we started usingBox::leak
to keep alive values required to return and move around the state hook closure. This caused the variables in the kept context to cause errors like:This PR includes changes to the parameters needed to create the blinded provider factory required by
StateRootTask
usingArc
instead of references, which don't have a big performance impact and are automatically cleaned up without lifetime issues.A provider transaction reference was one of the parameters required to create the blinded provider factory. Instead of it, we are now passing an
Arc<Provider>
, which can obtain the transaction reference as needed and doesn't impose additional lifetime constraints. This change requires further changes in all the code that use the cursors, limited to the trie crates.