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

fix: handle client disconnect properly with ignore_user_abort true #207

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Sapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ public static function sendResponse(ResponseInterface $response): void
if ($copied <= 0) {
break;
}
// Abort on client disconnect.
// With ignore_user_abort(true), the script is not aborted on client disconnect.
// To avoid reading the entire stream and dismissing the data afterward, check between the chunks if the client is still there.
if (1 === ignore_user_abort() && 1 === connection_aborted()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

https://www.php.net/manual/en/function.ignore-user-abort.php
This PHP function does return an int - looking at its name, I would have assumed it returned a bool !
So this code does look OK.

Copy link
Member

Choose a reason for hiding this comment

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

I had the same experience ;)

Copy link
Member

Choose a reason for hiding this comment

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

just figured, I should cover this better in PHPStan: phpstan/phpstan-src#2489

Copy link
Member

Choose a reason for hiding this comment

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

also connection_aborted impurity will be covered by phpstan/phpstan-src#2555

Copy link

Choose a reason for hiding this comment

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

I'd assume 0 means false and 1 is true?
If so, this code falsely breaks when the connection was aborted AND we want to ignore that?

Or did i mix something up?

Copy link
Contributor

Choose a reason for hiding this comment

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

https://www.php.net/manual/en/function.ignore-user-abort.php

1 === ignore_user_abort() means "ignore user abort has been enabled" (somewhere in "the system" or "the code" prior to this point)

1 === connection_aborted() means that a connection has "gone away".

When these 2 things both happen, we are deciding, for this "manual" stream-copy loop, we do want to stop feeding the stream copy (even though "the system" has enabed ignore_user_abort()). Because we know that it is pointless to keep looping and pushing out chunk-size bytes each time.

break;
}
$left -= $copied;
}
} else {
Expand Down
31 changes: 31 additions & 0 deletions tests/HTTP/SapiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,35 @@ public function testSendWorksWithCallbackAsBody(): void

self::assertEquals('foo', $result);
}

public function testSendConnectionAborted(): void
{
$baseUrl = getenv('BASEURL');
if (!$baseUrl) {
$this->markTestSkipped('Set an environment value BASEURL to continue');
}

$url = rtrim($baseUrl, '/').'/connection_aborted.php';
$chunk_size = 4 * 1024 * 1024;
$fetch_size = 6 * 1024 * 1024;

$stream = fopen($url, 'r');
$size = 0;

while ($size <= $fetch_size) {
$temp = fread($stream, 8192);
if (false === $temp) {
break;
}
$size += strlen($temp);
}

fclose($stream);

sleep(5);

$bytes_read = file_get_contents(sys_get_temp_dir().'/dummy_stream_read_counter');
$this->assertEquals($chunk_size * 2, $bytes_read);
$this->assertGreaterThanOrEqual($fetch_size, $bytes_read);
}
}
69 changes: 69 additions & 0 deletions tests/www/connection_aborted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

use Sabre\HTTP;

include '../bootstrap.php';

class DummyStream
{
private int $position;

public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool
{
$this->position = 0;

return true;
}

public function stream_read(int $count): string
{
$this->position += $count;

return random_bytes($count);
}

public function stream_tell(): int
{
return $this->position;
}

public function stream_eof(): bool
{
return $this->position > 25 * 1024 * 1024;
}

public function stream_close(): void
{
file_put_contents(sys_get_temp_dir().'/dummy_stream_read_counter', $this->position);
}
}

/*
* The DummyStream wrapper has two functions:
* - Provide dummy data.
* - Count how many bytes have been read.
*/
stream_wrapper_register('dummy', DummyStream::class);

/*
* Overwrite default connection handling.
* The default behaviour is however for your script to be aborted when the remote client disconnects.
*
* Nextcloud/ownCloud set ignore_user_abort(true) on purpose to work around
* some edge cases where the default behavior would end a script too early.
*
* https://github.com/owncloud/core/issues/22370
* https://github.com/owncloud/core/pull/26775
*/
ignore_user_abort(true);

$body = fopen('dummy://hello', 'r');

$response = new HTTP\Response();
$response->setStatus(200);
$response->addHeader('Content-Length', 25 * 1024 * 1024);
$response->setBody($body);

HTTP\Sapi::sendResponse($response);