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

Error reading 1 bytes #68

Closed
wartw opened this issue Oct 11, 2021 · 20 comments
Closed

Error reading 1 bytes #68

wartw opened this issue Oct 11, 2021 · 20 comments
Assignees
Labels
question Further information is requested

Comments

@wartw
Copy link

wartw commented Oct 11, 2021

This error often occurs when there are more than tens of thousands of concurrent requests

Fatal error: Uncaught RouterOS\Exceptions\StreamException: Error reading 1 bytes in /www/wwwroot/e5218129d40e2b46.sn.mynetname.net/routeros-api-php/src/Streams/ResourceStream.php:59 Stack trace: #0 /www/wwwroot/e5218129d40e2b46.sn.mynetname.net/routeros-api-php/src/APILengthCoDec.php(115): RouterOS\Streams\ResourceStream->read() #1 /www/wwwroot/e5218129d40e2b46.sn.mynetname.net/routeros-api-php/src/APIConnector.php(53): RouterOS\APILengthCoDec::decodeLength() #2 /www/wwwroot/e5218129d40e2b46.sn.mynetname.net/routeros-api-php/src/Client.php(255): RouterOS\APIConnector->readWord() #3 /www/wwwroot/e5218129d40e2b46.sn.mynetname.net/routeros-api-php/src/Client.php(310): RouterOS\Client->readRAW() #4 /www/wwwroot/e5218129d40e2b46.sn.mynetname.net/routeros-api-php/src/Client.php(481): RouterOS\Client->read() #5 /www/wwwroot/e5218129d40e2b46.sn.mynetname.net/routeros-api-php/src/Client.php(538): RouterOS\Client->login() #6 /www/wwwroot/e5218129d40e2b46.sn.mynetname.net/routeros-api-php/src/Client.php(88): RouterOS\Client->connect in /www/wwwroot/e5218129d40e2b46.sn.mynetname.net/routeros-api-php/src/Streams/ResourceStream.php on line 59

@wartw wartw added the question Further information is requested label Oct 11, 2021
@EvilFreelancer
Copy link
Owner

EvilFreelancer commented Oct 12, 2021

Hello! I'm not sure if this is a problem with this library, the RouterOS API is a very fragile thing in itself.

My opinion is that you see this error because the size of the buffer allocated for processing API requests was exceeded, I am sure that if you try to repeat this procedure (thousands of concurrent requests) using any other library, you will get a similar error.

Check the RouterOS logs first, probably answer will be here at the moment when this error occurs.

@EvilFreelancer
Copy link
Owner

EvilFreelancer commented Oct 12, 2021

Another reason is that the API made a mistake and tries to send request data N-1 to request N, but request N-1 has already received it and therefore the API does not know what to do and spits out an error.

By the way, I am very surprised that you found the way to make many parallel requests to the RouterOS API, usually this is a big headache :) Especially if you need a valid data from the RouterOS (not only creating/updating/deleting of something, because these three returns nothing).

@matracine
Copy link
Collaborator

Hello,
This error is triggered when the underlying lib did not manage to read data from the router.
I think that, as Paul said, your router is overloaded and cannot answer correctly to the API call.
@EvilFreelancer Perhaps it could be interesting to catch the StreamException at a higher level and rethrow a more contextualy explicit one ?

@EvilFreelancer
Copy link
Owner

EvilFreelancer commented Oct 12, 2021

Hello @matracine it's a good idea! I guess for the first need to reproduce this error via tests and add exception. You did the part about working with streams, perhaps it would be better if you implement this exception :) If you have spare time of course.

@wartw
Copy link
Author

wartw commented Oct 12, 2021

@EvilFreelancer After testing, I found that if I request the API from different IPs at the same time, this problem will not occur. If I request multiple requests at the same time on a single IP, the problem will occur.
Is there a way to specify the IP to access the API?

@EvilFreelancer
Copy link
Owner

EvilFreelancer commented Oct 12, 2021

You probably mean source ip?

Nope, library does not support this.

@wartw
Copy link
Author

wartw commented Oct 12, 2021

I mean, for example, if there are 192.168.1.2~5 in the vm, can you specify one of them to access ros?

@EvilFreelancer
Copy link
Owner

EvilFreelancer commented Oct 12, 2021

This library is just a converter of API commands from human-readable format to format which API server may understand, and that's all. So if you need something more, than it should be implemented from your side :)

@wartw
Copy link
Author

wartw commented Oct 12, 2021

For example curl's CURLOPT_INTERFACE

@EvilFreelancer
Copy link
Owner

EvilFreelancer commented Oct 12, 2021

The RouterOS API is not an ordinary HTTP API (RESTful/SOAP/GraphQL/etc.), it's TCP-based, with a non-typical format of requests and responses, with bitwise shifting. Communication between the API server takes place using the stream extensions, options can be passed via stream_context_create method. And as i know in raw TCP mode not possible to use proxy.

Ah, btw, curl flags is also not possible to use.

@matracine
Copy link
Collaborator

@EvilFreelancer : https://www.php.net/manual/en/context.socket.php, parameter "bindto"....
It's possible, but it seems to be outside the scope if the API...

@EvilFreelancer
Copy link
Owner

EvilFreelancer commented Oct 12, 2021

@matracine thanks, I did not know about this solution. Need to add this useful option to one of future releases.

@EvilFreelancer
Copy link
Owner

@wartw so as temporary solution you may add bindto to array with stream context options, and in some of future releases it will be implemented in normal way (via Config parameter I mean)

@wartw
Copy link
Author

wartw commented Oct 12, 2021

I will try tomorrow

@EvilFreelancer
Copy link
Owner

@wartw hello! A bindto solution is helped to you?

@wartw
Copy link
Author

wartw commented Oct 14, 2021

Not tested yet, it's busy these days

@EvilFreelancer
Copy link
Owner

Okay, please ping me if it works, i'll add support of this parameter to the library then.

@EvilFreelancer
Copy link
Owner

EvilFreelancer commented Oct 18, 2021

Hello! I've added support of socket_options parameter to the Config class.

https://github.com/EvilFreelancer/routeros-api-php#list-of-available-configuration-parameters
https://github.com/EvilFreelancer/routeros-api-php/blob/master/src/Config.php#L87

It's easy to use:

$client = new Client([
    'socket_options' => [
        'bindto' => '192.168.1.10:7000'
    ]
]);

Already available in version 1.4.3

@wartw
Copy link
Author

wartw commented Oct 21, 2021

I forgot to tell you, it works

@wartw
Copy link
Author

wartw commented Nov 3, 2021

Very good operation (processing tens of thousands of different records at the same time, there will be no abnormalities

@wartw wartw closed this as completed Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants