Out of memory: Killed process and HTTP request to instance: connection closed before message completed

Hi, I am running an app that takes a video via a form, resizes it using MoviePy, and then uploads it to Dropbox. The same code works on my local machine but fails here and I have no idea how to find a solution. There are two problems: ‘Out of memory: Killed process’ and ‘HTTP request to instance: connection closed before message completed.’

The question is why these errors? Is it even possible to call MoviePy on the free tier or is there not enough memory? And why was the HTTP request closed? Is it even possible to call the Dropbox API? Where can I find the answers to these questions? Thanks.

" 2023-03-23T19:49:30.093 app[1781949b460028] ams [info] [ 2463.181215] Out of memory: Killed process 518 (gunicorn) total-vm:242656kB, anon-rss:76936kB, file-rss:0kB, shmem-rss:4kB, UID:0 pgtables:332kB oom_score_adj:0

2023-03-23T19:49:30.103 proxy[1781949b460028] ams [error] could not make HTTP request to instance: connection closed before message completed "

Hi! Thanks for sharing the specific error messages; that always helps with this kind of thing.

I think the first message means pretty much what it says: your app instance is trying to use more memory than is available. As a result, the Linux kernel in your app’s VM is killing your app. That’s expected behavior in this case.

You may notice that the second message is from proxy, whereas the first says app. That means that the second message is from Fly’s edge proxy and says that the edge proxy’s connection to your app is being closed; it’s not your app trying to access the Dropbox API. (Apologies if you already know this! I just wasn’t sure based on what you wrote.)

You’ll also notice from the timestamps that the proxy message occurs right after the app’s out-of-memory error. When your app is killed due to memory pressure, any connections it had open will be closed. In this case, it looks like that happened before your app could finish responding to the edge proxy, which is why you get this message. So I’m pretty certain that it’s the same root cause: it looks like you’re out of memory.

Resizing videos sounds like it could be a decently memory-intensive operation, and I wouldn’t be surprised if it was too much for a 256 MB free tier VM. Based on running your app locally, do you know how much memory it needs when processing a video? You can also check memory statistics for your app on Fly’s web interface (click on your app and then look at the “Metrics” tab); just be aware that if your app’s memory usage goes up very quickly, then that change might not be reported in the UI before your app instance gets killed.

Thank you for your kind reply. I wrapped my function in mem_profiler:
"from memory_profiler import profile

instantiating the decorator

@profile
@login_required
def upload_video(request):
… "

and got :
"Line # Mem usage Increment Occurrences Line Contents

20     62.6 MiB     62.6 MiB           1           @wraps(view_func)
21                                                 def _wrapped_view(request, *args, **kwargs):
22     62.7 MiB      0.1 MiB           1               if test_func(request.user):
23     85.3 MiB     22.6 MiB           1                   return view_func(request, *args, **kwargs)
24                                                     path = request.build_absolute_uri()
25                                                     resolved_login_url = resolve_url(login_url or settings.LOGIN_URL)
26                                                     # If the login url is the same scheme and net location then just
27                                                     # use the path as the "next" url.
28                                                     login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
29                                                     current_scheme, current_netloc = urlparse(path)[:2]
30                                                     if (not login_scheme or login_scheme == current_scheme) and (
31                                                         not login_netloc or login_netloc == current_netloc
32                                                     ):
33                                                         path = request.get_full_path()
34                                                     from django.contrib.auth.views import redirect_to_login
35                                         
36                                                     return redirect_to_login(path, resolved_login_url, redirect_field_name)"

In the view panel dashboard there is spike from normal 114 MiB to max.

I am using one vm of 256 MiB for the app, I set up Postgres so I assume another is using Postgres. It remains the third one of 256, can’t I scale to 512 MiB on this app? I tried flyctl scale memory 512, but it gives me “Error This command doesn’t support V2 apps yet, use fly machines update and fly machines clone instead”. In the meantime I’m trying to limit the memory usage via :
"# using resource
import resource

def limit_memory(maxsize):
soft, hard = resource.getrlimit(resource.RLIMIT_AS)
resource.setrlimit(resource.RLIMIT_AS, (maxsize, hard))"

but I don’t know if it’s feasible.

Thank you very much!

Solved! I used “flyctl machines update 1781949b4***** --memory 512” to expand the memory.

Thank you very much, keep it up with the good work!

Edit:
Today I saw an amount due of 2 cents, and find out from https://community.fly.io/t/i-dont-understand-free-tier/5145/4:
“The 512MB RAM VM would create costs as you’d have an extra 256MB (256MB base + 256MB extra) and we’d bill for the extra. We bill memory per VM.”

So I will scale back to 256 MiB and try to limit pyhton resources to not hit the max allowed.