How to abort subrequest properly?

Yichun Zhang (agentzh) agentzh at gmail.com
Thu Jul 4 05:43:42 UTC 2013


Hello!

On Wed, Jul 3, 2013 at 5:37 PM, Marcus Clyne wrote:
>> If the subrequest uses the upstream mechanism, wouldn't it be safe to just
>> close the socket of upstream's connection?  I'm assuming there would be an
>> entry in the error log, but is there anything harmful that could come from
>> it?
>>
> And you'd obviously want to finalize the subrequest, probably do something
> like ngx_http_finalize_request(sr, NGX_OK).  I don't think you could just
> issue this on its own, since I think the subrequest's sockets wouldn't
> automatically be closed until the main request was finalized (but it's been
> a while since I've done core hacking, and I can't remember for sure).
>

No. For modules based on ngx_http_upstream, the right way to shut it
down is to call ngx_http_upstream_finalize_request. This is exactly
what the cleanup handler registered by ngx_http_upstream does (i.e.,
the gx_http_upstream_cleanup function). And that's why I propose the
solution of calling the cleanup handler on the subrequest.

Calling ngx_http_finalize_request(sr, NGX_OK) does not really help
here because the subrequest's cleanup handler that actually shuts down
the upstream socket (and other resources like the resolver) will not
be triggered until the main request is cleaned up.

On the other hand, calling ngx_http_upstream_finalize_request directly
on the subrequest should work for strict upstream modules but this is
just too specific and hacky to be really interesting for any descent
3rd-party modules :)

Best regards,
-agentzh



More information about the nginx-devel mailing list