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

Duplicated payload from gprc-call #15

Open
jeff-bonevich opened this issue Apr 10, 2021 · 5 comments
Open

Duplicated payload from gprc-call #15

jeff-bonevich opened this issue Apr 10, 2021 · 5 comments

Comments

@jeff-bonevich
Copy link

jeff-bonevich commented Apr 10, 2021

When testing a simple gPRC method that takes a non-streaming request but returns a streaming response, the gprc-call node is returning only the last payload sent by the server.

image

gprc-call-dups.txt

I believe this is due to gprc-call.js sending messages with identical _msgid; perhaps node-red is cacheing things? Anywho, I can submit a patch to fix.

@jeff-bonevich
Copy link
Author

Odd. This is starting to look like a node-red bug. Wrote a very simple node plugin that duplicates my very simple function node, and the two behave very differently. The simple function (no grpc involved) sends unique message to debug, but wrapping the same logic in a custom node does not: displays the same behavior I am seeing with the grcp nodes, the same msg.payload multiple times. I will reach out to the node-red community and follow up.

@jeff-bonevich
Copy link
Author

@namgk
Copy link
Collaborator

namgk commented Apr 11, 2021

Hi Jeff,

This is a Javascript basic problem, what you're running into is called closure inside a loop problem.

The difference between the function node and the wrapper node is that with the function node, the msg gets cloned upon arriving to the debug node. Meanwhile, the wrapper node just call the callback with the same msg object.

Put it simple, the callbacks are put in the runtime's event queue and when all of them are executed, the msg object has been changed to reflect the last value of the loop.

@namgk
Copy link
Collaborator

namgk commented Apr 11, 2021

You can try this:

const closureSend = (msg) => {
    return (sendFunction) => {
        sendFunction(msg)
    }
}

node.on('input', function(msg, send, done) {
            var name = msg.payload.name;
            var count = msg.payload.count;

            for (var i = 0; i < count; i++) {
                msg.payload = "Hello, " + name + i;
                console.log("msg", msg);
                closureSend(msg)(send);
            }
        });

@jeff-bonevich
Copy link
Author

That makes sense, however, it does not solve the issue - I tried it in the simple node and still the debug node is only seeing that last msg.payload. The closure is itself both initialized and resolved in the for loop, so it makes no difference.

So far the only thing that resolves the issue is cloning the msg (my original "fix" did as well, but as you pointed out it was not flow-friendly). Came across this article that sums it up: https://nodered.org/blog/2019/09/13/cloning-messages

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

2 participants