cargo: Cargo build failed with spurious network error
Problem
error: failed to load source for a dependency on rand
Steps
$ cargo run --verbose
Updating registry `https://github.com/rust-lang/crates.io-index`
warning: spurious network error (2 tries remaining): failed to send request: 无法与服务器建立连接
; class=Os (2)
warning: spurious network error (1 tries remaining): failed to send request: 无法与服务器建立连接
; class=Os (2)
error: failed to load source for a dependency on `rand`
Caused by:
Unable to update registry `https://github.com/rust-lang/crates.io-index`
Caused by:
failed to fetch `https://github.com/rust-lang/crates.io-index`
Caused by:
failed to send request: 无法与服务器建立连接
; class=Os (2)
The chinese sentences above are “Unable to establish connection to server”. I have check my firewall, cargo isn’t blocked, and there is personal CA installed by Adguard, no proxy.
Notes
OS: Windows 7 64bit, mingw64 with MSYS2
Output of cargo version: cargo 1.29.0
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 33
- Comments: 103 (14 by maintainers)
Commits related to this issue
- test: turn off multiplexing for cargo on cucumber (#3533) Description --- Turn off [http multiplexing](https://doc.rust-lang.org/cargo/reference/config.html#httpmultiplexing) for cargo on the int... — committed to tari-project/tari by deleted user 3 years ago
- Try a workaround for armv6 and armv7 See: https://github.com/pyca/cryptography/issues/6673#issuecomment-985943023 See: https://github.com/rust-lang/cargo/issues/6513 — committed to sudo-bot/docker-rustpython by williamdes a year ago
- Try another workaround for armv6 and armv7 See: https://github.com/rust-lang/cargo/issues/6513#issuecomment-864582147 See: https://github.com/rust-lang/cargo/issues/6513 — committed to sudo-bot/docker-rustpython by williamdes a year ago
- Try another workaround for armv6 and armv7 See: https://github.com/rust-lang/cargo/issues/6513#issuecomment-1425753938 See: https://github.com/rust-lang/cargo/issues/6513 — committed to sudo-bot/docker-rustpython by williamdes a year ago
- Remove hook that isn't working Caused by rust-lang/cargo#6513 — committed to qexat/prideflag by qexat a year ago
Possible solution for git bash users
Had the same issue, while trying to add
rand = "0.8.3"as therust-bookinstructs. I was getting the following error:failed to get `rand` as a dependency of package" `guessing_game v0.1.0`Found a workaround online. Turns out i had to create the config file under
~/.cargo(wasn’t created automatically) and add a network configuration.OS: Windows 10
cargo: 1.53.0 (4369396ce 2021-04-27) rustc: 1.53.0 (53cb7b09b 2021-06-17) git: 2.31.1.windows.1
@alexcrichton I am trying different projects in Windows Ubuntu 18.04 WSL - i.e. actix/examples. So cloned down the repo, go to the “hello-world” example and do cargo run. Get spurious network error. Firewall is turned off, no antivirus running, etc.
Posting here in case this helps anyone else.
I seem to have “solved” this problem in my situation by using
CARGO_HTTP_MULTIPLEXING=false cargo build. The issue was occurring mainly on when runningcargo buildas part of building a Docker image on Docker Hub and the same issue occurred when we tried using GitHub Actions build machines instead. Disabling HTTP/2 multiplexing forcing fallback to HTTP/1.1 seems to have “solved” the problem, repeated builds of two different affected Docker images have succeeded every time since the change.The kind of issues I was seeing were of the form:
After lots of delays and retries the build would eventually fail with something like:
For context the base image was Alpine Linux 3.13.x with cargo installed using
RUN apk add rust cargowhich installed packages with version(1.47.0-r2). The docs for the Cargo setting are at https://doc.rust-lang.org/cargo/reference/config.html#httpmultiplexing.My situation is because my git proxy is configured, and I temporarily turned off the git proxy to fix it.
It’s save my life. 😘
This was the fix for me! Thank you very much. I experienced the problems when building with Docker (Alpine Base). It was a cross ARCH build. Interestingly when I was build native there was not an issue. If anyone points that into a direction that helps.
I have had the same problem today but I could fix it.
The way is: Open a file, ~/.gitconfig Found [url “github:”] section? Comment out or delete all of the section like this:
Have fun!
Reference: https://github.com/rust-lang/cargo/issues/2605 Reference (Japanese): https://kokeshing.com/unable-to-update-registry-error-cargo/
seeing loads of failing github actions in the last day or so due to these network issues
The source blocked in China. And i fix it by:
I got the same issue on Windows 7 and it looks like the reason behind that is
cargocannot find its way through our corporate NTLM proxy.I was troubleshooting this with plain
curl. When I try to runI get the response
which implies that I need to authenticate in the proxy. By using
curlwith-vvoption I can see that it tries to authenticate using NTLM:Then what I did I added the following environment variables via Control Panel (the addresses are distorted on purpose):
After that I was able to receive the crates index using the following command:
It worked, it also worked with
--proxy-ntlminstead of--proxy-any, but if I don’t specify this parameter, I get the 407 error again. Instead of adding environment vars for user and password, I also was able to specify--proxy-user NT_DOMAIN\usernameand provide the password interactively - and it also worked.So it looks like at the moment rust’s
cargolacks support for NTLM proxy specifics, including authentication. Given thatcurldoes support this, I think it is just a matter of adding this support in future versions.Ideally, it would be nice to be able to to specify in
~/.cargo/config:Hm is this perhaps a network-level issue where github.com or github.com/rust-lang is blocked? Otherwise I’m not sure how we’d fix this as it seems Cargo can’t connect 😦
now i fixed it. in china, we need to set a china source for the crates: cd $HOME/.cargo nano config cat config
it shows:
[net] retry = 2 # network retries git-fetch-with-cli = true
[source.crates-io] registry = “https://github.com/rust-lang/crates.io-index” replace-with = ‘ustc’
[source.ustc] registry = “git://mirrors.ustc.edu.cn/crates.io-index”
cd /path/to/some-rust-project/ cargo build --release
then u are set.
The project comes from a tutorial. The following is
Cargo.toml.During my investigations on this issue I have found a workaround, which at least works in my network topology with NTLM corporate proxy.
First of all, there are two channels, through which cargo is communicating with crates index. One is libcurl (when accessing crates index file) and the other is libgit2 (when actually doing the subsequent search and fetch from the index). While for the first part your http_proxy / HTTP_PROXY settings at environment level might work, the reason it breaks is actually lying underneath - it breaks when libgit2 code is trying to actually fetch the index repo from GitHub via git protocol.
Some enterprises strictly prohibit access to https://github.com, but reasonable ones enable and control the access via corporate directories, e.g. Active Directory groups, and the user can be authorised when requests are going through NTLM proxy with authentication - authenticated users can be checked against the ACL in Active Directory and allowed access. So the question is how to invoke this authorisation each time a request is made.
If the above is true, one can usually fetch GitHub repos from behind the proxy with a command-line git via configuring proxy in git config (see below).
So while https://github.com/rust-lang/cargo/issues/7330 is still in the works, here’s my workaround.
I have made sure my command-line git finally works by adding configs for the NTLM proxy and my company’s CA bundle which contains the certificate chain and root CA certificate which signs certificates for company’s internal services (should be provided by your company):
You may also try the line below if getting SSL certificate-related errors:
I modified my Cargo config file: in
~/.cargo/config: I have switched to command-line git instead of using libgit2 code:One last step that did the trick. Define your environment variables (http_proxy / HTTP_PROXY depending on your platform) with your username/password provided, e.g. on Windows define your environment variable HTTP_PROXY this way:
Note the domain which should be the Windows domain of your account; then make sure your username and password are correctly URL-quoted (you may use online service such as https://www.urlencoder.org to check encoding for the special symbols, do not paste your entire password to those online services). Note that
%5Cabove stands for the backslash delimiting domain from username.Then that should be it. It worked for me - cargo was finally able to fetch the dependencies. Let me know if it helped in any way.
The only workaround that worked for me on my Dockerfile was
I tried all other workarounds but they did not work Commit : https://github.com/sudo-bot/docker-rustpython/commit/283fdda7f5f01aa111683d541f77bc965f1b10e2
It happened to me also, I’m using simple
docker build -t myapp .command. here is my Dockerfile.That solution helped me get through the initial download issue (listed by OP). But now it fails with “Peer certificate cannot be authenticated with given CA certificates (SSL certificate problem: certificate has expired)” on https://crates.io/api/v1/adler/1.0.2/download
MacOS 10.14.5 Cargo 1.70.0-nightly Firewall is off.
I have 0 issues with this on Windows or Linux, but for some reason MacOS can’t build.
UPDATE: Calling
cargo runmultiple times resolves the new ssl issue. https://github.com/rust-lang/cargo/issues/11670 Still strange, and would be nice if that wasn’t required.It’s amazing this is still open in 2023! My fix was to allow the crate.exe through Windows Defender Firewall.
In my case I am setting up a new rust-up cargo install on a Windows 10 machine with latest git cli installed. I used the 64-bit Windows installer from the downloads page because the chocolatey installer appeared out of date with 1.25.1 instead of 1.25.2 and flagged with some scan warnings.
https://community.chocolatey.org/packages/rustup.install
Was testing the setup using the examples crate.
The --vv command didn’t show any additional details.
Windows Firewall was not blocking the git client and was able to run both the inbuilt cargo git and use git cli git fetch options.
This worked fine both inside and outside of cargo, turning on and off the git fetch with cli option. Note the newer gh cli might be a good option to add to the list.
It was not the git step which was failing, it was the accessing of crates.io URL via a dynamic 6xxxx port.
Tested with curl and got following error
Allow an app through Windows Firewall might work, though I went the port route. https://support.microsoft.com/en-us/windows/risks-of-allowing-apps-through-windows-defender-firewall-654559af-3f54-3dcf-349f-71ccd90bcc5c
I am using Malwarebytes Windows Firewall which added this pretty broad rule: %USERPROFILE%.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\crate.exe Allow Out Local ports: Any Remote addresses: Any Remote Ports: Any Protocol: Any
Adding add rule to Windows Defender Firewall to the Windows installer or finding alternative approach might solve this particular issue.
@briteming 后面没事了,我也不知道怎么解决的
Hi, I had a similar issue recently.
cargo buildfailed with thisspurious network errormessage. I figured out that it was trying to clone a git repository asgit://.... I triedgit clone git://...manually and it really failed, however,git clone https://...worked. I addedto ~/.gitconfig as described here and then finally cargo worked, too.
I have the same issue with WSL. It seems to be related with filesystem.
With a folder mapped to my Windows filesystem:
With /tmp:
There are quite some bugs about mmap in WSL:
I found that it was indeed a network problem and solved it. If you do not have alternative choice to fetch what you want, you can try using SS or SSR, just go Option Settings (选项设置)->Local Proxy (本地代理), tick “Allow connections from LAN” (允许来自局域网的连接) and set Local Port (本地端口) = 1080. Then append the following to your .zshrc or .bashrc file:
Then cargo starts fetching successfully.
I use a similar solution with an additional “git@github.com” (for accessing private repo on github). It seems like the daemon service of git protocol doesn’t work well with my proxy.
Thannks. It works.
I had the same issue redently. Thank for@sikinmettugi 's inspiration. I had solved it by close my VPN proxy service. A word of warning: If you’re using a VPN proxy software in mainland China, it may cause the same
spurious network error. All you need to do is turn off the system proxy.Yesterday i had the same issue which lead me to digg into the problem . I just wanted to point out @weiby3 @alexcrichton the possibility that this may happens when cargo try to write into cache folder and because:
and the user you work with won’t have the required permissions under the cache folder i.e /usr/local/cargo/registry/cache (this would depends on you cargo installation). so. giving the right permission with i.e
sudo chown -R $(whoami) /usr/local/cargo/registry/cachePossibly should we consider a relation with this issue too https://github.com/rust-lang/cargo/issues/6757
If anyone on gentoo comes across this error, its apparently by design with portage. I dont even have a .cargo folder in my home directory so gentoo and librsvg-9999 git master cant be a thing on gentoo as of this post.
https://forums.gentoo.org/viewtopic-t-1134891-highlight-.html
EDIT: You can use “FEATURES=”-network-sandbox" in your make.conf file to bypass the emerge network sandbox and avoid this error.
@JohnCoconut This hadn’t broken anything else for me, but I don’t have any other packages that depend on http-parser, other than libgit2, and rust is the only thing that depends on libgit2 for me. I had just updated http-parser earlier that day. I don’t remember exactly what led me to it, but I tracked it into libgit2 through some of the error messages, looked through my recent updates, and found this arch linux bug that mentioned something of the sort, and then a quick rebuild of libgit2 sorted me out. I didn’t find anything in searches related to Gentoo, but I figured I’d put a PSA in here. Maybe somebody should open something in the Gentoo bug tracker. I’ll see if I can do that now. edit: Gentoo ticket opened now. Thanks for reminding me that the Gentoo community deserves a heads up.
PSA for people coming to this without any proxies in the mix (especially those who are on Gentoo like I am). This kind of issue can be caused by updating between the library http-parser versions 2.9.2, 2.9.3, and 2.9.4, as they apparently broke ABI compatibility. For me, rebuilding and reinstalling libgit2 fixed the issue.
Write permissions seems like a cause as well. I got this error using docker centost:8 mounting my app to /root. Even though I try to
cargo buildas root I get thoseCould not write data: Permission denied;...errors. I fixed it by simply mounting to another location.Solution that worked for me was updating my
/etc/hostsfile with new IP addresses for GitHub.+1 for this error even though git connectivity is available. Suggest this may be an SSL error.
For the recent reports, particularly for people using cargo provided via your Linux package manager, you may want to check which version of libcurl your package manager is using. 7.69.0 had a critical flaw in its HTTP2 handling that causes timeouts. I think 7.69.1 should fix it (or 7.68). See #7974 for more detail.
If you use the pre-built binaries distributed by rustup, they shouldn’t have this problem, since they statically link a version of libcurl that does not have that problem.
I found the source of the problem in our environment. Obviously the ZScaler proxy modifies the header of some HTTPS responses, specifically the
Transfer-Encoding: chunkedentry. It replaces it with_ransfer-Encoding: chunked. GnuTLS chokes on this, making the transfer fail. The solution was to rebuild everything with OpenSSL as backend. You can read more about that on https://github.com/curl/curl/pull/3148Similar problem here. I’m behind corporate proxy. Cloning the crates.io index from Github works fine but cargo fails with
There’s also a RST in the wireshark log somewhere during the transfer.
I try
git cloneandcurl https://github.com/rust-langand they works fine. On my computer github.com can be accessed successfully, I have search the firewall and there aren’t any block rule related to it. Maybe these are some useful information. I use HUORONG SECURITY as my anti-virus software, and if there is a program trying to connect to Internet, it will popup a windows and ask for permission. Strangely when I runcargo run --verbosethere is no popup window, but if I runcurl https://github.com/rust-langthere is a popup window, so it seems Cargo can’t connect. Is it possible to get more error information?