Skip to content

v2.7.18

Latest

Choose a tag to compare

@matsumotory matsumotory released this 02 Dec 16:21
e95707f

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.
    • What the documentation promised: Nginx::Async::HTTP::Response of 2.7.17 said Response#status returns the HTTP status of the subrequest's response.
  • 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本だけの場合は、変わりません。
  • 変更の理由:
    • 不具合:サブリクエストがハンドラを再開している最中に2本目の非同期の操作を出すと、リクエストが「変更前」に書いた不整合な状態になりました。詳細はGHSA-hxjh-2qj6-5xc7にあります。
    • 前の挙動を残せなかった理由:2.x系では、nginxはサブリクエストの終了処理の中からハンドラを再開します。そこからさらに非同期の操作を出すと、その操作を実行しつつリクエストを整合した状態に保つことはできません。3.0系は再開の仕組みを作り直し、nginxがきれいなスタックからハンドラを再開するので、複数の非同期の操作を続けて出せます。
    • ドキュメントが示していた挙動:2.7.17のNginx::Asyncは、Nginx::Async::HTTP.sub_requestとNginx::Async.sleepを説明していましたが、サブリクエストが再開したハンドラから2本目を出すことについては、何も書いていませんでした。
  • 影響の範囲:同じハンドラの中で、Nginx::Async::HTTP.sub_requestの後に2本目のNginx::Async::HTTP.sub_requestかNginx::Async.sleepを出すハンドラです。サブリクエストを1本だけ出すハンドラは、影響を受けません(Nginx::Async.sleepの後のサブリクエストも拒否します。挙動の変更2を参照してください)。
  • 必要な対応:2.x系では、1つのハンドラにつきNginx::Async::HTTP.sub_requestは1本にするか、RuntimeErrorをrescueしてください。1つのハンドラから複数のサブリクエストを続けて出すことは、3.0系で動きます。
  • テスト:ポート18210です(test/conf/conf.d/85-async-nested-reentry.conf、test/t/cases/async_nested_reentry.rb)。

2. Nginx::Async.sleepの後のNginx::Async::HTTP.sub_requestに続けて応答を書くと例外になる

  • 変更前:rewriteハンドラとaccessハンドラがNginx::Async.sleepを呼び、次にNginx::Async::HTTP.sub_requestを出し、そのサブリクエストが再開したハンドラからNginx.rputsかNginx.echoで応答を書くと、ハンドラが最初から何度も実行され、応答が終わりませんでした。読み続けるクライアントには、際限なく伸びる応答が返り、workerはその間動き続けました。応答を書かずにサブリクエストの後でNginx.return Nginx::DECLINEDで後続のフェーズに任せるハンドラは、答えを返しました(2.7.6以降)。
  • 変更後:そこで応答を書くと、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するハンドラは、変わりません。
  • 変更の理由:
    • 不具合:Nginx::Async.sleepの後、nginxはsleepのtimerからハンドラを再開しますが、そのリクエストのフェーズはこのハンドラのままです。サブリクエストが再開したハンドラから応答を書くと、フェーズがこのハンドラに留まり、モジュールのコンテキストも消えるため、サブリクエストが終わったときにnginxがそのフェーズとハンドラを再び実行し、それが際限なく続きました。declineするハンドラはフェーズを進めるので、答えを返しました。詳細はGHSA-hxjh-2qj6-5xc7にあります。
    • 前の挙動を残せなかった理由:2.x系では、sleepの後のハンドラをきれいなスタックから再開できないため、再開したハンドラから応答を書くと、実行しつつリクエストを終わらせることができません。3.0系は再開の仕組みを作り直し、そこで応答を書けます。
    • ドキュメントが示していた挙動:2.7.17のNginx::Asyncは、Nginx::Async::HTTP.sub_requestとNginx::Async.sleepを説明していましたが、sleepの後のサブリクエストに続けて応答を書くことについては、何も書いていませんでした。
  • 影響の範囲:同じハンドラの中で、Nginx::Async.sleepの後のNginx::Async::HTTP.sub_requestに続けてNginx.rputsかNginx.echoで応答を書く、rewriteハンドラとaccessハンドラです(contentハンドラは、sleepの後のNginx::Async::HTTP.sub_requestを2.7.3以降すでに例外にします)。サブリクエストの後でdeclineするハンドラ、サブリクエストが1本だけのハンドラ、後にサブリクエストを出さないsleepは、影響を受けません。
  • 必要な対応:sleepの後のサブリクエストに続けては、応答を書く代わりにNginx.return Nginx::DECLINEDで後続のフェーズに答えさせるか、RuntimeErrorをrescueしてください。そこで応答を書くことは、3.0系で動きます。
  • テスト:ポート18212です(test/conf/conf.d/87-async-sleep-subrequest.conf、test/t/cases/async_sleep_subrequest.rb)。

3. Nginx::Async::HTTP.last_responseが、応答ヘッダの前に終わったサブリクエストのstatusと空のbodyを返す

  • 変更前:Nginx::Async::HTTP.sub_requestの後、応答ヘッダの前に終わったサブリクエスト(接続できないかproxy_read_timeoutまでに応答しないupstream、nginx自身のreturn 404/301/503、例外を投げるかstatusを返すハンドラ)では、Nginx::Async::HTTP.last_responseはstatus 0を返し、同じハンドラの前のサブリクエストがあれば、そのヘッダとbodyを返しました。応答ヘッダをnginxが解析できないupstreamでは、bodyは、nginxが解析しなかったupstreamのヘッダの一部で、サブリクエストの応答ではありませんでした。
  • 変更後:last_responseは、サブリクエストの応答のstatusを返します。応答ヘッダの前に終わった場合は、nginxがそのサブリクエストを終了させたstatus(成功なら200、100以上の終了status、それ以外では500)を返します。bodyはサブリクエストの応答のbodyで、応答ヘッダの前に終わった場合は空です。ヘッダはサブリクエスト自身のものです。
  • 変更の理由:
    • 不具合:後処理は、サブリクエストにupstreamか出力のbufferがあるときだけlast_responseを設定していました。このため、応答ヘッダの前に終わったサブリクエストはstatus 0と前のサブリクエストのヘッダとbodyを残し、解析しなかったupstreamのヘッダの一部がbodyに入りました。詳細はGHSA-hxjh-2qj6-5xc7にあります。
    • 前の挙動を残せなかった理由:0は応答が持つstatusではなく、解析しなかったupstreamのヘッダの一部はサブリクエストの応答ではありません。lua-nginx-moduleのngx_http_lua_post_subrequestと同じく、終了statusと空のbodyを返すほうが、はっきり決まります。
    • ドキュメントが示していた挙動:2.7.17のNginx::Async::HTTP::Responseは、Response#statusがサブリクエストの応答のHTTP statusを返すと書いていました。
  • 影響の範囲:応答ヘッダの前に終わるサブリクエストについてNginx::Async::HTTP.last_responseを読むハンドラです。
  • 必要な対応:ありません。last_responseはstatus、空のbody、サブリクエストのヘッダを返します。
  • テスト:ポート18211です(test/conf/conf.d/86-async-subrequest-response.conf、test/t/cases/async_subrequest_response.rb)。