Background: So, basically I have created an API in which I run a calculation function on the background thread and return a response to the user without waiting for the thread to complete.
About function in thread: it has two cases (decides on the input parameters), one is faster (completes in around 10 seconds), and another one is slower (takes 2-5 minutes) to calculate. After calculation, it updates some values on DB.
Problem: It works fine when running on a local development environment but is failing on the Heroku app only on the slower calculation (works fine on faster calculation).
Here is some code reference that can be helpful:
@api.route('/calculate', methods=['POST'])
def do_calculation(fast_mode):
**Do some stuff**
thread = Thread(daemon=True, target=calculate, args=(fast_mode))
return json.dumps({message: 'Calculation started'})
def calculate(fast_mode):
if fast_mode:
my_function(0.15) # this fails on Heroku
else:
my_function() # this works fine on Heroku
def my_function(input_data = None):
**Calculation stuff**
**Update in DB**
Error on heroku logs:
2022-07-28T17:22:48.319694+00:00 heroku[router]: at=info method=OPTIONS path="/calculate?fastMode=1" host=something.herokuapp.com dyno=web.1 connect=0ms service=1ms status=200 bytes=393 protocol=https
2022-07-28T17:22:48.464106+00:00 heroku[router]: at=info method=POST path="/calculate?fastMode=1" host=something.herokuapp.com dyno=web.1 connect=0ms service=109ms status=200 bytes=1016 protocol=https
2022-07-28T17:22:48.803074+00:00 app[web.1]: free(): double free detected in tcache 2
2022-07-28T17:22:48.809857+00:00 app[web.1]: [2022-07-28 17:22:48 +0000] [4] [WARNING] Worker with pid 36 was terminated due to signal 6
2022-07-28T17:22:48.812310+00:00 app[web.1]: [2022-07-28 17:22:48 +0000] [52] [INFO] Booting worker with pid: 52
Dockerfile for deploying:
FROM python:3.10
WORKDIR /webapp
ADD . /webapp
COPY requirements.txt .
RUN pip install gunicorn
RUN cd /usr
RUN pip install -r requirements.txt
CMD ["gunicorn", "--log-file=-", "--log-level=debug", "wsgi:app", "--worker-class gevent", "--timeout 600", "-w 2"]
I was stuck for two days and tried many online solutions for similar problems but had no luck. Hope to get help from this :)
If you need to ask for more information, please let me know. Thank you in advance.