You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This commit was created on GitHub.com and signed with GitHub’s verified signature.
Behavior changes: read before upgrading
1. A second Nginx::Async operation from a handler a subrequest has resumed now raises
Before: a handler that called Nginx::Async::HTTP.sub_request and then, from the point the subrequest resumed it, a second Nginx::Async::HTTP.sub_request or Nginx::Async.sleep could end the worker process, leave the request without a response, or make it answer 500 with request count is zero in the error log. nginx resumes the handler's fiber from the post handler of the subrequest, inside its finalization of the subrequest, and the further asynchronous operation ran on that stack.
Now: the second operation raises a RuntimeError (... is not available while the response of a subrequest resumes the handler) before it posts a subrequest or arms a timer, so the handler can rescue it and the request finishes. A single subrequest is unchanged.
Why it changed:
The defect: a second asynchronous operation issued while a subrequest resumed the handler left the request in the inconsistent state that "Before" describes. GHSA-hxjh-2qj6-5xc7 has the details.
Why the old behavior could not be kept: on the 2.x line nginx resumes the handler from inside its finalization of the subrequest, so a further asynchronous operation from there cannot both run and leave the request consistent. The 3.0 line reworks the resume model, so that nginx resumes the handler from a clean stack, where a sequence of asynchronous operations runs.
What the documentation promised: Nginx::Async of 2.7.17 described Nginx::Async::HTTP.sub_request and Nginx::Async.sleep, and said nothing about a second one from a handler a subrequest has resumed.
Affected: a handler that issues a second Nginx::Async::HTTP.sub_request or Nginx::Async.sleep after a Nginx::Async::HTTP.sub_request in the same handler. A handler that issues a single subrequest is unaffected. (A subrequest after Nginx::Async.sleep is refused as well; see behavior change 2.)
What to do: issue one Nginx::Async::HTTP.sub_request per handler on the 2.x line, or rescue the RuntimeError. A sequence of subrequests from one handler runs on the 3.0 line.
Tests: port 18210 (test/conf/conf.d/85-async-nested-reentry.conf, test/t/cases/async_nested_reentry.rb).
2. Writing the response after Nginx::Async::HTTP.sub_request that follows Nginx::Async.sleep now raises
Before: a rewrite or access handler that called Nginx::Async.sleep, then Nginx::Async::HTTP.sub_request, and then wrote the response with Nginx.rputs or Nginx.echo from the handler that the subrequest resumed, ran the handler again and again and never finished the response, so a client that kept reading received an ever-growing reply and the worker spun. A handler that declined (Nginx.return Nginx::DECLINED) after the subrequest, instead of writing the response, answered (since 2.7.6).
Now: writing the response there raises a RuntimeError (a response cannot be written after Nginx::Async::HTTP.sub_request that follows Nginx::Async.sleep). A handler can rescue it and let a later phase answer by declining, as a handler that declines after the subrequest has done. Not rescued, the request gets 500. A single subrequest, Nginx::Async.sleep with no later subrequest (including a sleep after a sleep), and a subrequest the handler declines after, are unchanged.
Why it changed:
The defect: after Nginx::Async.sleep nginx resumes the handler from the sleep's timer with the request's phase still at this handler; writing the response from the handler that the subrequest then resumed left the phase there and cleared the module context, so nginx ran the phase and the handler again when the subrequest ended, without end. A handler that declined advanced the phase instead, so it answered. GHSA-hxjh-2qj6-5xc7 has the details.
Why the old behavior could not be kept: on the 2.x line nginx cannot resume the handler after the sleep from a clean stack, so writing the response from the resumed handler cannot both run and finish the request. The 3.0 line reworks the resume model and writes the response there.
What the documentation promised: Nginx::Async of 2.7.17 described Nginx::Async::HTTP.sub_request and Nginx::Async.sleep, and said nothing about writing the response after a subrequest that follows a sleep.
Affected: a rewrite or access handler that writes the response with Nginx.rputs or Nginx.echo after a Nginx::Async::HTTP.sub_request that follows Nginx::Async.sleep in the same handler. (A content handler already raises Nginx::Async::HTTP.sub_request after a sleep, since 2.7.3.) A handler that declines after the subrequest, a single subrequest, and a sleep with no later subrequest are unaffected.
What to do: after a subrequest that follows a sleep, let a later phase answer with Nginx.return Nginx::DECLINED instead of writing the response, or rescue the RuntimeError. The 3.0 line writes the response there.
Tests: port 18212 (test/conf/conf.d/87-async-sleep-subrequest.conf, test/t/cases/async_sleep_subrequest.rb).
3. Nginx::Async::HTTP.last_response reports the status and an empty body of a subrequest that ended before a response header
Before: after Nginx::Async::HTTP.sub_request, Nginx::Async::HTTP.last_response reported status 0 for a subrequest that ended before a response header (an upstream that could not be connected to or did not answer within proxy_read_timeout, nginx's own return 404/301/503, a handler that raised or returned a status), and the headers and body of an earlier subrequest of the same handler, if there was one. For an upstream whose response header nginx could not parse, the body was the part of the upstream's header that nginx had not parsed, not the subrequest's response.
Now: last_response reports the subrequest's response status, or, when the subrequest ended before a response header, the status nginx finalized it with (200 for a success, the finalization status from 100 up, or 500). The body is the subrequest's response body, and empty for a subrequest that ended before a response header. The headers are the subrequest's own.
Why it changed:
The defect: the post handler set last_response only when the subrequest had an upstream or an output buffer, so a subrequest that ended before a response header kept status 0 and the headers and body of an earlier subrequest, and the unparsed part of an upstream's header reached the body. GHSA-hxjh-2qj6-5xc7 has the details.
Why the old behavior could not be kept: 0 is not a status a response has, and the unparsed part of an upstream's header was not the subrequest's response. Reporting the finalization status and an empty body, as lua-nginx-module's ngx_http_lua_post_subrequest does, is well-defined.
Affected: a handler that reads Nginx::Async::HTTP.last_response for a subrequest that ends before a response header.
What to do: nothing; last_response reports the status, an empty body and the subrequest's headers.
Tests: port 18211 (test/conf/conf.d/86-async-subrequest-response.conf, test/t/cases/async_subrequest_response.rb).
更新前に確認する挙動の変更
1. サブリクエストが再開したハンドラの2本目のNginx::Async操作が例外になる
変更前:Nginx::Async::HTTP.sub_requestを呼び、そのサブリクエストがハンドラを再開した時点から、2本目のNginx::Async::HTTP.sub_requestかNginx::Async.sleepを出すと、workerプロセスが終わる、応答が返らない、またはrequest count is zeroをerror.logに出して500を返す、のいずれかが起きることがありました。nginxは、サブリクエストの終了処理の中で、その後処理からハンドラのfiberを再開します。2本目の非同期の操作は、その途中で実行されていました。
変更後:2本目の操作は、サブリクエストを張ったりtimerを仕掛けたりする前にRuntimeError(... is not available while the response of a subrequest resumes the handler)を投げます。ハンドラはこれをrescueでき、リクエストは最後まで進みます。サブリクエストが1本だけの場合は、変わりません。
変更後:そこで応答を書くと、RuntimeError(a response cannot be written after Nginx::Async::HTTP.sub_request that follows Nginx::Async.sleep)を投げます。ハンドラはこれをrescueし、サブリクエストの後でdeclineするハンドラと同じく、後続のフェーズに答えさせることができます。rescueしなければ、リクエストは500になります。サブリクエストが1本だけの場合、後にサブリクエストを出さないNginx::Async.sleep(sleepの後のsleepを含む)、サブリクエストの後でdeclineするハンドラは、変わりません。