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

Handle aborted connection #95

Merged
merged 8 commits into from
Nov 9, 2022
Merged

Handle aborted connection #95

merged 8 commits into from
Nov 9, 2022

Conversation

withinboredom
Copy link
Collaborator

This PR to #90 handles detecting a closed connection and setting the connection state to aborted both during a flush and during a write to output.

Since we were not previously setting the connection state to aborted, this is a breaking change in that by default, PHP stops working once the connection is aborted unless the user calls ignore_user_abort(true); or the ignore_user_abort setting in php.ini.

frankenphp.c Outdated Show resolved Hide resolved
frankenphp.c Outdated

size_t wrote = go_ub_write(ctx->current_request ? ctx->current_request : ctx->main_request, (char *) str, str_length, &failed);

if(failed) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(failed) {
if (failed) {

(we need to check and fix the CS in the CI btw)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a C-style checker? I'll admit I only recently noticed the space between if & (.

frankenphp.go Outdated Show resolved Hide resolved
frankenphp.go Outdated Show resolved Hide resolved
@withinboredom
Copy link
Collaborator Author

Things to test when compared to fastcgi/apache:

  1. fastcgi_finish_request() should return the correct connection status.
  2. fastcgi_finish_request() shouldn't kill processing because the connection is done.
  3. disconnecting before flush.
  4. disconnecting during output.

Due to the very nature of networks, I expect several race conditions need to be accounted for in unit tests (i.e., we can't detect a socket is closed until we write to it and get a reply that it is closed.)

Base automatically changed from feat/implement-flush to main November 8, 2022 23:56
}

if clientHasClosed(r) {
return true
}

flusher.Flush()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Flush() on a closed connection seems to be a noop, I'm inclined to remove the previous if-statement and just return the result of clientHasClosed(). What are your thoughts on that?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely a micro-optim, but doing this in this order prevents a useless call to Flush() event if it's a noop. I don't have a strong opinion on this TBH.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I don't have strong feelings either, so I'll leave as-is. My optimization thought was mostly to reduce the branching, but it's probably better to leave that sort of optimization for a compiler and just describe what is correct.

Copy link
Owner

@dunglas dunglas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Do you think we can prevent the sleep() in the tests?

@withinboredom
Copy link
Collaborator Author

withinboredom commented Nov 9, 2022

Do you think we can prevent the sleep() in the tests?

I've been mulling it over. It appears that it all resolves in less than a single millisecond, even on the GitHub runners, so if it becomes an issue, we bump it up to a couple of ms and/or commit upstream to zaptest to get some kind of channel on the log output so we can block until logs are output. There is certainly a race condition here, but I can't think of any way to currently get around it, though I'm open to ideas.

@dunglas dunglas merged commit 3abda4f into main Nov 9, 2022
@dunglas
Copy link
Owner

dunglas commented Nov 9, 2022

Thank you!

@dunglas dunglas deleted the handle-connection-aborts branch November 9, 2022 14:09
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

Successfully merging this pull request may close these issues.

2 participants