Repository navigation
Fix GH-24173: Sockets left in non-blocking mode on Windows #24174
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
Open
vibbow
wants to merge
4
commits into
php:PHP-8.4
Choose a base branch
from
vibbow:fix-gh24173-win-restore-blocking
base: PHP-8.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9136a2c
Fix GH-24173: Sockets left in non-blocking mode on Windows
vibbow 48a00ba
Make timed socket writes non-blocking on Windows
vibbow 5529795
Stop gh24173_write_timeout.phpt at the first timed-out write
vibbow d4b7a43
Run gh24173_write_timeout.phpt on Windows only
vibbow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| --TEST-- | ||
| GH-24173 (Sockets from synchronous stream_socket_client() are left in non-blocking mode on Windows) | ||
| --EXTENSIONS-- | ||
| sockets | ||
| --SKIPIF-- | ||
| <?php | ||
| if (PHP_OS_FAMILY !== 'Windows') die('skip Windows only'); | ||
| ?> | ||
| --FILE-- | ||
| <?php | ||
| $srv = stream_socket_server('tcp://127.0.0.1:0', $errno, $errstr); | ||
| $client = stream_socket_client('tcp://' . stream_socket_get_name($srv, false), $errno, $errstr, 5); | ||
| $conn = stream_socket_accept($srv); | ||
|
|
||
| $sock = socket_import_stream($client); | ||
| socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, ['sec' => 0, 'usec' => 100000]); | ||
|
|
||
| // No data is sent: a blocking socket waits and times out, while a | ||
| // non-blocking one fails at once with WSAEWOULDBLOCK. | ||
| var_dump(@socket_read($sock, 10)); | ||
| var_dump(socket_last_error($sock) === SOCKET_ETIMEDOUT); | ||
| ?> | ||
| --EXPECT-- | ||
| bool(false) | ||
| bool(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| --TEST-- | ||
| GH-24173 (Timed writes on socket streams must not block past the timeout) | ||
| --SKIPIF-- | ||
| <?php | ||
| if (PHP_OS_FAMILY !== 'Windows') die('skip Windows only'); | ||
| ?> | ||
| --FILE-- | ||
| <?php | ||
| $srv = stream_socket_server('tcp://127.0.0.1:0', $errno, $errstr); | ||
| $client = stream_socket_client('tcp://' . stream_socket_get_name($srv, false), $errno, $errstr, 5); | ||
| $conn = stream_socket_accept($srv); | ||
|
|
||
| // Neither side reads, so writes eventually fill the socket buffers. A write | ||
| // that times out may still have written part of the chunk, so stop at the | ||
| // first timeout rather than at the first zero-length write. | ||
| $chunk = str_repeat('x', 1 << 20); | ||
| foreach (['client' => $client, 'accepted' => $conn] as $name => $stream) { | ||
| stream_set_timeout($stream, 0, 200000); | ||
| for ($i = 0; $i < 256; $i++) { | ||
| @fwrite($stream, $chunk); | ||
| if (stream_get_meta_data($stream)['timed_out']) { | ||
| break; | ||
| } | ||
| } | ||
| echo $name, ': '; | ||
| var_dump(stream_get_meta_data($stream)['timed_out']); | ||
| } | ||
| ?> | ||
| --EXPECT-- | ||
| client: bool(true) | ||
| accepted: bool(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be changing semantic and can potentially break things. The issues is that with async connect php_network_connect_socket() returns early and intentionally leaves the OS socket non-blocking, but php_stream_generic_socket_factory() still sets is_blocked = 1. So the first timed fwrite() on such stream switches to non-blocking and then restores to blocking here, which is a mode the socket never had. A later stream_socket_recvfrom() or stream_socket_sendto() then blocks indefinitely on Windows while it still returns immediately on POSIX.
The actual problem is that is_blocked tracks what PHP wants and not what the OS socket is, and Winsock cannot query the real mode. We should probably track it separately (there seems to be still space to do it in ABI safe way in php_netstream_data_t but would need to think about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it should check the return value here.