Python – child process, stderr to DEVNULL but printing error

child process, stderr to DEVNULL but printing error… here is a solution to the problem.

child process, stderr to DEVNULL but printing error

I’m developing a French chatbot using python. For my first text-to-speech attempt, I used espeak in conjunction with mbrola. I call it with a subprocess :

from subprocess import run, DEVNULL

def speak(text):
    command = ["espeak", "-vmb-fr1", text]
    run(command, stderr=DEVNULL, stdout=DEVNULL)

speak("Bonjour.")

As you can see, I send stderr and stdout to /dev/null

When I run the program it seems to work fine, espeak is talking, but I get this :

*** Error in `mbrola': free(): invalid pointer: 0x08e3af18 ***
 Error in `mbrola': free(): invalid pointer: 0x0988af88 ***

I think this is a C bug in mbrola. I don’t think I can fix it. But it works, so I just want to eliminate the error. What can I do ? Is there a way?


Edit, respond abarnert :

When I redirect stdout and stderr via shell (python myscript.py 2>&1 >/dev/null), the message still shows.

  • Distribution: Debian 9.3
  • glibc version: 2.24

Solution

Run it with setsid ( Just precede the string to the command and parameters). This prevents it from opening /dev/tty to report malloc errors. It also blocks terminal signals, including when the terminal is closed SIGHUP, so as not to affect the process, which can be a good thing or a bad thing.

Alternatively, set the environment variable LIBC_FATAL_STDERR_ to some nonempty string, whose name I found several similar questions .

Related Problems and Solutions