Supabase edge function time out

I have an edge function that calls an api that takes around 30 seconds to provide a response since that API calls GPT. Sometimes my edge function times out before I can get a result. How do I increase this timeout time?
28 Replies
garyaustin
garyaustin3mo ago
Edge functions don't have any timeout close to 30 seconds if you are waiting for a fetch call. https://supabase.com/docs/guides/functions/limits 150 seconds is the timeout waiting. If you are consuming CPU time doing work then that accumulated can only be 2 seconds, but hard to measure that with "clock" time.
vspuzzler
vspuzzlerOP3mo ago
Well all I am doing is calling the webhook and waiting for it give a return call Can I share you my supabase edge function code
garyaustin
garyaustin3mo ago
You can, if I can't help maybe someone else can.
What is your exact error? Where are you getting the error from? Also is this a hosted edge function?
vspuzzler
vspuzzlerOP3mo ago
I am getting this: Function call failed: Edge Function returned a non-2xx status code I will send detailed logs
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type'
};
serve(async (req)=>{
// Handle CORS preflight requests
if (req.method === 'OPTIONS') {
return new Response(null, {
headers: corsHeaders
});
}
try {
// Get the JSON payload from the request
const payload = await req.json();
// Set up a 30-second timeout for the fetch call
const controller = new AbortController();
const timeout = setTimeout(()=>controller.abort(), 30000); // 30 seconds
try {
// Make the API call to the external analysis service
const response = await fetch("MY_API", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(payload),
signal: controller.signal
});
clearTimeout(timeout);
if (!response.ok) {
throw new Error(`API call failed: ${response.status} ${response.statusText}`);
}
const analysisData = await response.json();
return new Response(JSON.stringify(analysisData), {
headers: {
...corsHeaders,
'Content-Type': 'application/json'
}
});
} catch (error) {
clearTimeout(timeout);
throw error;
}
} catch (error) {
return new Response(JSON.stringify({
error: error.message || 'Failed to analyze base'
}), {
status: 500,
headers: {
...corsHeaders,
'Content-Type': 'application/json'
}
});
}
});
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type'
};
serve(async (req)=>{
// Handle CORS preflight requests
if (req.method === 'OPTIONS') {
return new Response(null, {
headers: corsHeaders
});
}
try {
// Get the JSON payload from the request
const payload = await req.json();
// Set up a 30-second timeout for the fetch call
const controller = new AbortController();
const timeout = setTimeout(()=>controller.abort(), 30000); // 30 seconds
try {
// Make the API call to the external analysis service
const response = await fetch("MY_API", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(payload),
signal: controller.signal
});
clearTimeout(timeout);
if (!response.ok) {
throw new Error(`API call failed: ${response.status} ${response.statusText}`);
}
const analysisData = await response.json();
return new Response(JSON.stringify(analysisData), {
headers: {
...corsHeaders,
'Content-Type': 'application/json'
}
});
} catch (error) {
clearTimeout(timeout);
throw error;
}
} catch (error) {
return new Response(JSON.stringify({
error: error.message || 'Failed to analyze base'
}), {
status: 500,
headers: {
...corsHeaders,
'Content-Type': 'application/json'
}
});
}
});
I know the API itself works because I have tested it 10 times in a row and it outputted the desired json This is the listening log:
{
"boot_time": null,
"cpu_time_used": null,
"deployment_id": "--------",
"event_type": "Log",
"execution_id": "--------",
"function_id": "-----------",
"level": "log",
"memory_used": [],
"project_ref": "-----",
"reason": null,
"region": "us-east-1",
"served_by": "supabase-edge-runtime-1.67.3 (compatible with Deno v1.45.2)",
"timestamp": "2025-06-23T21:18:01.391Z",
"version": "2"
}
]
{
"boot_time": null,
"cpu_time_used": null,
"deployment_id": "--------",
"event_type": "Log",
"execution_id": "--------",
"function_id": "-----------",
"level": "log",
"memory_used": [],
"project_ref": "-----",
"reason": null,
"region": "us-east-1",
"served_by": "supabase-edge-runtime-1.67.3 (compatible with Deno v1.45.2)",
"timestamp": "2025-06-23T21:18:01.391Z",
"version": "2"
}
]
[
{
"boot_time": null,
"cpu_time_used": 12,
"deployment_id": "qlchwkcigszmbidckawa_c177d6c7-4328-4fbd-966e-25fbf985781b_2",
"event_type": "Shutdown",
"execution_id": "ee92d27e-151a-43b1-b106-a18348fb574e",
"function_id": "c177d6c7-4328-4fbd-966e-25fbf985781b",
"level": "log",
"memory_used": [
{
"external": 2890063,
"heap": 4032380,
"mem_check_captured": null,
"total": 6922443
}
],
"project_ref": "------",
"reason": "EarlyDrop",
"region": "us-east-1",
"served_by": "supabase-edge-runtime-1.67.3 (compatible with Deno v1.45.2)",
"timestamp": "2025-06-23T21:19:16.197Z",
"version": "2"
}
]
[
{
"boot_time": null,
"cpu_time_used": 12,
"deployment_id": "qlchwkcigszmbidckawa_c177d6c7-4328-4fbd-966e-25fbf985781b_2",
"event_type": "Shutdown",
"execution_id": "ee92d27e-151a-43b1-b106-a18348fb574e",
"function_id": "c177d6c7-4328-4fbd-966e-25fbf985781b",
"level": "log",
"memory_used": [
{
"external": 2890063,
"heap": 4032380,
"mem_check_captured": null,
"total": 6922443
}
],
"project_ref": "------",
"reason": "EarlyDrop",
"region": "us-east-1",
"served_by": "supabase-edge-runtime-1.67.3 (compatible with Deno v1.45.2)",
"timestamp": "2025-06-23T21:19:16.197Z",
"version": "2"
}
]
This is the shutdown ^^^ And it says reason as EarlyDrop Any solution for this?
garyaustin
garyaustin3mo ago
No idea. EarlyDrop is usually some resource ran out. Edge functions start up and then can run several different calls before they retire. Each call is supposed to get 150sec of "wait" time and 2 seconds of CPU time.
Maybe post the overall log of events as I think it shows a function start up, then each call it gets, then spin down. There could be several start ups though.
I don't see a loop in your code that would use up CPU time. There will be messages in the log (possibly shutdown) even if code is working for functions that reach their limit of staying alive 400seconds for pro, 150 for free. Also feel free to try supabase/edge-runtime issues for potential help from a dev but unknown time frame. You are waiting for a user here to have an idea for help.
I don't see a loop in your code that would use up CPU time. There will be messages in the log even if code is working for functions that reach their limit of staying alive 400seconds for pro.
tomaspozo
tomaspozo3mo ago
could it be your AbortController timeour code?
// Set up a 30-second timeout for the fetch call
const controller = new AbortController();
const timeout = setTimeout(()=>controller.abort(), 30000); // 30 seconds
// Set up a 30-second timeout for the fetch call
const controller = new AbortController();
const timeout = setTimeout(()=>controller.abort(), 30000); // 30 seconds
try extending that to 140000
vspuzzler
vspuzzlerOP3mo ago
Oh, I added that because I thought it would fix the issue I was having I will try that though
vspuzzler
vspuzzlerOP3mo ago
No description
vspuzzler
vspuzzlerOP3mo ago
That didn't seem to work These are the logs I am getting
garyaustin
garyaustin3mo ago
Is this not hosted edge functions? I asked before but you did not say. There is no error in those logs. Also add some console.logs to the function to help debug.
vspuzzler
vspuzzlerOP3mo ago
Sorry yes this is a hosted edge function
tomaspozo
tomaspozo3mo ago
did it worked?
vspuzzler
vspuzzlerOP3mo ago
Unfortunately no
tomaspozo
tomaspozo3mo ago
but according to your logs there is no error add logs as Gary suggests to see what steps does the function does reach if you call your api directly how long does it take to respond?
vspuzzler
vspuzzlerOP3mo ago
It takes around 25 seconds for a response I am adding logs right now to test
garyaustin
garyaustin3mo ago
I don’t understand the log. Why would there be a localhost port if hosted?
vspuzzler
vspuzzlerOP3mo ago
Maybe I am misunderstanding what you mean by hosted Edge Function I just have it setup on Supabase
garyaustin
garyaustin3mo ago
There is local development with the CLI and there is a hosted instance on Supabase’s infrastructure. The limits I provided are for hosted functions. You would see them in the dashboard under a project you created in an org.
vspuzzler
vspuzzlerOP3mo ago
Oh yea then mine would be hosted since I created them in the Supabase website Ok so I am testing with more logs
vspuzzler
vspuzzlerOP3mo ago
Wait I think I found the issue lol
vspuzzler
vspuzzlerOP3mo ago
No description
garyaustin
garyaustin3mo ago
Seems like your API call is sending back that it timed out.
garyaustin
garyaustin3mo ago
Also not sure if critical buy I thought you needed to return ok as the response for Options. But seems to work for you the way you have it.
No description
vspuzzler
vspuzzlerOP3mo ago
Ok, so I found some issue with my AWS Actually nvm
vspuzzler
vspuzzlerOP3mo ago
"🚨 Edge function error Error: API call failed: 504 Gateway Timeout\n at Server.<anonymous> (file:///tmp/user_fn_qlchwkcigszmbidckawa_c177d6c7-4328-4fbd-966e-25fbf985781b_7/source/index.ts:48:13)\n at eventLoopTick (ext:core/01_core.js:168:7)\n at async #respond (https://deno.land/std@0.168.0/http/server.ts:221:18)\n"
vspuzzler
vspuzzlerOP3mo ago
No description
vspuzzler
vspuzzlerOP3mo ago
I am assuming the issue is not with my AWS API now because I have it send out a test webhook message to my discord for error logging, and it got sent to me after I got this error, which shows that the issue wasn't with the API right?

Did you find this page helpful?