Skip to content

libstore: curl retry: reset content-encoding and don't use string after move - #12603

Merged
Mic92 merged 1 commit into
NixOS:masterfrom
NaN-git:fix-curl-retry
Mar 12, 2025
Merged

Mic92 merged 1 commit into
NixOS:masterfrom
NaN-git:fix-curl-retry

Conversation

@NaN-git

@NaN-git NaN-git commented Mar 4, 2025

Copy link
Copy Markdown
Contributor

Motivation

While analyzing #12419 I discovered that in case of a HTTP error 429 the retry behavior is problematic. The same TransferItem is issued again. decompressionSink is not reinitialized, so that it might be used after finish() is called. Furthermore it's possible that errorSink is used again after errorSink->s is moved.

Additionally the Content-Encoding header of the HTTP 429 response and of the HTTP 200 response might be different. In this case the decompression of the HTTP 200 response body might fail.

Context

Might fix #12419, although I wasn't able to provoke any HTTP error 429 on Github. Github seems to prefer gzip encoding and the response body of the error response shown in the issue seems to be short, but not empty. Thus it seems to be unlikely that this error response contained the header Content-Encoding: gzip, while the reply with the content probably had it set. More details about the responses would be helpful.


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@NaN-git
NaN-git requested a review from Ericson2314 as a code owner March 4, 2025 18:06
@roberth

roberth commented Mar 4, 2025

Copy link
Copy Markdown
Member

I wasn't able to provoke any HTTP error 429 on Github.

Maybe the fake github in tests/nixos/github-flakes.nix might be of use?

@roberth roberth added the fetching Networking with the outside (non-Nix) world, input locking label Mar 4, 2025
@roberth roberth added this to Nix team Mar 4, 2025
@github-project-automation github-project-automation Bot moved this to To triage in Nix team Mar 4, 2025
@roberth roberth removed this from Nix team Mar 4, 2025
@NaN-git

NaN-git commented Mar 5, 2025 •

Copy link
Copy Markdown
Contributor Author

@roberth The problem is that I don't know exactly which replies Github sent. But here are instruction how to trigger the bug:

  • Start this HTTP server:
from http.server import BaseHTTPRequestHandler, HTTPServer

def gzipencode(content):
    from io import BytesIO
    import gzip
    out = BytesIO()
    f = gzip.GzipFile(fileobj=out, mode='w', compresslevel=5)
    f.write(content.encode())
    f.close()
    return out.getvalue()

strcontent = """{
  x86_64-linux = "/nix/store/kwck2vdfdp2v4jr9c4daryyk9mlbx406-nix-2.24.12";
  i686-linux = "/nix/store/s9qqyxzcgjl7xrfhnnjiiy9v876pcphi-nix-2.24.12";
  aarch64-linux = "/nix/store/pmj4g05d4nlr3gcr8nyadgwir3svbkmx-nix-2.24.12";
  riscv64-linux = "/nix/store/nzr3m4x3mcnfpnmyap31f9pviwv29vyc-nix-riscv64-unknown-linux-gnu-2.24.12";
  x86_64-darwin = "/nix/store/pf6msb0yzccznd75yil32mzk284h90z4-nix-2.24.12";
  aarch64-darwin = "/nix/store/vaaakw66qnbw4g007rf2nggy0rmhf8fh-nix-2.24.12";
}"""
strerror = "429: Too Many Requests"

content = gzipencode(strcontent)
error = gzipencode(strerror)

class TestHTTP429(BaseHTTPRequestHandler):
    GZIP_ERROR = False
    ctr = 0

    def do_GET(self):
        fail = TestHTTP429.ctr % 2 == 0
        TestHTTP429.ctr = TestHTTP429.ctr + 1

        self.protocol_version = 'HTTP/1.1'
        if fail:
            body = error if TestHTTP429.GZIP_ERROR else strerror.encode()
            self.send_response(429)
        else:
            body = content
            self.send_response(200)
        self.send_header("Content-Type", "text/plain; charset=utf-8")
        self.send_header("Accept-Ranges", "bytes")
        self.send_header("Content-Length", str(len(body)))
        if not fail or TestHTTP429.GZIP_ERROR:
            self.send_header("Content-Encoding", "gzip")
        self.end_headers()
        self.wfile.write(body)
        self.wfile.flush()


if __name__ == "__main__":
    server = HTTPServer(("127.0.0.1", 10000), TestHTTP429)
    server.serve_forever()
  • Trigger the bug with nix-prefetch-url -vvv http://127.0.0.1:10000/test.nix

With GZIP_ERROR = False a gzip compressed file ends up without decompression in the Nix store with the first bytes being 1f 8b 08 00, i.e. non-printable characters followed by a null byte. This explains the syntax error in #12419, which looks like an empty string.

If GZIP_ERROR = True is used, then an assertion is triggered in the code:

terminating due to unexpected unrecoverable internal error: Unexpected condition in operator() at src/libutil/serialise.cc:237

My patch does not fail this test. Maybe a test case should be added, i.e. with a fake HTTP server that sends two hard coded responses.

Edit: Change error message to match #12419 and simplify code

@Mic92
Mic92 merged commit c9a3101 into NixOS:master Mar 12, 2025
@Mic92 Mic92 added the backport 2.28-maintenance Automatically creates a PR against the branch label Apr 15, 2025
@Mic92

Mic92 commented Apr 15, 2025

Copy link
Copy Markdown
Member

@SuperSandro2000 added backport label.

Mic92 added a commit that referenced this pull request Apr 15, 2025
…2603

libstore: curl retry: reset content-encoding and don't use string after move (backport #12603)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 2.28-maintenance Automatically creates a PR against the branch fetching Networking with the outside (non-Nix) world, input locking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"nix upgrade-nix" sometimes fails with GitHub 429 "Too Many Requests" error

3 participants