Skip to content
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(static-file): pass producer as Arc<Mutex<_>> to ensure only one is active #7143

Merged
merged 12 commits into from Mar 15, 2024

Conversation

shekhirin
Copy link
Collaborator

@shekhirin shekhirin commented Mar 14, 2024

This PR fixes a critical bug with multiple StaticFileProducer instances writing to static files at the same time, potentially causing duplicate and inconsistent data.

We need to have at most one StaticFileProducer active at a time. Otherwise, such a situation is possible:

  1. StaticFileHook is started in the engine loop via polling the next hook
    if let Poll::Ready(result) = this.hooks.poll_next_hook(
    cx,
    EngineContext {
    tip_block_number: this.blockchain.canonical_tip().number,
    finalized_block_number: this
    .blockchain
    .finalized_block_number()
    .map_err(RethError::Provider)?,
    },
    this.sync.is_pipeline_active(),
    )? {
  2. On the next engine loop iteration, sync controlled is polled
    // process sync events if any
    match this.sync.poll(cx) {
  3. Sync controller spawns the pipeline, and we call the StaticFileProducer before actually starting the execution of all stages
    pub async fn run_loop(&mut self) -> Result<ControlFlow, PipelineError> {
    self.produce_static_files()?;
  4. Both StaticFileHook and Pipeline::produce_static_files run the StaticFileProducer, and it writes to static files from both places.

Having only one StaticFileProducer can be ensured via sharing one copy between everyone using Arc<Mutex<StaticFileProducer>>, so that the user will need to lock the static file producer first for the duration of its usage. This PR introduces a newtype wrapper StaticFileProducer(Arc<Mutex<StaticFileProducerInner>>) that makes it easier to initialize and pass it around.

An additional measure that can be done to prevent the static files corruption is implemented #7137.

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

it's not super pretty, but this should do it

@@ -21,7 +21,7 @@ pub type StaticFileProducerResult = RethResult<StaticFileTargets>;
pub type StaticFileProducerWithResult<DB> = (StaticFileProducer<DB>, StaticFileProducerResult);

/// Static File producer routine. See [StaticFileProducer::run] for more detailed description.
#[derive(Debug, Clone)]
#[derive(Debug)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

good!

@shekhirin
Copy link
Collaborator Author

it's not super pretty, but this should do it

can do it prettier via a type alias that's public and a private internal type

@mattsse
Copy link
Collaborator

mattsse commented Mar 14, 2024

can do it prettier via a type alias

no aliases please :)

@shekhirin
Copy link
Collaborator Author

can do it prettier via a type alias

no aliases please :)

newtype

@shekhirin shekhirin force-pushed the alexey/static-files-arc-mutex branch from 6e8c0e7 to e7f0930 Compare March 14, 2024 19:09
@shekhirin shekhirin marked this pull request as ready for review March 14, 2024 19:29
@shekhirin shekhirin force-pushed the alexey/static-files-arc-mutex branch from c79d9ec to f7f3f30 Compare March 14, 2024 19:37
@shekhirin shekhirin requested a review from mattsse March 14, 2024 20:11
@shekhirin shekhirin added C-enhancement New feature or request A-consensus Related to the consensus engine A-static-files Related to static files labels Mar 14, 2024
@shekhirin shekhirin changed the title feat(static-file): always pass producer as Arc<Mutex<_>> fix(static-file): always pass producer as Arc<Mutex<_>> to ensure only one active at a time Mar 14, 2024
@shekhirin shekhirin changed the title fix(static-file): always pass producer as Arc<Mutex<_>> to ensure only one active at a time fix(static-file): pass producer as Arc<Mutex<_>> to ensure only one active Mar 14, 2024
@shekhirin shekhirin changed the title fix(static-file): pass producer as Arc<Mutex<_>> to ensure only one active fix(static-file): pass producer as Arc<Mutex<_>> to ensure only one is active Mar 14, 2024
@@ -131,7 +131,7 @@ where
) -> Option<CallOutcome> {
call_inspectors!([&mut self.custom_print_tracer], |inspector| {
if let Some(outcome) = inspector.call(context, inputs) {
return Some(outcome);
Copy link
Member

@rkrasiuk rkrasiuk Mar 15, 2024

Choose a reason for hiding this comment

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

how do these keep making their way back into the codebase

@shekhirin shekhirin added this pull request to the merge queue Mar 15, 2024
Merged via the queue into main with commit 52d4983 Mar 15, 2024
28 checks passed
@shekhirin shekhirin deleted the alexey/static-files-arc-mutex branch March 15, 2024 13:36
Ruteri pushed a commit to Ruteri/reth that referenced this pull request Apr 17, 2024
… is active (paradigmxyz#7143)

Co-authored-by: joshieDo <ranriver@protonmail.com>
Ruteri pushed a commit to Ruteri/reth that referenced this pull request Apr 17, 2024
… is active (paradigmxyz#7143)

Co-authored-by: joshieDo <ranriver@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-consensus Related to the consensus engine A-static-files Related to static files C-enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants