Skip to content

fix: properly defer results of a promise #882

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 1 commit into from
Apr 25, 2025
Merged

Conversation

Wodann
Copy link
Member

@Wodann Wodann commented Apr 23, 2025

Fixes the issue raised here by only returning errors when the promise is resolved.

@Wodann Wodann requested review from frangio, a team and Copilot April 23, 2025 18:07
@Wodann Wodann self-assigned this Apr 23, 2025
Copy link

changeset-bot bot commented Apr 23, 2025

🦋 Changeset detected

Latest commit: 0687ab9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@nomicfoundation/edr Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

changeset-bot bot commented Apr 23, 2025

🦋 Changeset detected

Latest commit: 7a298b4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@nomicfoundation/edr Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Wodann Wodann had a problem deploying to github-action-benchmark April 23, 2025 18:07 — with GitHub Actions Error
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses the issue of deferring promise results by ensuring errors are only returned when the promise is resolved. It updates error handling in the test file, refactors the provider’s callback handling, and improves error propagation in the context creation.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
crates/edr_napi/test/issues.ts Replaces try/catch error handling with assert.isRejected and renames the variable for clarity
crates/edr_napi/src/provider.rs Refactors the call_override_callback to use deferred rejection in case of errors
crates/edr_napi/src/context.rs Introduces early deferred creation and uses match expressions to reject on configuration or build errors
.changeset/fresh-chairs-relate.md Documents the patch release with a brief description of the fix

@Wodann Wodann force-pushed the fix/create-provider-async branch from 7a298b4 to 37c063d Compare April 23, 2025 18:08
@Wodann Wodann temporarily deployed to github-action-benchmark April 23, 2025 18:08 — with GitHub Actions Inactive
Copy link

codecov bot commented Apr 23, 2025

Codecov Report

Attention: Patch coverage is 0% with 38 lines in your changes missing coverage. Please review.

Project coverage is 54.21%. Comparing base (6232310) to head (0687ab9).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/edr_napi/src/context.rs 0.00% 16 Missing ⚠️
crates/edr_napi/src/logger.rs 0.00% 16 Missing ⚠️
crates/edr_napi/src/provider.rs 0.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #882      +/-   ##
==========================================
- Coverage   54.23%   54.21%   -0.03%     
==========================================
  Files         252      252              
  Lines       29685    29704      +19     
  Branches    29685    29704      +19     
==========================================
+ Hits        16101    16103       +2     
- Misses      12620    12637      +17     
  Partials      964      964              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@agostbiro agostbiro left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM

@agostbiro
Copy link
Member

Hmm on a second look, there are some Hardhat 2 integration tests failing that we should fix before merging I think

@Wodann
Copy link
Member Author

Wodann commented Apr 23, 2025

Hmm on a second look, there are some Hardhat 2 integration tests failing that we should fix before merging I think

I'm looking into this. It has to do with my changes, but I'm still uncertain why.

Copy link
Contributor

Choose a reason for hiding this comment

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

I wanted to see if I could come up with a nicer way to write this since the match statements introduce a lot of noise. We may not want to do this but I'll share what I thought of:

        let (deferred, promise) = env.create_deferred()?;
        let deferred = &std::cell::Cell::new(Some(deferred));

        let try_create_provider = || -> Result<(), napi::Error> {
            let provider_config = edr_napi_core::provider::Config::try_from(provider_config)?;

            let logger_config = logger_config.resolve(&env)?; // <-- keep using ? operator

            // ...

            let deferred = deferred.take().unwrap();

            runtime.clone().spawn_blocking(move || {
                let result = builder.build(runtime.clone()).map(|provider| {
                    Provider::new(
                        provider,
                        runtime,
                        contract_decoder,
                        #[cfg(feature = "scenarios")]
                        scenario_file,
                    )
                });

                deferred.resolve(|_env| result);
            });

            Ok(())
        };

        try_create_provider().unwrap_or_else(|error| {
            deferred.take().unwrap().reject(error);
        });

        Ok(promise)

Copy link
Member Author

Choose a reason for hiding this comment

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

We could introduce a macro (for which ? is syntactic sugar) or extract all of the conversions into a local function - resulting in a single match statement. That way we're remaining fully statically typed, as opposed to Cell<Option>'s runtime checks.

Copy link
Member Author

@Wodann Wodann Apr 24, 2025

Choose a reason for hiding this comment

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

Addressed by 19410ea

@Wodann Wodann requested a review from agostbiro April 24, 2025 23:05
@Wodann
Copy link
Member Author

Wodann commented Apr 24, 2025

Hmm on a second look, there are some Hardhat 2 integration tests failing that we should fix before merging I think

The issue was resolved in 24b0d94. @agostbiro could you please review this change?

@@ -101,8 +101,22 @@ impl LoggerConfig {
print_line_callback.unref(env)?;

let print_line_fn = Arc::new(move |message, replace| {
let status =
print_line_callback.call((message, replace), ThreadsafeFunctionCallMode::Blocking);
let (sender, receiver) = channel();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we consider using a tokio::oneshot::channel here? Not sure how significant a difference it may be.

Copy link
Member Author

Choose a reason for hiding this comment

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

Tokio's version would require async and this is executed in a sync environment.

@Wodann Wodann had a problem deploying to github-action-benchmark April 24, 2025 23:33 — with GitHub Actions Error
@Wodann Wodann force-pushed the fix/create-provider-async branch from fc8e3f1 to 74e474a Compare April 24, 2025 23:36
@Wodann Wodann had a problem deploying to github-action-benchmark April 24, 2025 23:37 — with GitHub Actions Error
@Wodann Wodann force-pushed the fix/create-provider-async branch from 74e474a to 19410ea Compare April 24, 2025 23:39
@Wodann Wodann temporarily deployed to github-action-benchmark April 24, 2025 23:39 — with GitHub Actions Inactive
@Wodann Wodann force-pushed the fix/create-provider-async branch from 19410ea to 0687ab9 Compare April 25, 2025 00:44
@Wodann Wodann temporarily deployed to github-action-benchmark April 25, 2025 00:44 — with GitHub Actions Inactive
@Wodann
Copy link
Member Author

Wodann commented Apr 25, 2025

I force pushed to create a clean commit message

@Wodann Wodann added this pull request to the merge queue Apr 25, 2025
Merged via the queue into main with commit 1b6d123 Apr 25, 2025
17 checks passed
@Wodann Wodann deleted the fix/create-provider-async branch April 25, 2025 00:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants