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: limit the max amount of concurrent network requests #116

Closed
wants to merge 6 commits into from

Conversation

zkochan
Copy link
Member

@zkochan zkochan commented Sep 16, 2023

Without limiting the amount of concurrent network requests, installation fails with too many open files error

Without limiting the amount of concurrent network requests, installation fails with too many open files error
@github-actions
Copy link

github-actions bot commented Sep 16, 2023

Benchmark Results

Linux

group                          main                                   pr
-----                          ----                                   --
tarball/download_dependency    1.00      7.9±2.88ms   549.4 KB/sec    1.00      7.9±2.81ms   551.8 KB/sec

.unwrap();
self.install_dependencies(&dependency).await;
.map(|(name, version)| {
let semaphore_clone = semaphore.clone();
Copy link
Contributor

@KSXGitHub KSXGitHub Sep 16, 2023

Choose a reason for hiding this comment

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

I recommend Arc::clone(&semaphore) to avoid confusing it with regular expensive .clone().

@KSXGitHub
Copy link
Contributor

Is this related to the error on macOS?

@@ -55,17 +57,19 @@ impl PackageManager {
package
.dependencies(self.config.auto_install_peers)
.map(|(name, version)| async {
let semaphore_clone = semaphore.clone();
Copy link
Contributor

Choose a reason for hiding this comment

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

semaphore is not an Arc here. Cloning it would create a new Semaphore. Is this intentional?

@zkochan
Copy link
Member Author

zkochan commented Sep 16, 2023

Is this related to the error on macOS?

This is one of the errors on macOS. The other one is failed to lookup address information: nodename nor servname provided, or not known. I don't have a fix for that one yet.

Also, this PR is not ready yet. Looks like this change is causing a deadlock.

Copy link
Member

@anonrig anonrig left a comment

Choose a reason for hiding this comment

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

Can you add a test so that we don't regress in the future?

@@ -59,13 +60,15 @@ impl PackageManager {
/// 5. Symlink all dependencies to node_modules/.pacquet/pkg@version/node_modules
/// 6. Update package.json
pub async fn add(&mut self, args: &AddCommandArgs) -> Result<(), PackageManagerError> {
let semaphore = Arc::new(Semaphore::new(16));
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be configurable somehow

@KSXGitHub
Copy link
Contributor

@KSXGitHub
Copy link
Contributor

Looks like this change is causing a deadlock.

I don't get deadlock on my machine. What is your package.json?

@zkochan
Copy link
Member Author

zkochan commented Sep 17, 2023

I am using this package.json

@KSXGitHub
Copy link
Contributor

The error is not unique to macOS, it seems.

screenshot

@zkochan
Copy link
Member Author

zkochan commented Sep 17, 2023

This is a different one. I also get this one. pnpm solves this by using graceful-fs, which retries the file system operations on these type of errors.

) -> Result<Self, RegistryError> {
let url = || format!("{registry}{name}"); // TODO: use reqwest URL directly
let network_error = |error| NetworkError { error, url: url() };
let _permit = semaphore.acquire().await;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason you gave this value a name? If you want to postpone dropping it, I recommend explicitly calling drop (i.e. drop(permit)) at the end of the scope. If you only want to .await the semaphore, please remove let _permit =.

Copy link
Member Author

Choose a reason for hiding this comment

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

won't it be dropped immediately if I don't assign it to a value? As far as I understand, the permit is dropped, when it goes out of scope.

@KSXGitHub
Copy link
Contributor

KSXGitHub commented Sep 17, 2023

Looks like this change is causing a deadlock.

I am using this package.json

After updating the package.json and switching branch to network-concurrency (this branch), what I get is not a deadlock, but instead (it seems) an infinite loop that logs endlessly.

You may try it with TRACE='pacquet::install,pacquet::download' to see for yourself.

@KSXGitHub
Copy link
Contributor

If I set the semaphore number to 1000, this error appears:

Error:   × Main thread panicked.
  ├─▶ at crates/cli/src/package_import.rs:38:26
  ╰─▶ expected no write errors: CreateLink { from: "/home/khai/Temp/test-pacquet/pacquet-home/store/7c/a8bebf69ba6047dd7be009132ae6a30992062657634c1573fa04f35cb9de7b31d4316606d61f748300eddc0cc91220ccaa8562814d430d218a6bd0a13471e2", to:
      "/home/khai/Temp/test-pacquet/node_modules/.pacquet/[email protected]/node_modules/dom-helpers/util/inDOM.js", error: Os { code: 24, kind: Uncategorized, message: "Too many open files" } }

But if I don't, the install process never stops.

@KSXGitHub
Copy link
Contributor

I also created a more "polished" version here: df491ea

Same result.

@zkochan
Copy link
Member Author

zkochan commented Sep 19, 2023

I think the issue might be that pacquet doesn't support circular dependencies yet.

@KSXGitHub
Copy link
Contributor

I think the issue might be that pacquet doesn't support circular dependencies yet.

It would helps a lot if you can reduce your package.json to only circular dependencies (for testing and implementing purposes).

@zkochan
Copy link
Member Author

zkochan commented Sep 20, 2023

As I have mentioned in #124, there are tests in pnpm related to circular dependencies. And there are small mock packages for testing: https://github.com/pnpm/registry-mock/tree/main/packages/circular-deps-1-of-2

@KSXGitHub KSXGitHub deleted the network-concurrency branch November 21, 2023 15:40
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