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

close file descriptor #248

Merged
merged 7 commits into from
Mar 28, 2023
Merged

Conversation

matthewkeil
Copy link
Member

This PR closes the leaked file descriptor in node.js bindings.

Relates to #246

@matthewkeil
Copy link
Member Author

@asn-d6 I added the close check and matched the error message with the other similar errors in that function

Copy link
Contributor

@asn-d6 asn-d6 left a comment

Choose a reason for hiding this comment

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

LGTM! Feel free to merge!

@matthewkeil
Copy link
Member Author

If fclose fails the setup is free'd where the error is thrown not in the cleanup function for clarity

@asn-d6
Copy link
Contributor

asn-d6 commented Mar 28, 2023

@jtraglia i will wait for you to merge this one

@jtraglia
Copy link
Member

One problem, the file descriptor is still leaked if the call to load_trusted_setup_file fails.

@jtraglia
Copy link
Member

I believe that would work, but I would do it a little differently. I think this is simpler:

Napi::Value LoadTrustedSetup(const Napi::CallbackInfo& info) {
  Napi::Env env = info.Env();

  // Check if the trusted setup is already loaded
  KzgAddonData *data = env.GetInstanceData<KzgAddonData>();
  if (data->is_setup) {
    Napi::Error::New(env, "Error trusted setup is already loaded").ThrowAsJavaScriptException();
    return env.Undefined();
  }

  // Open the trusted setup file
  std::string file_path = info[0].As<Napi::String>().Utf8Value();
  FILE *file_handle = fopen(file_path.c_str(), "r");
  if (file_handle == nullptr) {
    Napi::Error::New(env, "Error opening trusted setup file: " + file_path).ThrowAsJavaScriptException();
    return env.Undefined();
  }

  // Load the trusted setup from that file
  C_KZG_RET ret = load_trusted_setup_file(&(data->settings), file_handle);

  // Close the trusted setup file
  if (fclose(file_handle) != 0) {
    if (ret == C_KZG_OK) {
      free_trusted_setup(&(data->settings));
    }
    Napi::Error::New(env, "Error closing trusted setup file").ThrowAsJavaScriptException();
    return env.Undefined();
  }

  // Check that it was successful
  if (ret != C_KZG_OK) {
    Napi::Error::New(env, "Error loading trusted setup file: " + file_path).ThrowAsJavaScriptException();
    return env.Undefined();
  }

  data->is_setup = true;
  return env.Undefined();
}

@matthewkeil
Copy link
Member Author

I believe that would work, but I would do it a little differently. I think this is simpler:

You are a gentleman and a scholar sir!

@jtraglia
Copy link
Member

LGTM 👍

@jtraglia jtraglia merged commit db8f425 into ethereum:main Mar 28, 2023
@jtraglia jtraglia deleted the node-close-file-desc branch March 28, 2023 20:23
asn-d6 added a commit to asn-d6/c-kzg-4844 that referenced this pull request Mar 29, 2023
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.

None yet

3 participants