From: basavaraj-sm05 Date: Sat, 5 Dec 2026 03:55:03 +0000 (-0800) Subject: Fetch: stop address failover once a response has started X-Git-Url: https://git.kaiwu.me/web/http/static/ngx_http_perl_module.html?a=commitdiff_plain;ds=sidebyside;p=njs.git Fetch: stop address failover once a response has started Previously, ngx_js_http_next() reconnected to the next resolved address after a recv() or send() error at any stage of the exchange, resetting only http->buffer. If a read error occurred after the first peer had already sent part of the response, the retry reused the dirty parser and response state and an already destroyed request chain. Now address failover is disabled once response bytes have been received from the peer, and the fetch fails with "prematurely closed connection" just like on an unexpected EOF. For HTTPS via a forward proxy, the flag is reset once the CONNECT tunnel is established so origin connect failures are still handled as before. The issue was introduced in 73c75e41 (0.5.1). --- diff --git a/nginx/ngx_js_http.c b/nginx/ngx_js_http.c index d9fb8e96..9db4939f 100644 --- a/nginx/ngx_js_http.c +++ b/nginx/ngx_js_http.c @@ -595,6 +595,7 @@ ngx_js_http_process_connect_response(ngx_js_http_t *http) } http->proxy.state = HTTP_STATE_PROXY_TUNNEL_READY; + http->response_started = 0; ngx_memzero(hp, sizeof(*hp)); @@ -618,6 +619,11 @@ ngx_js_http_next(ngx_js_http_t *http) { ngx_log_debug0(NGX_LOG_DEBUG_EVENT, http->log, 0, "js http next addr"); + if (http->response_started) { + ngx_js_http_error(http, "prematurely closed connection"); + return; + } + if (++http->naddr >= http->naddrs) { ngx_js_http_error(http, "connect failed"); return; @@ -745,6 +751,7 @@ ngx_js_http_read_handler(ngx_event_t *rev) if (n > 0) { b->last += n; + http->response_started = 1; rc = http->process(http); diff --git a/nginx/ngx_js_http.h b/nginx/ngx_js_http.h index 564bbfd9..cb4d51c3 100644 --- a/nginx/ngx_js_http.h +++ b/nginx/ngx_js_http.h @@ -140,6 +140,7 @@ struct ngx_js_http_s { unsigned content_length:1; unsigned chunked:1; unsigned keepalive:1; + unsigned response_started:1; unsigned body:3; off_t content_length_n; diff --git a/nginx/t/js_fetch_resolver.t b/nginx/t/js_fetch_resolver.t index 67680283..726e18e5 100644 --- a/nginx/t/js_fetch_resolver.t +++ b/nginx/t/js_fetch_resolver.t @@ -13,6 +13,7 @@ use strict; use Test::More; use IO::Select; +use Socket qw/ CRLF SOL_SOCKET SO_LINGER /; BEGIN { use FindBin; chdir($FindBin::Bin); } @@ -98,6 +99,7 @@ EOF my $p0 = port(8080); my $p1 = port(8081); +my $p2 = port(8082); $t->write_file('test.js', <write_file('test.js', <try_run('no njs.fetch'); -$t->plan(5); +$t->plan(7); $t->run_daemon(\&dns_daemon, port(8981), $t); $t->waitforfile($t->testdir . '/' . port(8981)); +$t->run_daemon(\&reset_daemon, $p2); +$t->waitforsocket('127.0.0.2:' . $p2); +$t->run_daemon(\&ok_daemon, $p2); +$t->waitforsocket('127.0.0.1:' . $p2); ############################################################################### like(http_get('/dns?domain=aaa'), qr/aaa:GET:::$/s, 'fetch dns aaa'); like(http_get('/dns?domain=many'), qr/many:GET:::$/s, 'fetch dns many'); +like(http_get('/dns?domain=partial&port=p2&loc=partial'), + qr/501.*prematurely closed connection/s, + 'fetch dns failover stops after partial status line'); +like(http_get('/dns?domain=midbody&port=p2&loc=midbody'), + qr/501.*prematurely closed connection/s, + 'fetch dns failover stops after response headers'); like(http_get('/dns?domain=unknown'), qr/"unknown" could not be resolved/s, 'fetch dns unknown'); @@ -220,6 +234,10 @@ sub reply_handler { } elsif ($name eq 'many' && $type == A) { push @rdata, rd_addr($ttl, '127.0.0.2'); push @rdata, rd_addr($ttl, '127.0.0.1'); + + } elsif (($name eq 'partial' || $name eq 'midbody') && $type == A) { + push @rdata, rd_addr($ttl, '127.0.0.2'); + push @rdata, rd_addr($ttl, '127.0.0.1'); } $len = @name; @@ -285,4 +303,73 @@ again: } } +sub reset_daemon { + my ($port) = @_; + + my $server = IO::Socket::INET->new( + Proto => 'tcp', + LocalAddr => '127.0.0.2:' . $port, + Listen => 5, + Reuse => 1, + ) or die "Can't create listening socket: $!\n"; + + local $SIG{PIPE} = 'IGNORE'; + + while (my $client = $server->accept()) { + $client->autoflush(1); + + my $headers = ''; + while (<$client>) { + $headers .= $_; + last if /^\x0d?\x0a?$/; + } + + my $uri = ''; + $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i; + + if ($uri eq '/partial') { + print $client "HTTP/1.1 200 O"; + } else { + print $client + "HTTP/1.1 200 OK" . CRLF . + "Content-Length: 100" . CRLF . + "Connection: close" . CRLF . CRLF . + "part"; + } + + select undef, undef, undef, 0.02; + setsockopt($client, SOL_SOCKET, SO_LINGER, pack('ii', 1, 0)); + close $client; + } +} + +sub ok_daemon { + my ($port) = @_; + + my $server = IO::Socket::INET->new( + Proto => 'tcp', + LocalAddr => '127.0.0.1:' . $port, + Listen => 5, + Reuse => 1, + ) or die "Can't create listening socket: $!\n"; + + local $SIG{PIPE} = 'IGNORE'; + + while (my $client = $server->accept()) { + $client->autoflush(1); + + while (<$client>) { + last if /^\x0d?\x0a?$/; + } + + print $client + "HTTP/1.1 200 OK" . CRLF . + "Content-Length: 2" . CRLF . + "Connection: close" . CRLF . CRLF . + "OK"; + + close $client; + } +} + ###############################################################################