Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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/
    only_from =
    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 ?

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer
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 Sep 17 '10 at 13:52
@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. – grawity Sep 17 '10 at 14:04
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 Sep 17 '10 at 14:41
@Sirex: I actually meant both sys.stdin and sys.stdout. (Yes, it should've been either or stdout.write().) – grawity Sep 17 '10 at 14:53

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).
share|improve this answer
didnt seem to help initially, but i'll test further. – Sirex Sep 17 '10 at 13:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .