xinetd keeps process open when client disconnects

0

I've asked this question more or less before on stackoverflow and believed it to be solved (hence accepted the answer) but it turns out it wasnt solved. :-(

In simple terms, I've written a python script which just outputs text constantly to stdout, thats all it does 24/7. I've linked it to this xinetd file

service myservice
{
    instances = 1
    port = 887
    socket_type = stream
    type = UNLISTED
    wait = no
    user = nobody
    server = /usr/local/bin/myscript.py
    only_from = 127.0.0.1 192.168.1.2
    disable = no
    max_load = 5.0
    nice = 5
    per_source = 1
}

This works fine in as much as when a client connects it starts spewing out text on their console. The problem is when the client disconnects, the process launched stays open, blocking the port. There is only one client allowed (instances = 1) but this can occur when the client reboots while connected.

Previously I thought this was because the python script was ignoring kill signals (which it was) but with this fixed, the same behaviour is observed. To clarify, kill -1 etc is now happily observed by the python script.

I'm assuming this is a xinetd issue and fairly simple to fix ?

Sirex

Posted 2010-09-17T08:46:57.950

Reputation: 10 321

Answers

1

Make the server process exit when it detects a disconnection.


Added When the server's OS detects that the TCP connection has been closed, reads to and writes from stdout/stderr will fail with:

IOError: [Errno 104] Connection reset by peer

So make sure your code is not ignoring exceptions when they occur.


However, this (and any other method) will only work when the server knows about the disconnection. Clean reboots usually close all TCP connections, but "pulling the plug" does not.

user1686

Posted 2010-09-17T08:46:57.950

Reputation: 283 655

is this from solid experience ? - because as far as i can tell there no way of doing that from the python script (maybe there is)? - it just spews out output constantly, similar to the "yes" bash command. – Sirex – 2010-09-17T13:52:00.830

@Sirex: sys.stdin is tied to the TCP socket, and if the socket is closed sys.stdin.write will fail. The same applies to any language: you can't write to a closed socket. – user1686 – 2010-09-17T14:04:07.467

ok, ill leave this question open as im going away for 2 weeks, but i'll test when i get back. Im not sure if tcp connection is closed cleanly by the client but ill have a look. in the above, you mean sys.stdout and sys.stdout.write, surely ? – Sirex – 2010-09-17T14:41:55.103

@Sirex: I actually meant both sys.stdin and sys.stdout. (Yes, it should've been either stdin.read() or stdout.write().) – user1686 – 2010-09-17T14:53:10.507

-1

Have you tried to set wait = yes?

According to documentation, that is

wait — Defines whether the service is single-threaded (yes) or multi-threaded (no).

Janne Pikkarainen

Posted 2010-09-17T08:46:57.950

Reputation: 6 717

didnt seem to help initially, but i'll test further. – Sirex – 2010-09-17T13:52:21.243