]> git.kaiwu.me - njs.git/commitdiff
Fetch: stop address failover once a response has started master
authorbasavaraj-sm05 <basavaraj@digiscrypt.com>
Sat, 5 Dec 2026 03:55:03 +0000 (19:55 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Sat, 5 Dec 2026 06:17:05 +0000 (22:17 -0800)
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).

nginx/ngx_js_http.c
nginx/ngx_js_http.h
nginx/t/js_fetch_resolver.t

index d9fb8e9612a279db724e5af0ec08802ce85fa7f6..9db4939fe6ed6194dc2d8ad0dcd6ba184ecf6b82 100644 (file)
@@ -595,6 +595,7 @@ ngx_js_http_process_connect_response(ngx_js_http_t *http)
     }
 
     http->proxy.state = HTTP_STATE_PROXY_TUNNEL_READY;
     }
 
     http->proxy.state = HTTP_STATE_PROXY_TUNNEL_READY;
+    http->response_started = 0;
 
     ngx_memzero(hp, sizeof(*hp));
 
 
     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");
 
 {
     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;
     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;
 
         if (n > 0) {
             b->last += n;
+            http->response_started = 1;
 
             rc = http->process(http);
 
 
             rc = http->process(http);
 
index 564bbfd9ffa768e4507b6a8f5f81c57037b6c7cc..cb4d51c3a7c9b3ad321a56089a4ac85ec6204815 100644 (file)
@@ -140,6 +140,7 @@ struct ngx_js_http_s {
     unsigned                       content_length:1;
     unsigned                       chunked:1;
     unsigned                       keepalive:1;
     unsigned                       content_length:1;
     unsigned                       chunked:1;
     unsigned                       keepalive:1;
+    unsigned                       response_started:1;
     unsigned                       body:3;
     off_t                          content_length_n;
 
     unsigned                       body:3;
     off_t                          content_length_n;
 
index 6768028314822235e53e90fd7b47570cf2e52f0c..726e18e566c9f076baca2de1b23b934146c8547e 100644 (file)
@@ -13,6 +13,7 @@ use strict;
 use Test::More;
 
 use IO::Select;
 use Test::More;
 
 use IO::Select;
+use Socket qw/ CRLF SOL_SOCKET SO_LINGER /;
 
 BEGIN { use FindBin; chdir($FindBin::Bin); }
 
 
 BEGIN { use FindBin; chdir($FindBin::Bin); }
 
@@ -98,6 +99,7 @@ EOF
 
 my $p0 = port(8080);
 my $p1 = port(8081);
 
 my $p0 = port(8080);
 my $p1 = port(8081);
+my $p2 = port(8082);
 
 $t->write_file('test.js', <<EOF);
     function test_njs(r) {
 
 $t->write_file('test.js', <<EOF);
     function test_njs(r) {
@@ -106,9 +108,11 @@ $t->write_file('test.js', <<EOF);
 
     const p0 = $p0;
     const p1 = $p1;
 
     const p0 = $p0;
     const p1 = $p1;
+    const p2 = $p2;
 
     async function dns(r) {
 
     async function dns(r) {
-        var port = r.args.port == undefined || r.args.port == 'p0' ? p0 : p1;
+        var port = r.args.port == 'p1' ? p1
+                   : r.args.port == 'p2' ? p2 : p0;
         var loc = r.args.loc == undefined ? 'loc' : r.args.loc;
         var url = `http://\${r.args.domain}:\${port}/\${loc}`;
 
         var loc = r.args.loc == undefined ? 'loc' : r.args.loc;
         var url = `http://\${r.args.domain}:\${port}/\${loc}`;
 
@@ -138,15 +142,25 @@ EOF
 
 $t->try_run('no njs.fetch');
 
 
 $t->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(\&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=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');
 
 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 '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;
        }
 
        $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;
+       }
+}
+
 ###############################################################################
 ###############################################################################