danielzzz / node-ping

a poor man's ping library (using udp scanning) for node
MIT License
336 stars 105 forks source link

isAlive false, no error in electron, but isAlive true (same IP) under node? #134

Closed JamesNewton closed 3 years ago

JamesNewton commented 3 years ago

I'm baffled. Any insight on how to debug this appreciated. We are using ping to check if a robot is online before we attempt to connect to it. The specific code is: https://github.com/cfry/dde/blob/master/core/robot.js#L1049

copied here for your convenience: (and THIS code IS open source, so I can share it)

                let ping = require('ping') //https://www.npmjs.com/package/ping
                ping.sys.probe(this.ip_address,
                                function(isAlive, err){
                                    if (isAlive) {
                                        if(job_instance.name == "set_link_lengths") { //don't attempt to set link lengths again!
                                            this_robot.start_aux(job_instance)
                                        }
                                        else { setTimeout(function(){
                                                            this_robot.set_link_lengths(this_job)
                                                          },
                                                          500)} //in case dexster is booting up, give it a chance to complete boot cycle
                                        //this_robot.use_ping_proxy(job_instance)
                                    }
                                    else if (err){
                                        this_job.stop_for_reason("errored_from_dexter_connect", "Ping on robot: " + this_robot.name + " errored with: " + err.message)
                                    }
                                    else {
                                        this_job.stop_for_reason("errored_from_dexter_connect", "Could not connect to Dexter.\nIf it is because Dexter is initializing,\ntry again in a minute,\nor click Misc pane 'simulate' button.", true)
                                        //3rd arg is true so that we will run the stop method for dex_read_file job,
                                        //so that this error of "not connected" will reset the orig editor files menu item.
                                    }
                                },
                               {timeout: 10}
                               )

On Windows, and Mac, this code is working (e.g. reaching the "if (isAlive)" clause when the robot is online and pingable. In Ubuntu 16 and 18, it is reaching the "else {" clause, not the "else if (err) {" clause.

At the same time, on the same Ubuntu 18 machine, a ping to the same IP address from bash is returned, and the following little node.js test code works as expected, reporting "Dex is alive" every time:

let ping = require('ping') //https://www.npmjs.com/package/ping
ping.sys.probe("192.168.0.142"
        , function(isAlive, err){
                var msg = isAlive ? 'Dex is alive' : 'Dex is dead:'+err;
                console.log(msg);
                }
        , {timeout: 10}
        );

I have searched the electron issues for any mention of ping not working. e.g. https://github.com/electron/electron/issues?q=is%3Aissue+%22node-ping%22+ and I'm not finding anything.

I've also, of course, searched your issues for anything related to electron and none of that seems to match: https://github.com/danielzzz/node-ping/issues?q=is%3Aissue+electron

I'm sort of at a point of... I don't know how to go forward here. It's probably an electron issue, but I was hoping you might have some ideas.

JamesNewton commented 3 years ago

To narrow this down, I use the source code of node-ping to write code that does the same thing, in a very minimal version inside the electron app. "out" is just console log, but it goes to a special text box which is easy to see while the program is running.

var cp = require('child_process');
ping = cp.spawn("ping", ["192.168.0.142"]);
ping.stdout.on('data', (data) => {
  out(`stdout: ${data}`);
});

ping.stderr.on('data', (data) => {
  out(`stderr: ${data}`);
});

ping.on('close', (code) => {
  out(`child process exited with code ${code}`);
});

the stderr message comes back as stderr: ping: icmp open socket: Operation not permitted and the child process existed with code 2.

Why doesn't node-ping report an error? Maybe I wrote the minimal code incorrectly? But the same code runs just fine under node, so electron is failing to run it with the same permissions...

JamesNewton commented 3 years ago

Wonder if it's related to this: https://github.com/electron/electron/issues/18521

JamesNewton commented 3 years ago

Submitted a bug report to Electron. https://github.com/electron/electron/issues/27168

mondwan commented 3 years ago

Try 0.4.1, and see whether the error stderr: ping: icmp open socket: Operation not permitted can be reported now

JamesNewton commented 3 years ago

Sorry, it's been 4 months so we moved on and are no longer using this library at all. Rather than pretend that I could find time to revert, update the library and try it, I will just let you know that testing won't happen. Close this issue if you feel you've corrected the problem.

mondwan commented 3 years ago

oops. Anyway, thanks for the report.