Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force close HTTP request/response #186

Closed
janbarasek opened this issue Nov 11, 2020 · 12 comments
Closed

Force close HTTP request/response #186

janbarasek opened this issue Nov 11, 2020 · 12 comments

Comments

@janbarasek
Copy link
Contributor

The Response service could include a method to force the termination of the current HTTP request.

This can be very useful if I want to send output to the user but continue running the script and, for example, finish saving to a database that the user does not have to wait for.

The use may be suitable for an API that stores data, but also for a Presenter that has already submitted a template and the user does not have to wait for background operations to complete.

If Tracy is active, it should prevent this feature from being rendered.

I could implement this feature myself, but I think the native support is great.

Thanks!

@JanTvrdik
Copy link
Contributor

You mean fastcgi_finish_request?

@janbarasek
Copy link
Contributor Author

@JanTvrdik Maybe yes, but this function can be sometimes unavailable:

Snímek obrazovky 2020-11-11 v 21 54 50

@JanTvrdik
Copy link
Contributor

Then I don't understand this issue. You cannot implement this in userland.

@dg
Copy link
Member

dg commented Jan 5, 2021

ping @janbarasek

@janbarasek
Copy link
Contributor Author

@dg I'm not sure this is Nette's responsibility, but it would be nice to be able to control all the output to the browser via the Http\Response object.

For example, a user invokes a long-running task in the Presenter, so I send him a quick response (Latte template), but the task continues to run in the background even after the user closes the connection and all headers are sent correctly.

This allows, for example, to respond to a request very quickly, but to write data to the database only after the user does not have to wait for it.

If this does not make sense, please close the issue. Thanks.

@dg
Copy link
Member

dg commented Jan 6, 2021

The question is, how to implement it? Is there a function for that?

@milo
Copy link
Member

milo commented Jan 6, 2021

There are some attempts in comments: https://www.php.net/manual/en/features.connection-handling.php

For such long-term running cases, I usually render ordinary response and from browser then, invoke WebSockets/Ajax request with ignore_user_abort() on server side.

@mabar
Copy link
Contributor

mabar commented Jan 6, 2021

Before any output is sent

ignore_user_abort(true);

When connection should be closed, output sent and script continue:

if (connection_aborted() === 0) {
	ob_flush();
	flush();
	if (session_status() === PHP_SESSION_ACTIVE) {
		session_write_close();
	}
}

It should do the trick, but be beware of possible issues:

  • IIS + fastcgi doesn't work at all https://bugs.php.net/bug.php?id=60586#1378935714
  • it should be checked whether session is written into or output is sent after connection was closed
  • I found a note that current working directory is changed after connection is closed, but never really tested it.
  • Time limit is a must-have, in case of ini_get('max_execution_time') === '0' would be an exception thrown on connection abort welcome

@milo Good job, you was faster :D

@mabar
Copy link
Contributor

mabar commented Jan 6, 2021

But ignoring the implementation question - there are imho better options for non-blocking processing like message queues, swoole and possibly fibers in the near future.
I am kinda worried about constantly running into race conditions because this behavior is global and e.g. depending on Doctrine EntityManager->flush() auto-called on script termination is quite often. Whether sql transaction or redirect finished first could create quite unexpected behavior, at least for less advanced users

@dg
Copy link
Member

dg commented Jan 6, 2021

Wouldn't it be better to write a feature request on PHP?

@dg
Copy link
Member

dg commented Jan 6, 2021

@mabar I think the magic is only in Content-Length header.

@dg
Copy link
Member

dg commented Jan 6, 2021

Cannot be solved in PHP.

@dg dg closed this as completed Jan 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants