The issue you're encountering — where your Python script runs fine when executed manually but crashes (std::bad_alloc) when run as a service — is likely due to environment differences between your interactive shell and the systemd service environment.
🔍 What's Happening?
When you run the script manually, it inherits your user shell environment (including PATH, locale settings, ROS setup, etc.). However, systemd services run in a minimal environment with limited or no environment variables. This can cause:
Missing paths to libraries
Missing ROS environment setup
Memory allocation issues if some initialization logic fails silently
The bad_alloc error typically indicates that some part of the code tried to allocate memory and failed — this could be because of misconfigured dependencies or missing resources that would have been available in your normal shell session.
✅ Steps to Fix the Issue
1. Use a Proper Systemd Service File
Instead of using a bash script to launch your Python node via systemd, define a proper .service file in /etc/systemd/system/.
Create a file like: /etc/systemd/system/gto_audio.service
This will help you identify what exactly caused the bad_alloc — maybe a missing library or resource.
4. Verify Resource Limits
Sometimes, systemd imposes stricter memory limits than your user shell. You can increase memory limits in the service file:
ini
LimitAS=infinity
LimitRSS=infinity
But this should only be done if necessary.
5. Test Environment Differences
You can simulate the systemd environment manually to debug:
bash
env -i /bin/bash --noprofile --norc
Then try running your script inside this clean environment. If it fails, you know it depends on environment variables that aren't set in systemd.
✅ Summary
Step
Action
✅
Create a proper systemd .service file
✅
Ensure all environment variables are set (especially ROS-related)
✅
Use a wrapper script to source setup files before launching Python
✅
Check logs using journalctl
✅
Consider adding memory/resource limits if needed
Let me know if you want help writing the full service file or wrapper script!
User
when the script it run with systemd, it starts taking up a lot of memory, up to 13G, however when i run it by hand, it takes a few megabytes. what may be the problem?
Assistant
This is a very telling symptom:
✅ When run manually: ~few MB
❌ When run via systemd: climbs up to 13 GB and crashes with std::bad_alloc
This strongly suggests that something in your Python or ROS2 node behaves differently under systemd, probably due to missing environment variables, configuration files, or resources — which causes it to go into an infinite loop, memory leak, or failed initialization path that tries (and fails) to allocate large chunks of memory.
🔍 Root Cause Possibilities
1. Missing Environment Variables
Systemd runs services in a minimal environment. If your script depends on any environment variables (like ROS_DOMAIN_ID, paths, or model file locations), they may be missing when run via systemd.
If the code assumes these values are set and doesn't validate them, it might:
Try to load a default model from an incorrect path
Fail silently and retry endlessly
Allocate buffers based on invalid config values
2. Missing or Wrong Configuration Files
Your TTS node might look for config/model files in a specific location. When run as a systemd service, it might not find them due to:
Relative paths in the code
Environment-specific paths not set (HOME, BASE_DIR, etc.)
Different working directory
This can cause the node to misbehave, e.g., trying to allocate memory for models that don’t exist.
3. Locale/Encoding Issues
Systemd services often have minimal locale settings. If your Python code uses string operations or libraries that assume UTF-8 encoding and falls back to ASCII, this can lead to silent failures or unexpected behavior.
Add this to your service file to fix locale issues: