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 data. Recevived 0 instead of expected 1 bytes #72

Closed
anki-code opened this issue Mar 14, 2013 · 49 comments
Closed

Error reading data. Recevived 0 instead of expected 1 bytes #72

anki-code opened this issue Mar 14, 2013 · 49 comments

Comments

@anki-code
Copy link

Hi, when I try to run

videlalvaro/php-amqplib/demo/amqp_consumer.php

I've got the error after process completed:

PHP Fatal error:  Uncaught exception 'PhpAmqpLib\Exception\AMQPRuntimeException' with message 'Error reading data. Received 0 instead of expected 1 bytes' in /application/vendor/videlalvaro/php-amqplib/PhpAmqpLib/Wire/IO/StreamIO.php:50
Stack trace:
#0 /application/vendor/videlalvaro/php-amqplib/PhpAmqpLib/Wire/AMQPReader.php(106): PhpAmqpLib\Wire\IO\StreamIO->read(1)
#1 /application/vendor/videlalvaro/php-amqplib/PhpAmqpLib/Wire/AMQPReader.php(145): PhpAmqpLib\Wire\AMQPReader->rawread(1)
#2 /application/vendor/videlalvaro/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(236): PhpAmqpLib\Wire\AMQPReader->read_octet()
#3 /application/vendor/videlalvaro/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(264): PhpAmqpLib\Connection\AbstractConnection->wait_frame(0)
#4 /application/vendor/videlalvaro/php-amqplib/PhpAmqpLib/Channel/AbstractChannel.php(111): PhpAmqpLib\Connection\AbstractConnection->wait_channel(1, 0)
#5 /application/vendor/videlalvaro/php-amqplib/PhpAmqpLib/Channel/AbstractChannel.php(207): PhpAmqpLib\Channel\AbstractChannel->next_fram in /application/vendor/videlalvaro/php-amqplib/PhpAmqpLib/Wire/IO/StreamIO.php on line 50

What's the problem? Help me please.

@videlalvaro
Copy link
Collaborator

I can't diagnose it with this information. I also can't reproduce it

@Scyks
Copy link

Scyks commented Mar 20, 2013

Found this issue too. You can reproduce it by providing a wrong vhost to AMQPConnection.

@videlalvaro
Copy link
Collaborator

What's the point of providing a wrong vhost?

@Scyks
Copy link

Scyks commented Mar 20, 2013

I mean, when you configure a reabbitmq queue with no vhost and implemnt a vhost like "test" in connection. I hope you could understand me :)

@anki-code
Copy link
Author

Code:
$connect = new AMQPConnection('myhost', 5672, 'test', 'test'); // default vhost = '/'

RabbitMQ (3.0.4):

root@myhost:~# rabbitmqctl list_user_permissions test
Listing permissions for user "test" ...
/ .* .* .*
...done.

Where is the problem?

@luxifer
Copy link

luxifer commented Apr 17, 2013

i had the same issue, i have 2 front and rabbitmq is only on one of these, so when the request is held by the second front i had the same message.
Instead of localhost i put the correct host of the forst front and it works perfectly

@ashish278
Copy link

I am connecting to rabbitmq using guest a/c on default vhost "/".
root@myserver:~# rabbitmqctl list_user_permissions guest
Listing permissions for user "guest" ...
/ .* .* .*
...done.

Permissions seem to be okay but I am still getting this issue.

This happens intermittently on our production boxes and it looks totally undebuggable after "Error reading data. Recevived 0 instead of expected 1 bytes" error.

Please help me out here.

@stof
Copy link
Contributor

stof commented May 11, 2013

