Skip to content

Accept null for microseconds argument in stream_select() #6879

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

Merged
merged 4 commits into from
Apr 22, 2021

Conversation

kelunik
Copy link
Member

@kelunik kelunik commented Apr 18, 2021

The deprecation of passing null is thrown otherwise.

If the timeout is calculated conditionally before calling stream_select(), passing 0 to avoid the deprecation seems unreasonable, example:

https://github.com/amphp/amp/blob/7d4bbc6e0b47c6bb39b6cce1a4b5942e0c5125fb/lib/Loop/NativeDriver.php#L286

kelunik added 2 commits April 18, 2021 14:26
The deprecation of passing null is thrown otherwise.

If the timeout is calculated conditionally before calling stream_select(), passing  to avoid the deprecation seems unreasonable, example:

https://github.com/amphp/amp/blob/7d4bbc6e0b47c6bb39b6cce1a4b5942e0c5125fb/lib/Loop/NativeDriver.php#L286
@nikic
Copy link
Member

nikic commented Apr 19, 2021

I think this makes sense. If $sec is nullable, then $usec should be too.

cc @kocsismate

@@ -788,6 +789,11 @@ PHP_FUNCTION(stream_select)

PHP_SAFE_MAX_FD(max_fd, max_set_count);

if (secnull && !usecnull) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should allow || usec == 0 here, so those tests wouldn't have broken? After all, this was documented as the default.

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought about that, too, but there's no reason to pass 0 in these tests.

Copy link
Member

Choose a reason for hiding this comment

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

I'm mainly concerned about people who have defined a wrapper around this function that uses $usec = 0 as the default value, because that's the default we used to document. They would now get an exception.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should we deprecate passing 0 or just allow it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Downgraded passing 0 to a deprecation message.

Copy link
Member

@kocsismate kocsismate left a comment

Choose a reason for hiding this comment

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

I think this makes sense. If $sec is nullable, then $usec should be too.

+1

@nikic nikic merged commit 59d5b3d into php:master Apr 22, 2021
@kelunik kelunik deleted the fix-stream-select-usec branch April 22, 2021 08:10
@zobo
Copy link
Contributor

zobo commented Oct 20, 2021

I understand this has as been in the 8.1 tree a while, but I just stumbled on it in the exact same spot as the mentioned issue. Writing code that fully satisfies PHP <= 8.0 and >= 8.1 is.. well.. impossible and results in ugly patches as this https://github.com/sabre-io/event/pull/88/files

Since <= 8.0 did not allow $usec to be NULL there is no way to avoid a deprecation notice - or write version specific split code.

I would suggest to allow $usec to be either NULL or 0.

@nikic
Copy link
Member

nikic commented Oct 20, 2021

I'd be okay with dropping the deprecation. Don't see much value in it.

@zobo
Copy link
Contributor

zobo commented Oct 20, 2021

Can I help in any way?

@kelunik
Copy link
Member Author

kelunik commented Oct 21, 2021

@zobo I haven't seen any issue with passing $usec = null on PHP 7.4 and earlier. Do you have a reproduction script that shows this isn't allowed?

@zobo
Copy link
Contributor

zobo commented Oct 21, 2021

PHP 8.0 and before define the $usec argument as Z_PARAM_LONG https://github.com/php/php-src/blob/PHP-7.4.25/ext/standard/streamsfuncs.c#L764
if you enable declare(strict_types=1); it will throw an error

7.4.13:
Fatal error: Uncaught TypeError: stream_select() expects parameter 5 to be int, null given in ...

8.0.12:
Fatal error: Uncaught TypeError: stream_select(): Argument #5 ($microseconds) must be of type int, null given in ...

code

<?php

declare(strict_types=1);

$r = $w = $e = [STDIN];
stream_select($r, $w, $e, null, null);

@kelunik
Copy link
Member Author

kelunik commented Oct 21, 2021

Strict types have been the missing piece, we're not using them at @amphp, so we didn't see any TypeError.

Regarding the usefulness of the deprecation:

var_dump(stream_select($stream, $empty, $empty, 0, null));

Returns immediately.

var_dump(stream_select($stream, $empty, $empty, null, 0));

Blocks forever.

Given this difference, the deprecation still seems relevant to me.

@zobo
Copy link
Contributor

zobo commented Oct 21, 2021

Yes. But thats controlled by the 4th parameter $sec. Deprecation notice is about the 5th.

@zobo
Copy link
Contributor

zobo commented Oct 27, 2021

I have created a PR that makes the proposed change, removing the deprecation warning.

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.

4 participants