Skip to content
Closed
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
45 changes: 45 additions & 0 deletions ext/standard/tests/poll/poll_stream_add_unpollable_wsapoll.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
Io\Poll: WSAPoll reports a handle that is not a socket as unsupported
--SKIPIF--
<?php
if (!Io\Poll\Backend::WSAPoll->isAvailable()) {
die("skip WSAPoll backend required\n");
}
if (!function_exists('proc_open')) {
die("skip proc_open required\n");
}
?>
--FILE--
<?php
require_once __DIR__ . '/poll.inc';

$ctx = new Io\Poll\Context(Io\Poll\Backend::WSAPoll);

// WSAPoll() only polls sockets, while files and pipes give a CRT fd
$proc = proc_open([PHP_BINARY, '-n', '-r', 'echo "done";'], [1 => ['pipe', 'w']], $pipes);
foreach (['regular file' => fopen(__FILE__, 'r'), 'pipe' => $pipes[1]] as $label => $stream) {
try {
$ctx->add(new StreamPollHandle($stream), [Io\Poll\Event::Read]);
echo "$label: added\n";
} catch (Io\Poll\FailedHandleAddException $e) {
printf("%s: %s, code %s\n", $label, $e->getMessage(),
Io\Poll\FailedPollOperationException::ERROR_NOSUPPORT === $e->getCode() ? 'ERROR_NOSUPPORT' : $e->getCode());
}
fclose($stream);
}
proc_close($proc);

// a socket is still added and reported
[$r, $w] = pt_new_socket_pair();
$watcher = $ctx->add(new StreamPollHandle($r), [Io\Poll\Event::Read]);
echo "socket: added\n";
fwrite($w, "ping");
$events = $ctx->wait(Time\Duration::fromSeconds(5));
var_dump(count($events), $events[0] === $watcher);
?>
--EXPECT--
regular file: Failed to add handle, code ERROR_NOSUPPORT
pipe: Failed to add handle, code ERROR_NOSUPPORT
socket: added
int(1)
bool(true)
10 changes: 10 additions & 0 deletions main/poll/poll_backend_wsapoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ static zend_result wsapoll_backend_add(php_poll_ctx *ctx, int fd, uint32_t event
return FAILURE;
}

/* WSAPoll() can only poll sockets. Files and pipes give a CRT fd,
* which it never reports. */
int type;
int type_len = sizeof(type);
if (getsockopt((SOCKET) fd, SOL_SOCKET, SO_TYPE, (char *) &type, &type_len) != 0
&& WSAGetLastError() == WSAENOTSOCK) {
php_poll_set_error(ctx, PHP_POLL_ERR_NOSUPPORT);
return FAILURE;
}

php_poll_fd_entry *entry = php_poll_fd_table_get_new(backend_data->fd_table, fd);
if (!entry) {
php_poll_set_error(ctx, PHP_POLL_ERR_NOMEM);
Expand Down
Loading