I faced the same error today. After and advice from @videlalvaro saying it was probably related to bad credentials, I found that in my case, it was a wrong vhost: I was using /XXXXX as vhost whereas my hosted RabbitMQ on CloudAMQP was actually expecting the vhost without the leading slash XXXXX (I don't give the actual vhost as it would disclose the cloudamqp user name)

@johnkrovitch
Copy link

Hi,
I encounter the same issue here. When I try to connect, it takes a long time and return "Error reading data. Received 0 instead of expected 1 bytes". It does not seem to be a credentials issue, because when I change my credentials to wrong, it returns a different error.

I have access to RabbitMq server through a tunnel, maybe it a cause...

UPDATE : it was a configuration error. the port was not good. I entered the web interface port instead of the rabbitMq one...

@linkdd
Copy link

linkdd commented Jul 12, 2013

Hi,
Same issue here, all credentials are correct, but still getting this error.
I'm trying with the PECL extension to see if the error comes from my RabbitMq configuration.

@ioleo
Copy link

ioleo commented Jul 23, 2013

Happened to me when I tried to login with bad credentials.

@phillpafford
Copy link

loostro is correct, I had the same issue, it was bad credentials

@doubaokun
Copy link

This is caused by error settings of IP, user name, password or port. Needs a mechanism quick timeout and show errors.

@aivsev
Copy link

aivsev commented Oct 8, 2013

Same here. Host? Right. Credentials? Ok. Vhost? Good. Permissions? Checked.
That was madness.
Finally I found the problem. Rabbitmq verssion was too old (1.8) because I used mirror debian sources.

@ioleo
Copy link

ioleo commented Mar 6, 2014

@videlalvaro maybe we could have a more informative error message here?

@dlsniper
Copy link

So, I'm facing the same problem as the original poster.
I've made sure I'm using the right host and other settings and the app mostly works. The issue appears only when there are no more messages in the queue.
I'm using RabbitMQ 3.2.4 (http://www.rabbitmq.com/news.html#2014-03-04T13:11:00+0000) and Ubuntu 12.04 with PHP 5.3.10.
You can see the debug output here: http://pastebin.com/Cg0KJJ2E I've removed some sensitive data from it, was on a test server but still.. :)

I'll try to fix it as well but if someone has an idea on how to do it, do let me know.

@delirehberi
Copy link

if you had change rabbitmq guest/guest password, amqplib return that error.

@videlalvaro
Copy link
Collaborator

Yes, I gotta improve the error message at some point.

The problem is not changing the guest/guest password, but using the wrong credentials.

@dlsniper
Copy link

Hi. I'm pretty sure that I was / am using the right user / pass. It was one
of the first things I've checked for.

Thanks.
On 28 Apr 2014 07:52, "Alvaro Videla" notifications@github.com wrote:

Yes, I gotta improve the error message at some point.

The problem is not changing the guest/guest password, but using the wrong
credentials.


Reply to this email directly or view it on GitHubhttps://github.com//issues/72#issuecomment-41549535
.

@videlalvaro
Copy link
Collaborator

Then probably to a lack of heartbeats, the socket connection died after being idle for a while.

@corentin-larose
Copy link

I use RabbitMq via Haproxy for failover, then, I had this problem since the connection timeout was too low. You should check all the network softwares/equipments between your client and RabbitMq. Hope this helps.

@dlsniper
Copy link

@corentin-larose unfortunately for me, my hardware is AWS. Most of the times it's great, sometimes it fails. But even so, I would expect to have a response in one second, which is my current timeout. However it seems that even if I have messages or not on the message queue, I still get the thing to crash and I have no clue how to fix it unfortunately.

@corentin-larose
Copy link

@disniper the problem is with the idle connection, so as long as you have messages going by and the time between two messages is lower than the littlest timeout in your network, then it works. As soon as the idle time (time between two messages) is greater than the littlest timeout in your network, it fails.

@crypto-sporidium
Copy link

This is interesting and might point to the source of the problem..
It first occurred when I tried to use a vhost.

This code breaks and from the stack trace logs as far as I can see is not setting the vhost name any where ??

 $con_args = array(
            'host' =>  Config::get( 'app.exchangeHost', 'localhost' ),
            'vhost' => Config::get( 'app.exchangeVhost', 'test' ),
            'port' => Config::get( 'app.exchangePort', 5672 ),
            'login' =>  Config::get( 'app.exchangeUser', 'test' ),
            'password' => Config::get( 'app.exchangePass', 'test' ),
        );
        $connection = new AMQPConnection($con_args);
       $connection ->connect(); 

Logs ... ??? is this correct ?

PhpAmqpLib\Connection\AbstractConnection->__construct('test', 'test', '/', false, 'AMQPLAIN', NULL, 'en_US', Object(PhpAmqpLib\Wire\IO\StreamIO))

THIS CODE WORKS !!

$connection = new AMQPConnection(
               Config::get( 'app.exchangeHost', 'localhost' ),
               Config::get( 'app.exchangePort', 5672 ),
               Config::get( 'app.exchangeUser', 'test' ),
               Config::get( 'app.exchangePass', 'test' ),
               Config::get( 'app.exchangeVhost', 'test' )
        );

        // I don't even call connect just call publish later after setting the exchange and so forth
        //$connection ->connect();

        $channel = $connection->channel();
        $routing_key =  'mykey';

        $data = array( 'message' => array(
                'text'   => 'hello server',
                'timestamp' => time(),
            ),
        );

        $msg = new AMQPMessage( json_encode( $data ) );
        $channel->basic_publish( $msg, Config::get( 'app.exchange' , 'TEST' ), $routing_key );
        $channel->close();
        $connection->close();

// done ?? no errors

@bzis
Copy link

bzis commented Jun 26, 2014

I don't use vhost, but have the same problem.
It works good for some time, but then I receive an exception after any action: $chanel->declare() or $chanel->close():

[PhpAmqpLib\Exception\AMQPRuntimeException]
  Error reading data. Received 0 instead of expected 7 bytes
#0 PhpAmqpLib/Wire/AMQPReader.php(128): PhpAmqpLib\Wire\IO\StreamIO->read(7)
#1 PhpAmqpLib/Wire/AMQPReader.php(88): PhpAmqpLib\Wire\AMQPReader->rawread(7)
#2 PhpAmqpLib/Connection/AbstractConnection.php(312): PhpAmqpLib\Wire\AMQPReader->read(7)
#3 PhpAmqpLib/Connection/AbstractConnection.php(345): PhpAmqpLib\Connection\AbstractConnection->wait_frame(0)
#4 PhpAmqpLib/Channel/AbstractChannel.php(121): PhpAmqpLib\Connection\AbstractConnection->wait_channel(1, 0)
#5 PhpAmqpLib/Channel/AbstractChannel.php(229): PhpAmqpLib\Channel\AbstractChannel->next_frame(0)
#6 PhpAmqpLib/Channel/AMQPChannel.php(152): PhpAmqpLib\Channel\AbstractChannel->wait(Array)
#7 Command/CtestCommand.php(88): PhpAmqpLib\Channel\AMQPChannel->close()

or

#0 PhpAmqpLib/Wire/AMQPReader.php(130): PhpAmqpLib\Wire\IO\StreamIO->read(7)
#1 PhpAmqpLib/Wire/AMQPReader.php(88): PhpAmqpLib\Wire\AMQPReader->rawread(7)
#2 PhpAmqpLib/Connection/AbstractConnection.php(312): PhpAmqpLib\Wire\AMQPReader->read(7)
#3 PhpAmqpLib/Connection/AbstractConnection.php(345): PhpAmqpLib\Connection\AbstractConnection->wait_frame(0)
#4 PhpAmqpLib/Channel/AbstractChannel.php(121): PhpAmqpLib\Connection\AbstractConnection->wait_channel(0, 0)
#5 PhpAmqpLib/Channel/AbstractChannel.php(229): PhpAmqpLib\Channel\AbstractChannel->next_frame(0)
#6 PhpAmqpLib/Connection/AbstractConnection.php(396): PhpAmqpLib\Channel\AbstractChannel->wait(Array)
#7 PhpAmqpLib/Connection/AbstractConnection.php(138): PhpAmqpLib\Connection\AbstractConnection->close()
#8 [internal function]: PhpAmqpLib\Connection\AbstractConnection->__destruct()

@scipilot
Copy link

The default "guest" user can only connect via localhost, by default.
https://www.rabbitmq.com/access-control.html

I got this 'received 0' error when trying to connect remotely for the first time after testing successfully locally. Setting up a different user solved it for me.

rabbitmqctl add_user newuser <PASSWORD>
rabbitmqctl set_permissions -p / newuser ".*" ".*" ".*"

@Anticom
Copy link

Anticom commented Sep 22, 2014

Please note, that the user name is also case sensitive.

@RubenKruiswijk
Copy link

Please note, that if you use RabbitMQ behind a Rackspace Cloud Loadbalancer, you should use TCP_STREAM instead of TCP to prevent this issue. Similar settings could probably be used for AWS services.

@dlsniper
Copy link

@RubenKruiswijk could you please elaborate on this? I can't really see anything useful in Google for "rabbitmq TCP_STREAM". Thank you.

@RubenKruiswijk
Copy link

@dlsniper see http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/protocols.html

"TCP Streaming allows either the client or server to send the first message when a connection is established. This option is for protocols where there is no request-response semantic. Either side of the connection can write the first message, with no response being necessarily required or expected."

I guess the regular TCP protocol handler of the Rackspace Load Balancer closes the connection earlier because no traffic is sent.

@oriolgm
Copy link

oriolgm commented Aug 10, 2015

We also had this issue and it was related with the setup of the HAProxy. It was closing the connections after a short period of time. The solution were found by reading this post:

https://deviantony.wordpress.com/2014/10/30/rabbitmq-and-haproxy-a-timeout-issue/

Another solution is to use heartbeats.

Hope this helps

@rskanron
Copy link

I get this issue when using heartbeats if the process runs over the heartbeat timeout. I'm not sure why it would try reading again without checking the heartbeat first. Actually, I would think there would be an error when trying to ack the message but the connection is closed.

@nubeiro
Copy link
Member

nubeiro commented Feb 21, 2016

Is this still an issue? I can't reproduce the problem.

@krzysztof-gzocha
Copy link

For me the problem was missing mbstring extension..

@nubeiro
Copy link
Member

nubeiro commented Mar 14, 2016

mbstring extension is already required in composer.json file, so it should not be an issue anymore.

@nubeiro nubeiro closed this as completed Mar 14, 2016
@nubeiro nubeiro removed the waiting label Mar 14, 2016
@taozywu
Copy link

taozywu commented Apr 27, 2016

@anki-code do you have solved your problem ?

i can give u one idea ... see your username and password or vhost

@topikito
Copy link

Just for the record, as @krzysztof-gzocha said, I installed the extension and that fixed my issue.

The polyfill was triggering a Notice:

PHP Notice:  iconv_strlen(): Detected an illegal character in input string in …/vendor/symfony/polyfill-mbstring/Mbstring.php on line 338

Which contains:

    public static function mb_strlen($s, $encoding = null)
    {
        $encoding = self::getEncoding($encoding);
        var_dump($s);

        return iconv_strlen($s, $encoding);
    }

Doing a var_dump on $s:

string(14) "\000\000\000\000�\000
\000
\000    \000"

Which explains my issue - In case anyone else gets the same due to this reason.

@stof
Copy link
Contributor

stof commented May 18, 2016

@topikito please open an issue on the symfony/polyfill repository, as this looks like a bug in the polyfill which should be fixed.

@krzysztof-gzocha
Copy link

@stof in my situation there was just an missing extension and polyfill was working fine.
If @topikito didn't actually have it installed before then I assume everything is correct.

@topikito
Copy link

Done @stof symfony/polyfill#63

@michaelperrin
Copy link

I get this error too (Error reading data. Recevived 0 instead of expected 7 bytes) although:

  • The mbstring extension is installed
  • My host, port, user, password, vhost configuration are correct
  • I'm using a RabbitMQ version (3.6.5)

The messages are consumed, but this error is shown.

I'm using RabbitMQ 3.6.5 in a Docker container. PHP is running in a PHP 7 Docker container.

I have not found the problem yet, but I'll try to investigate more. If someone has an idea that would be appreciated.

@taozywu
Copy link

taozywu commented Sep 22, 2016

@michaelperrin
you must look Virtual Hosts set, Including the permissions.
and you can follow this link for your problem

  1. Error reading data. Recevived 0 instead of expected 1 bytes #72
    2.stackoverflow

at last good luck!

@michaelperrin
Copy link

@taozywu Thanks for the help. Unfortunately I had to ask on StackOverflow, as I didn't find any solution (http://stackoverflow.com/questions/39640489/error-reading-data-while-executing-a-rabbitmq-consumer-in-symfony).

@jhonatanTeixeira
Copy link

AbstractConnection.php

protected function wait_frame($timeout = 0)
    {
        if (is_null($this->input))
        {
            $this->setIsConnected(false);
            throw new AMQPRuntimeException('Broken pipe or closed connection');
        }

        $currentTimeout = $this->input->getTimeout();
        $this->input->setTimeout($timeout);

        try {
            // frame_type + channel_id + size
            $this->wait_frame_reader->reuse(
                $this->input->read(AMQPReader::OCTET + AMQPReader::SHORT + AMQPReader::LONG)
            );

            $frame_type = $this->wait_frame_reader->read_octet();
            $class = self::$PROTOCOL_CONSTANTS_CLASS;
            if (!array_key_exists($frame_type, $class::$FRAME_TYPES)) {
                throw new AMQPRuntimeException('Invalid frame type ' . $frame_type);
            }
            $channel = $this->wait_frame_reader->read_short();
            $size = $this->wait_frame_reader->read_long();

            // payload + ch
            $this->wait_frame_reader->reuse($this->input->read(AMQPReader::OCTET + (int) $size));

            $payload = $this->wait_frame_reader->read($size);
            $ch = $this->wait_frame_reader->read_octet();

        } catch (AMQPTimeoutException $e) {
            $this->input->setTimeout($currentTimeout);
            throw $e;
        }

        $this->input->setTimeout($currentTimeout);

        if ($ch != 0xCE) {
            throw new AMQPRuntimeException(sprintf(
                'Framing error, unexpected byte: %x',
                $ch
            ));
        }

        return array($frame_type, $channel, $payload);
    }

SocketIO.php

    public function read($n)
    {
        $res = '';
        $read = 0;

        $buf = socket_read($this->sock, $n);
        while ($read < $n && $buf !== '' && $buf !== false) {
            // Null sockets are invalid, throw exception
            if (is_null($this->sock)) {
                throw new AMQPRuntimeException(sprintf(
                    'Socket was null! Last SocketError was: %s',
                    socket_strerror(socket_last_error())
                ));
            }

            $read += mb_strlen($buf, 'ASCII');
            $res .= $buf;
            $buf = socket_read($this->sock, $n - $read);
        }

        if (mb_strlen($res, 'ASCII') != $n) {
            throw new AMQPIOException(sprintf(
                'Error reading data. Received %s instead of expected %s bytes',
                mb_strlen($res, 'ASCII'),
                $n
            ));
        }

        return $res;
    }

As you can see, AMQPReader::OCTET + AMQPReader::SHORT + AMQPReader::LONG = 7.

this value will be passed to the read function wich will try to read from a socket and according to php documentation:
"socket_read() returns a zero length string ("") when there is no more data to read."

when the queue is empty and rabbit returns nothing a empty string is return from socket_read wich is not 7 bytes long, then the error occurs. its a bug, i dont have time to solve it right now and make a MR, and i'm using stream instad, but thats a shame since sockets are much faster and i needed speed.

@m0onspell
Copy link

@michaelperrin thank you, answer on stackoverflow fixed my issue. But does that means that we basically unable to use sockets? Isn't it a bug?

@michaelperrin
Copy link

@m0onspell This is probably a bug indeed, but I didn't manage to find what was wrong... That's too bad as the use of sockets would probably result in better performance.

@m0onspell
Copy link

@jhonatanTeixeira
Copy link

jhonatanTeixeira commented May 23, 2017 via email

@Ocramius
Copy link

@michaelperrin I think this got solved via #448

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

No branches or pull requests