0

My python daemon is run through systemd, with stderr logging on a file.

A simplified version of the systemd service file is this:

[Unit]
Description=Test

[Service]
ExecStart=/path/to/test.py
StandardError=file:/path/to/error.log

[Install]
WantedBy=multi-user.target

The python daemon instantiates a threaded TCP server and runs serve_forever on it. The problem is any error happening after the thread start is not logged on the error.log file. Any error happening before is logged.

A simplified version of the python daemon is this:

import threading, socketserver

class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
  pass

class TCPHandler(socketserver.BaseRequestHandler):
  def handle(self):
    pass

#barfoo
TCPServer = ThreadedTCPServer(("127.0.0.1", 12345), TCPHandler)
TCPThread = threading.Thread(target=TCPServer.serve_forever)
TCPThread.start()
#foobar

If I uncomment "barfoo", a "name not defined" is logged in the error.log file.

If I uncomment "foobar", nothing is logged.

Also, since the TCP server thread is still running, the systemd daemon is shown as running, so I can't even detect the error from there.

If I run the daemon from command line, both errors are correctly logged on stderr (and redirected if I redirect stderr on a file). Trying the same redirect in the systemd script, instead of the StandardError directive, has the same effect as the latter.

EDIT: I tested the same thing with a generic infinite looping thread, for the same effect:

import threading

def run():
  while True:
    pass

#aaaaaa # logs an error on systemd's stderr
test_thread = threading.Thread(target=run)
test_thread.start()
#foobar # doesn't log an error on systemd's stderr
Tukler
  • 51
  • 4

1 Answers1

0

I found the solution. Leaving this for future reference.

It was actually not a systemd problem at all, but rather a buffering problem.

Disabling the default Python (for versions < 3.7) buffering, by running "python -u" or using the PYTHONUNBUFFERED environment variable, causes all the output to be correctly logged.

Tukler
  • 51
  • 4