Failing to close service that runs a streamlit app

I have a streamlit script that is run by a systemd service on a raspberry pi. If I try to stop my script using systemctl stop, it seems that streamlit is ignoring the SIGTERM signal:

07:38:00 systemd[1]: Stopping streamlitui.service - Starts webserver for ...
07:39:31  systemd[1]: streamlitui.service: State 'stop-sigterm' timed out. Killing.
07:39:31  systemd[1]: streamlitui.service: Killing process 2758 (streamlit) with signal SIGKILL.
07:39:31  systemd[1]: streamlitui.service: Main process exited, code=killed, status=9/KILL
07:39:31  systemd[1]: streamlitui.service: Failed with result 'timeout'.

Following [Stop Streamlit app guide — Restack], one should connect to the signal.SIGTERM which I tried like this:

import streamlit as st
import signal


def cleanup_and_exit():
    print('Aborting')

if __name__ == '__main__' :
    signal.signal(signal.SIGTERM, lambda signum, frame: cleanup_and_exit()) 
    # ValueError: signal only works in main thread of the main interpreter 

    st.write("Test")

    print("Exit Streamlit")

But this leads to the error

ValueError: signal only works in main thread of the main interpreter

How can I make my streamlit app to react on SIGTERM ?