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

connection.on('data' ...) never fires while opening more than 255 sockets #198

Closed
Saif-A opened this issue Apr 5, 2019 · 12 comments
Closed
Labels

Comments

@Saif-A
Copy link

Saif-A commented Apr 5, 2019

I am trying to start ReactPHP TCP sockets on a range of TCP ports (1000 to 2000). All the ports are open and I can see the connection established while testing using telnet but the sockets never fires the connection.on('data' ...) event. Everything works while using a smaller port range of exactly 255 socket instead of 1000.
reproduce code :

<?php
require 'vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
for ($i = 1000; $i < 2000; $i++) {
    $socket[$i] = new React\Socket\Server('127.0.0.1:' . $i, $loop);
    $socket[$i]->on('connection', function (React\Socket\ConnectionInterface $connection) {
        echo "connection happened";
        $connection->write("Hello " . $connection->getRemoteAddress() . "!\n"); // this part works 
        $connection->on('data', function ($data) use ($connection) {
            echo $data; // for some reason this never fires
        });
    });
}
$loop->run();
@WyriHaximus
Copy link
Member

Works for me:
Screenshot from 2019-04-07 00-00-11

@Saif-A
Copy link
Author

Saif-A commented Apr 7, 2019

maybe I should mention that I am using win10 x64. I have started with few ports and everything was working but as soon as I increased the number to more than 255, the events never fire for the entire range. Could it be php/windows issue ?

@WyriHaximus
Copy link
Member

Good point I tried on Linux yesterday, will try on windows 10 now hold on

@clue clue added the question label Apr 7, 2019
@clue
Copy link
Member

clue commented Apr 7, 2019

@Saif-A Interesting find!

It looks like this might indeed be a limitation of the platform you're using. Other platforms (see @WyriHaximus's post) are known to support a much larger number of sockets. This project builds on top of the APIs that your platform and PHP provides, so I don't think there's much we can do here to avoid this.

I did a quick search and could not find any references for any limitations in Windows that would limit this to 255 sockets though. If you happen to find a way to configure this on the OS level, then this should also take effect for this library (please share your findings!).

In your case, the connection should either succeed successfully with the connection event or it should report an error event at least. Perhaps this helps with diagnosing this?

I believe this has been answered and don't think there's much we can do here, so I'm closing this for now. Please come back with more details if this problem persists and we can reopen this 👍

@clue clue closed this as completed Apr 7, 2019
@WyriHaximus
Copy link
Member

Can confirm that snippet doesn't work on directly Windows 10 (x64), but it does work in WSL on Windows 10 (x64). So going for WSL is probably you best development bet @Saif-A

@Saif-A
Copy link
Author

Saif-A commented Apr 7, 2019 via email

@clue
Copy link
Member

clue commented Apr 7, 2019

@Saif-A I don't see anything special that we're doing in this library that could be the cause of this. Can you try reproducing this with plain PHP without ReactPHP to see if this is an issue in PHP and/or the platform?

Here's some pseudo code to get you started:

$server = stream_socket_server('tcp://127.0.0.1:1000');

$client = stream_socket_accept($server);
$data = fread($client, 8000);

var_dump($data);

@clue
Copy link
Member

clue commented Apr 10, 2019

@Saif-A I just stumbled upon https://docs.microsoft.com/en-us/windows/desktop/winsock/maximum-number-of-sockets-supported-2 which suggests FD_SETSIZE defaults to 64 on Windows. I don't have a system to test this on right now, but this would suggest that any program that uses more than 64 sockets (or streams in ReactPHP) would be unable to check all of them on Windows.

On Unix platforms, this constant is usually defined as 1024, so it's much less of a problem here. As an alternative, you might be able to use one of the alternative loop implementations which provide ways to work around this limitation (e.g. using thread-pools to select() no more than 64 sockets per thread ref).

@leocharrua
Copy link

Hello. I have the same problem (Windows 10 64 bits PHP 7.3.3). Someone has some other solution or workaround? Thanks

@clue
Copy link
Member

clue commented Feb 8, 2021

@leocharrua Can you provide a gist to reproduce the problem you're seeing?

If you're using the default StreamSelectLoop, it may be a good idea to use one of the other loop implementations as per https://github.com/reactphp/event-loop#loop-implementations

If you want to stick to the default StreamSelectLoop, you may want to try temporarily removing the error suppression operator as discussed in reactphp/event-loop#220.

@leocharrua
Copy link

Hello @clue, thanks for your help.

We have a tcp server that accepts conections from multiple clients at the same time. Now, we are stuck with this limitation of 255 sockets, because we have more than 255 devices sending data to the server, and we have data lost.

The StreamSelectLoop can help? I don't know how, can you explain a little more?.

Thanks

@clue
Copy link
Member

clue commented Feb 20, 2021

@leocharrua May I ask you to file an issue in https://github.com/reactphp/event-loop with more details and a link back to this issue? A short gist to reproduce this and details about your platform would be helpful 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants