Sonic Pi latest version 4.6.0 headless on Linux Mint works except OSC port number is always changing after restart

I have hacked my way to get it working. I can send OSC messages from Python and receive these in Sonic-Pi. Great. I figured out how to do headless. Works. I need this as a part of an art installation which means after a reboot everything should just work. I understand that the ever-changing port number is a kind of security measure. However, I cannot figure out how to automate this. Local host is just fine if this would be an issue. I looked everywhere, but I cannot find a fix for this. I have learned this feature changed over the past years and some people found fixes that no longer seems to work like looking in the logs for the port number. Also, the docs are not really useful on this topic. Maybe I am overlooking the solution and it is hiding in plain sight. I would appreciate it if someone could help me out. Otherwise, I need to turn to plain supercollider and python, but before I do, I like to double-check.

Did some further thinking and looked at … /sonic-pi/app/server/ruby/bin/daemon.rb Ruby is not my specialty, but one thing that could help is at startup write a file with port number, token etc. and delete it when the daemon is killed. That would be a great help for localhost implementations at least …

One day further, and I figured out why port 4560 was not showing up. Killing the daemon leaves orphaned processes. Starting again detects port 4560 is already taken and locates a fresh one. Confusing :-/ pkill -9 -f “scsynth|beam.smp|sonic-pi|boot-lin.sh” cleans up. However, I found that sending a message to /daemon/exit will kill the process, but here you need a security token.

When I look into daemon.rb you can enable this I see now …

    # Uncomment for debugging purposes
    Util.log "Ports: #{@ports.inspect}"

    # Uncomment for debugging purposes
    Util.log "Daemon token: #{@daemon_token}"

That will do the trick … so now I can parse the logs, or even put this in a separate file.

Honestly, I think OSC handling with the security imposed should be handled in a better way …