Rails 应用程序在未处理的路由上出现 Heroku H18 错误

Heroku H18 error in a Rails app on an unhandled route

我在 Heroku 上遇到神秘的 H18 错误。这些请求似乎是由机器人或爬虫在我的应用程序的根目录 URL 上发送 POST 请求发出的。让我担心的是,这些请求在被终止之前都需要超过 30 秒的时间来处理,很可能是超时。这是我的日志文件和指标图的示例:

Dec 14 03:52:26 poll-en heroku/router sock=backend at=error code=H18 desc="Server Request Interrupted" method=POST path="/" host=app.do request_id=72252a6a-d4b5-4ecc-ae3c-bc69c273eb16 fwd="138.201.76.138" dyno=web.1 connect=1ms service=30034ms status=503 bytes=71 protocol=http
Dec 14 04:05:11 poll-en heroku/router sock=backend at=error code=H18 desc="Server Request Interrupted" method=POST path="/" host=app.do request_id=78ea5ae4-9e8a-4c90-b26e-c2eae40116b4 fwd="148.251.231.105" dyno=web.1 connect=0ms service=31392ms status=503 bytes=71 protocol=http
Dec 14 04:19:07 poll-en heroku/router sock=backend at=error code=H18 desc="Server Request Interrupted" method=POST path="/" host=app.do request_id=bcc76545-24e0-4fc7-8e63-a08bb463bb31 fwd="148.251.231.105" dyno=web.1 connect=0ms service=30195ms status=503 bytes=71 protocol=http
Dec 14 04:45:35 poll-en heroku/router sock=backend at=error code=H18 desc="Server Request Interrupted" method=POST path="/" host=app.do request_id=87efe086-9879-4506-a7ac-52504219126d fwd="144.76.141.230" dyno=web.1 connect=0ms service=31733ms status=503 bytes=71 protocol=http

我的应用程序不处理根 URL 上的 POST 请求,我无法使用 cURL 复制错误。可以肯定的是,我还添加了一个 Rack 中间件,它首先运行并终止根 URL 上的所有 POST 请求。所以请求甚至不应该到达 Rails 路由器,但我的日志文件中仍然出现这些 H18 错误。

怎么会这样?什么样的请求会在甚至没有到达我的应用程序时挂起 30 秒?

您使用的是什么版本的 Puma?您的 Puma 配置是什么样的?

Heroku documents 当 H18 发生时请求确实到达您的应用:

An H18 signifies that the socket connected, some data was sent as part of a response by the app, but then the socket was destroyed without completing the response.

也许您在 Puma 中遇到了这个错误,听起来请求可能会卡在 Puma 中(永远不会到达您的应用程序):https://github.com/puma/puma/issues/2282(在 Puma 5.0.3 及更高版本中解决)

您可以尝试在 Heroku 给您 H18 错误之前设置 first_data_timeout in your Puma config to something that's lower than 30 seconds. Then Puma should respond with 408 Request Timeout