hapijs / hapi

The Simple, Secure Framework Developers Trust
https://hapi.dev
Other
14.63k stars 1.34k forks source link

reply interface called twice #3340

Closed dinfyru closed 8 years ago

dinfyru commented 8 years ago
Debug: internal, implementation, error 
    Error: reply interface called twice
    at Object.exports.assert (/var/www/web-site/swinechat/node_modules/hoek/lib/index.js:736:11)
    at Function.internals.response (/var/www/web-site/swinechat/node_modules/hapi/lib/reply.js:150:10)
    at reply (/var/www/web-site/swinechat/node_modules/hapi/lib/reply.js:72:22)
    at callback (/var/www/web-site/swinechat/modules/routes.js:37:17)
    at playerModel.where.populate.populate.populate.findOne.exec (/var/www/web-site/swinechat/controller/game/base.js:55:17)
    at Query.<anonymous> (/var/www/web-site/swinechat/node_modules/mongoose/lib/query.js:2233:28)
    at /var/www/web-site/swinechat/node_modules/kareem/index.js:259:21
    at /var/www/web-site/swinechat/node_modules/kareem/index.js:127:16
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickDomainCallback (internal/process/next_tick.js:122:9)

controller Base.js called callback (cb()) for use reply http://pastebin.com/B8FYEDMj I have a loop that once per second refers to the server (function - getTimeEnergy in Base.js). And sometimes there is this error "reply interface called twice"

hapi version 15.0.3 nodejs - 6.3.0

mtharrison commented 8 years ago

You need to ensure you only ever call reply(result) once per request.

dinfyru commented 8 years ago

@mtharrison i use once per request

Marsup commented 8 years ago

I don't think the code you pasted will help us understand what you're doing wrong, it doesn't contain any hapi related code.

dinfyru commented 8 years ago

function cb() leads to router (example: Base.js:55)

$lib.s.route({
        method: 'POST',
        path:'/game',
        config: {
            plugins: {
                'hapi-io': 'user-action'
            }
        },
        handler: function (request, reply) {
            require($lib.controllerpath + 'game/base')(callback, request, true);

            function callback (err, result){
                return reply(result);
            }
        }
    });
Marsup commented 8 years ago

Best guess is this.getTimeEnergy throws an exception which triggers an implicit reply. hapi protects you from failures in your code by doing that.

dinfyru commented 8 years ago

The error occurs when you call at the same time 2 soket.emmit image

dinfyru commented 8 years ago

@Marsup how i can fix it?)

mtharrison commented 8 years ago

@Dinfyru I advise you to try to create a minimal example that shows the problem and we can debug from there. Otherwise it's too difficult to advise. Have a look at How to create a Minimal, Complete, and Verifiable example if you need help.

dinfyru commented 8 years ago

I call socket.emit 2 times for route

$lib.s.route({
        method: 'POST',
        path:'/game',
        config: {
            plugins: {
                'hapi-io': 'user-action'
            }
        },
        handler: function (request, reply) {
            require($lib.controllerpath + 'game/base')(callback, request, true);

            function callback (err, result){
                return reply(result);
            }
        }
    });

And this return error

Debug: internal, implementation, error 
    Error: reply interface called twice
dinfyru commented 8 years ago

this

socket.emit('user-action', { userAction: 'start'}, function(res) {
    socketFunc(res);
});

and this

socket.emit('user-action', { userAction: 'getTimeEnergy' }, function(res) {
    $('.header .energy .text').text(res.val);
    if (res.type !== 'time') {
        $('.header .energy .progress').css('width', (res.val + '%'));
    }
});
dinfyru commented 8 years ago

The error only occurs when they are called at the same time, and then they fall into one reguest (somehow)

dinfyru commented 8 years ago

This will fix my problem? https://github.com/mtharrison/susie In example i see many reply

reply.event({ id: 1, data: 'my data' });
setTimeout(function () {
     reply.event({ id: 2, data: { a: 1 } }); // object datum
 }, 500);
dinfyru commented 8 years ago

Or ho i can disable error (reply called twice)

mtharrison commented 8 years ago

I advise you to try to create a minimal example that shows the problem and we can debug from there.

This advice still stands. You're not giving us enough to help. Simply adding another library won't fix the problem.

Marsup commented 8 years ago

You're mixing up websockets and normal handlers, I have no idea what you're doing there. This should really go into the discuss repo, this is not the place.

dinfyru commented 8 years ago

@Marsup and what i can use for websockets?

Marsup commented 8 years ago

nes.