0
var arDrone = require('ar-drone');
var PaVEParser = require('./PaVEParser');
var output = require('fs').createWriteStream('./vid.h264');
var client;
var mostRecentFrame;
var frameCounter = 0;

const net = require('net');
const server = net.createServer((c) => {
  // 'connection' listener
  console.log('client connected');
  c.on('end', () => {
    console.log('client disconnected');
  });
    c.on('data', (data) => {
    c.write(mostRecentFrame);
    });
});
server.on('error', (err) => {
  throw err;
});
server.listen('/tmp/ard.sock', () => {
  console.log('server bound');
});

process.stdin.on('data', function() {
    console.log("Ending the program, landing the drone and terminating the connection.");
    server.close();
    process.exit();
});

When I force-kill this program, I cannot re-launch it.

daniel@beepboop:~/AR-drone Project$ node test-save-pngs-no-flight.js 
/home/daniel/AR-drone Project/test-save-pngs-no-flight.js:24
  throw err;
  ^

Error: listen EADDRINUSE /tmp/ard.sock
    at Object.exports._errnoException (util.js:1036:11)
    at exports._exceptionWithHostPort (util.js:1059:20)
    at Server._listen2 (net.js:1239:19)
    at listen (net.js:1288:10)
    at Server.listen (net.js:1377:5)
    at Object.<anonymous> (/home/daniel/AR-drone Project/test-save-pngs-no-flight.js:26:8)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)

I believe this is because the socket wasn't closed correctly.

  1. How can I make the program close the socket when I force kill it, and
  2. How can I manually close the socket afterwards from the command line?

Edit:

I am using Ubuntu 16.04. By force kill, I mean that I am hitting "control-C" during runtime.

Daniel Paczuski Bak
  • 3,720
  • 8
  • 32
  • 78
  • What OS? How are you force killing it? Have you verified that it is not still running by checking system processes? Have you looked at what sockets the system reports as open? – jfriend00 Sep 23 '16 at 14:01
  • I'm unsure about how to check system processes. Does control-C just put them to sleep? I added the other info. – Daniel Paczuski Bak Sep 23 '16 at 14:11
  • You can check the system processes that are running with `ps aux`. You can kill an individual process by typing `kill xxxx` where `xxxx` is the process id you saw in `ps aux`. This is how you clean up a process you don't want running any more. See here http://www.cyberciti.biz/tips/linux-display-open-ports-owner.html for how to list open sockets and the processes that own them. – jfriend00 Sep 23 '16 at 14:21
  • @jfriend00 This is how it works. Domain sockets are not removed on process exit and that's by design. See the links to the issues on GitHub that I posted in my answer below. – rsp Sep 23 '16 at 14:27

1 Answers1

2

Yes, this is how it works. This is a shorter example to reproduce it:

var net = require('net');
const server = net.createServer((c) => {
});
server.on('error', (err) => {
  throw err;
});
server.listen('/tmp/x.sock', () => {
  console.log('server bound');
});

You can avoid that problem by running:

server.close();

before the program exits.

In your case you can avoid that problem by handling the SIGINT:

var net = require('net');
const server = net.createServer((c) => {
});
server.on('error', (err) => {
  throw err;
});
server.listen('/tmp/x.sock', () => {
  console.log('server bound');
});
process.on('SIGINT', () => {
  server.close();
  process.exit();
});

See also:

rsp
  • 107,747
  • 29
  • 201
  • 177
  • How can I trigger a server.close() when the program is force-killed? Is this possible? Further, how can I fix this problem after I've caused it? Do I need to restart my machine? – Daniel Paczuski Bak Sep 23 '16 at 14:19
  • @DanielPaczuskiBak If by "force-killed" you mean Ctrl+C then you can handle the SIGINT signal - you may also handle SIGTERM and other signals, but you will not be able to handle SIGKILL. If you've caused the problem then you need to remove the `/tmp/ard.sock` file. Restarting the system should also remove files in `/tmp` but all you need is remove the file. – rsp Sep 23 '16 at 14:23